/* menu.js version 1.09*/

/* set to false when it is desired for 
the child menu of the top nav to show even 
when parent link is current page. */
var activeKillChild = true;

//If scripting this will hide the sideNav submenus
document.write("<style>#sideNav li ul {display:none;}</style>");

/***********************************
MenuMemory - This function is needed to 
style and deactivate anchors that link to 
the page that is currently loaded.
***********************************/
function MenuMemory(elementId, submenu) {	
	var root = document.getElementById(elementId);
	
	if(!root) {
		return;
	}

	var currentLi = root.getElementsByTagName("LI");

	var docUrl = clarifyURL(document.URL);

	for (var x = 0; x < currentLi.length; x++) {

		if (currentLi.item(x).getElementsByTagName("A").length>0) {			
			var linkUrl = clarifyURL(currentLi.item(x).getElementsByTagName("A").item(0).href);
			
			//alert("link: " +linkUrl+ " = docUrl: " +docUrl)
           	if(linkUrl == docUrl) {
           		currentLi.item(x).getElementsByTagName("A").item(0).className = "currentPage";
           		currentLi.item(x).getElementsByTagName("A").item(0).onclick = function() {return false;}				
				
				//show current page's submenu if it has one			
				if(submenu && currentLi.item(x).getElementsByTagName("UL").length>0) {
					displaySubMenu(currentLi.item(x), docUrl, true);
				} //deactivate drop-down if parent is current page.				
				else if (activeKillChild && currentLi.item(x).getElementsByTagName("UL").length>0) {
					currentLi.item(x).getElementsByTagName("UL").item(0).style.display = "none";
				}
				
				//This "if" section only affects the graphical navigation
				if(currentLi.item(x).getElementsByTagName("A").item(0).getElementsByTagName("IMG").length>0) {
					var changeImg = currentLi.item(x).getElementsByTagName("A").item(0).getElementsByTagName("IMG").item(0);
					changeImg.src = changeImg.src.replace(/\.png/,'On.png');
					//alert(changeImg.src);
				}
         	} 			  	                      
		}		

		//if on a submenu page this call allows the menu to display.
		if(submenu && currentLi.item(x).getElementsByTagName("UL").length>0) {
			displaySubMenu(currentLi.item(x), docUrl);
		}				
	}	
}

/***********************************
displaySubMenu - Is used to display 
submenu items used in side navigation.
***********************************/
function displaySubMenu(parentLi, docUrl, currentPage) {	
	var subMenu = parentLi.getElementsByTagName("UL");

	if(currentPage && parentLi.getElementsByTagName("UL").length>0) {
		subMenu.item(0).style.display = "block";
	}
	else {
		for (var i = 0; i < subMenu.length; i++) {
			for (var k = 0; k < subMenu.item(i).getElementsByTagName("A").length; k++) {
				var curLink = clarifyURL(subMenu.item(i).getElementsByTagName("A").item(k).href);
				if(curLink == docUrl) {
					subMenu.item(i).style.display = "block";						
				}
			}
		}
	}
}


/***********************************
sideNavHasChild - if there is a side 
nav we want the parent topNav to stay 
highlighted. This is a supporting 
function that is used by other 
functions.
***********************************/
function sideNavHasChild(currentUL) {
	var sideNav = document.getElementById("sideNav");
	var currentULHref = clarifyURL(currentUL.getElementsByTagName("A").item(0).href);


	//alert(currentULHref);
	var hasChild = false;
	if(sideNav) {		
		for( var i = 0; i < sideNav.getElementsByTagName("A").length; i++){	
			var curLink = clarifyURL(sideNav.getElementsByTagName("A").item(i).href);

			//alert("link: " +curLink+ " = href: " +currentULHref)
			if (curLink == currentULHref) {				
				//alert(currentUL.getElementsByTagName("A").item(i).href.replace(/\/default.*/,'/'));
				hasChild = true;
			}
		}
	}
	
	//alert(hasChild);
	return hasChild;
}


/***********************************
currentNavHasChild - This function tells 
us if there is a parent topNav item.
This is a supporting function that 
is used by other functions.
***********************************/
function currentNavHasChild(curList) {
	var hasChild = false;
	var docUrl = clarifyURL(document.URL);
	
	for(var x = 0; x < curList.item(0).getElementsByTagName("A").length ;x++) {
		var linkUrl = clarifyURL(curList.item(0).getElementsByTagName("A").item(x).href);

		if(docUrl == linkUrl){
			hasChild = true;
		}
	}
	return hasChild;
}




/***********************************
parentHighlight - This function 
highlights the parent if one of its 
child anchors is the current page.
The matching is url based and has 
not been tested on complex urls.
***********************************/
function parentHighlight(elementId) {
	var nav = document.getElementById(elementId);
	if(nav) {
		for( var i = 0; i < nav.getElementsByTagName("LI").length; i++){					
			var curUl = nav.getElementsByTagName("LI").item(i).getElementsByTagName("UL");
			
			//if there is not a submenu maybe there is a flyout menu. Ordered lists are use for flyout menus
			if(curUl.length == 0) {	
				curUl = nav.getElementsByTagName("LI").item(i).getElementsByTagName("OL");
			}


			if(curUl.length > 0 && nav.getElementsByTagName("LI").item(i).getElementsByTagName("A")) {
				var hasChild = false;

				if(elementId == "topNav") {
					
					hasChild = sideNavHasChild(curUl.item(0));
					if(!hasChild) {
						hasChild = currentNavHasChild(curUl); 	
					}		

					if(hasChild) {							
						nav.getElementsByTagName("LI").item(i).getElementsByTagName("A").item(0).style.borderBottom = "6px solid #009dd9";
					}
				}
				else if(elementId == "sideNav") {			
					hasChild = currentNavHasChild(curUl);		
					if(hasChild){
						nav.getElementsByTagName("LI").item(i).getElementsByTagName("A").item(0).className = "parentSelected";						
					}
				}		
	
			}
		}
	}
	
}



/******
clarifyURL(curUrl) - Sometimes separate 
urls that do not exactly match are still 
referencing the same page and various functions 
in this file need the following match and replace 
functions to recognize these different addresses 
as calling the same page.
******/
function clarifyURL(curUrl) {
	
	curUrl = curUrl.replace(/\/index.*/,'/');
	curUrl = curUrl.replace(/\/default.*/,'/');
	curUrl = curUrl.replace(/#.*/,'');
	curUrl = curUrl.replace(/\?.*/,'');

	return curUrl;
}

/******
hideMenu(parentMenuId, menuTagType) - Allows us to hide the menu whenever called. 
parentMenuId is the menu we are dealing with and menuTagType is the tag we used 
for the flyout/drop-down menu (usually UL or OL).
******/
function hideMenu(parentMenuId, menuTagType) {
		var root = document.getElementById(parentMenuId);
		var childMenu = false;

		if(root) {
			childMenu = root.getElementsByTagName(menuTagType);
		}
					
		if(childMenu && childMenu.length > 0) {			
			for(var i = 0; i < childMenu.length; i++) {		
				childMenu.item(i).className = "";
			}
		}
}

