// global variables
activeNode = null;
activeLinkColor = "#F7941D"; //#31204A=darkblue / #F7941D"=orange / #00723F=green / #0055A5=lightblue
defaultLinkColor = "#31204A";

// expands menu where the link is found
function discoverDOM(elementID) {
	menuID = elementID;
	aNodeList = document.getElementById(elementID);
	if (!aNodeList.hasChildNodes()) { return false; } // kick out of the JS

	// recursive function(s) to follow the entire nav tree down each branch
	for (i=0; i < aNodeList.childNodes.length; i++) {
		if (aNodeList.childNodes[i].hasChildNodes()) findMoreChildren(aNodeList.childNodes[i])
	}
	// found the link for the page were on so were going to highlight it
	if(activeNode != null) {
		activeNode.style.color = activeLinkColor; // Change the active links color
		findParentNodes(activeNode); // find the tree to display the link
	}
}

// recursive function to find all children in the tree
// if we find a anchor link (#) in url we use our custom 
// url to compare with the link
function findMoreChildren(aNodeList) { 	
	for (var i=0; i < aNodeList.childNodes.length; i++) {
		var cNode = aNodeList.childNodes[i];
		if(cNode.href) {
			var cHref = unescape(cNode.href);

			// look for # (unicode 0023) or ? (unicode 003F) in href
			if(cHref.search(/\u0023/) != -1 || cHref.search(/\u003F/) != -1) { cUrl = location.href; }
			else { // custom url location to handle anchors
				cUrl = location.protocol +"//"+ location.hostname + location.pathname;
			} 
			
			//*** FIXES ***//
			// if http is not found in the nav link (herf="") we build out the string
			// fix for Apple Safari beta 2 - works fine everywhere else
			if (cHref.search(/http/) == -1) { 
				var tHref = location.protocol +"//"+ location.hostname + cHref;
				cHref = tHref;
			}

			// Fix for Opera 7.11+? -- append trailing "/" to the url when going to a folder only

			// search for trailing / and since we know opera klobbers the trailing slash, we going to pull 
			// it out of the cUrl so we get a good comparison
			// Should only need to run it once since the cUrl doesn't change until were on a new page
			if (isOperaBrowser) {
				var str = new String(cUrl);
				var mySlice = str.substring(str.length-1);
				if (mySlice == "/") {
					var nUrl = str.substring(0,str.length-1);
					cUrl = nUrl;
				}
			}

			// Checks to see if current Href (within nav) matches the current URL we are on
			// If so, we know what object to "activate" with the color change
			if(cHref == cUrl) { activeNode = cNode; } 
			//alert("cHref: "+cHref+" | cUrl: "+cUrl+" | activeNode: "+activeNode);
		}
		else if (cNode.hasChildNodes()) { findMoreChildren(cNode); }
	}
}

// we know where the link is so we use this function to find the 
// parentNodes back up to the top of the tree
function findParentNodes(activeNode) { 
	var parStr = "activeNode.parentNode";
	for (i=0; i < 10; i++) {
		var obj = parStr;
		var parent = eval(parStr+".id");
		document.getElementById(parent).style.display = 'block'; // display parent menu as we move up the tree
		if (parent != menuID) {
			// identifies top level navigation and applies css
			if (parent.search(/Title/) != -1) {
				activateMenu(parent);
				activeNode.style.color = defaultLinkColor; // Change the title color back to default
				var showMenu = parent.replace(/Title$/,"Menu");
				if (document.getElementById(showMenu)) {
					document.getElementById(showMenu).style.display = 'block';
				}
			}
			else if (parent.search(/Menu/) != -1 && parent.search(/Sub/) == -1) {
				// used when were in a sub nav; identifies top level nav 
				// then does a string replace so we can apply the css to the title
				var topLevelMenu = parent.replace(/Menu$/,"Title")
				activateMenu(topLevelMenu);
			}
			parStr += ".parentNode"; 
		}
		else { break; }
	}
}

// only used to active the current top level menu
function activateMenu(divID) { 
	document.getElementById(divID).className = "activeTitle"; 
}

// leave activeTitle class if set otherwise apply mouseover effects
function hoverOver(divID) {
	if (document.getElementById(divID).className == "activeTitle") { return; }
	else { document.getElementById(divID).className = "hoverOver"; }
}

// leave activeTitle class if set otherwise revert to normal
function hoverOff(divID) {
	if (document.getElementById(divID).className == "activeTitle") { return; }
	else { document.getElementById(divID).className = "navTitle"; }
}

// displays: none for the div's with "Menu" in their ID
function hideAll() {
	var allDivs = document.getElementsByTagName("div");
	for (i=0; i < allDivs.length; i++) {
		var currDiv = allDivs[i].id;
		if(currDiv != null) {
			if (currDiv.search(/Menu/) != -1) {
				document.getElementById(currDiv).style.display = "none";
			}
		}
	}
}

// initiates navigation (called on image load bottom of nav file)
function init() {
	isOperaBrowser = null; isOperaBreakLoop = null;
	if (document.layers || navigator.userAgent.indexOf("Opera") != -1) {
		if (navigator.userAgent.indexOf("Opera 7") != -1) { isOperaBrowser = true; }
		else { return; } // stops NS4.x & Opera 6 (allows Opera 7)
	}
	hideAll(); 
	discoverDOM("ilaNavigation");
}

// initiates EconDev navigation (called on image load bottom of nav file) -- runs after main init()
function initEconDev() {
	var divs = window.document.getElementsByTagName("div");
	for (var i=0; i < divs.length; i++) {
   	if (divs[i].className == "navMenu") {
         divs[i].style.display = "block";
      }
   } // end for
} // end function