/*
Note: Self-selecting tab code disabled because it causes a brief flash on at least some browsers (IE win for sure).
Note: Therefore, this whole code could be removed in favor of Dreamweaver created roll-over behaviors.

Functions to make sure the navigation bar tabs are set right, i.e. when you're in programming section, programming tab should be down.
Also handles rollovers and clicks.

By jeff@cyberiandesign.com

This code expects:
	- any html page using at root (like /page.html) or in a folder/section at root (like /folder/another_page.html)
		- pages CAN NOT be at a third level (like /folder/folder2/page.html) or the code will mess up the rollover images on the tabs
	- folder names must correspond to section names, i.e. /sectionName

Known Sections includes names of sections that appear in URLs, i.e. domain.com/sectionName/page.html
	- not every section has to have a tab image on the page (i.e. "contact" and "about" don't have images and that's okay)
	- every folder that can contain a page using this code must be listed in the Known Sections

*/

KNOWN_SECTIONS  = [ "advertising","networks","overview","programming", "contact", "about" ];
// The following on based on images/tabs being in HTML like:
// <img src="assets/images/overviewTab.gif" name="overviewTab" id="overviewTab" />
ID_PREFIX = "";
ID_SUFFIX = "Tab";
IMAGE_SUFFIX = "Tab"; // based on template value being: ../assets/images/overviewTab.gif
// images filenames have suffixes to designate up, over, and down states
UP_SUFFIX = "";
OVER_SUFFIX = "_F2";
DOWN_SUFFIX = "_F3";
IMAGE_EXT = ".gif";

sectionForThisPage = "";

function setupNav() {
	// determine location of images

	// setup tab nav bar
	sectionForThisPage = getSectionForThisPage(); // i.e. "programming"

	// DISABLED: if (sectionForThisPage != "Root") updateNavImages(sectionForThisPage);
	
	preloadImages();
}

function getImageFolderPath () {
	// returns path to images folder; assumes certain images are on the page, assumes images folder is called "images"
	// test look for a section tab image graphic
	var lastIndex = KNOWN_SECTIONS.length - 1;
	for ( var i=0; i<=lastIndex; i++) {
		var sectionName = KNOWN_SECTIONS[i];
		var imageObject  = getImageObject(sectionName);
		if (imageObject == null) continue; // if can't find object on page, bail on this one
		// if found, extract its image path
		var fullPath = imageObject.src;
		var endOfPath = fullPath.lastIndexOf("/") + 1; // TODO: Make compatible with Windows backslash
		return fullPath.substring(0,endOfPath);
	}
}

function onMouseDown (sectionName) {
	// user going to this section; switch this graphics to down state
	changeImageState ( sectionName, "down" );
}

function onMouseOver (sectionName) {
	// 
	if (sectionName != sectionForThisPage) changeImageState ( sectionName, "over" );
}

function onMouseOut (sectionName) {
	if (sectionName != sectionForThisPage) changeImageState ( sectionName, "up" );
}

function preloadImage (sectionName, state) {
	var imagePath = getImagePath(sectionName, state);
	var newImage = new Image;
	newImage.src = imagePath;
}

function preloadImages () {
	// go through all known sectionsand preload images for all states
	var lastIndex = KNOWN_SECTIONS.length - 1;
	for ( var i=0; i<=lastIndex; i++) {
		var sectionName = KNOWN_SECTIONS[i];
		var imageObject  = getImageObject(sectionName);
		if (imageObject == null) continue; // if can't find object on page, bail on this one (section "about" or other doesn't have an image on the page)
		preloadImage ( sectionName, "down" );
		preloadImage ( sectionName, "over" );
	}
}

function updateNavImages (sectionForThisPage) {
	// go through all known sections and update graphics
	var lastIndex = KNOWN_SECTIONS.length - 1;
	for ( var i=0; i<=lastIndex; i++) {
		var sectionName = KNOWN_SECTIONS[i];
		if ( sectionName == sectionForThisPage) {
			// this section name image should be in the down state
			changeImageState ( sectionName, "down" );
		}
		else {
			changeImageState ( sectionName, "up" );
		}
	}
}

function getImageObject (sectionName) {
	// returns object of tab for sectionName
	return object = document.images[ ID_PREFIX + sectionName + ID_SUFFIX ];
}

function changeImageState ( sectionName, state ) {
	var imageObject  = getImageObject(sectionName);
	if (imageObject == null) return; // if can't find object on page, bail on this one
	var imagePath = getImagePath(sectionName, state);
	imageObject.src = imagePath;
}

function getImagePath (sectionName, state) {
	var imagePath;
	switch (state) {
		case "up":
			imagePath = getImageFolderPath() + sectionName + IMAGE_SUFFIX + UP_SUFFIX + IMAGE_EXT;
			break;
		case "over":
			imagePath = getImageFolderPath() + sectionName + IMAGE_SUFFIX + OVER_SUFFIX + IMAGE_EXT;
			break;
		case "down":
			imagePath = getImageFolderPath() + sectionName + IMAGE_SUFFIX + DOWN_SUFFIX + IMAGE_EXT;
			break;
	}
	return imagePath;
}

function getSectionForThisPage () {
	// see what section this page is in
	var pageUrl = document.location.href;
	var lastIndex = KNOWN_SECTIONS.length - 1;
	for ( var i=0; i<=lastIndex; i++) {
		var sectionName = KNOWN_SECTIONS[i];
		if (pageUrl.indexOf("/"+sectionName+"default.htm") != -1 ) {
			// found the section name in the URL; this page is in this section
			return sectionName;
		}
	}
	return "Root"; // if section not found, we must be at root (or in an unknown section)
}


