
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FILE LOCATION: /2008_templates/scripts/menu.js
DESCRIPTION: This script controls the behaviour of the left-side expand-collapse menu (#primaryMenu)
DATE of LAST EDIT: October 30, 2008 
CHANGE LOG: 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

// Preload menu bullet images
var oPreload = new Image();
var aImgSrc = new Array();
aImgSrc[0] = sSiteRoot + "2008_templates/images/icons/blueBullet.gif";
aImgSrc[1] = sSiteRoot + "2008_templates/images/backgrounds/leftColumn/arrowExpanded.gif";
aImgSrc[2] = sSiteRoot + "2008_templates/images/backgrounds/leftColumn/arrowExpandedSubMenu.gif";
aImgSrc[3] = sSiteRoot + "2008_templates/images/backgrounds/leftColumn/arrowCollapsed.gif";
aImgSrc[4] = sSiteRoot + "2008_templates/images/backgrounds/leftColumn/arrowCollapsedSubMenu.gif";
for (i in aImgSrc) {
	oPreload.src = aImgSrc[i];
}


var pageUrl = document.URL; // Gets page URL
//Remove any hash or querystring values from the current URL for comparison in the checkMenu function
if (pageUrl.indexOf("#")>0)
	pageUrl = pageUrl.substring(0,pageUrl.indexOf("#"));
if (pageUrl.indexOf("?")>0)
	pageUrl = pageUrl.substring(0,pageUrl.indexOf("?"));


//Functions to run after the window loads
window.onload = function() {
	initFontSize(); //Sets the font size
	checkMenu(pageUrl);	//Calls the menu checker function	
}


// Checks to see whether the page URL matches the URL for any of the #primaryMenu list items
var currentMenuItem;
function checkMenu(pageUrl) {
	// Get all link elements in #primaryMenu 
	var menulinks = document.getElementById("primaryMenu");
	menulinks = menulinks.getElementsByTagName("ul")[1];
	menulinks = menulinks.getElementsByTagName("a");
	
	showSubs(menulinks);
	
	// Go through all the links and check to see if they match the page name
	for (var i = 0; i < menulinks.length; i++) {
		if (menulinks[i].href == pageUrl) {			// The page matches a page in the menu
			var thismenu = menulinks[i].parentNode;
			setCurrentMenuItem(thismenu);
		}
	}
}


function setCurrentMenuItem(thismenu) {
    // Call menu expansion function, pass this menu and submenu variables
    if(currentMenuItem) {
      expandOrCollapseMenu(currentMenuItem,false);
    }
    currentMenuItem = thismenu;
	highlightCurrent(thismenu);	
	expandOrCollapseMenu(thismenu,true);
	
}


function expandOrCollapseMenu(thismenu,isExpand) {
	if(thismenu.getElementsByTagName("A")){
		if(isExpand) {
			thismenu.getElementsByTagName("A")[0].className += " expanded";
		} else {
			thismenu.getElementsByTagName("A")[0].className = "";
		}
	}
	var submenu = null;
	// Get submenu, if applicable
	if (thismenu.getElementsByTagName("ul")) {
		submenu = thismenu.getElementsByTagName("ul")[0];
	}

	// First, show the submenu (if there is any)
	if (submenu) {
		submenu.style.display = (isExpand?"block":"none");
	}
	
	// recursive calling expandMenu upwards
	if (thismenu.parentNode.parentNode.tagName == "LI") {
		expandOrCollapseMenu(thismenu.parentNode.parentNode,isExpand);
	}
}


// Adds a class to the menu items that have submenus associated with them
function showSubs(menu) {	
	for (var i = 0; i < menu.length; i++) {
		var inMenu = menu[i].parentNode.innerHTML.toLowerCase().indexOf("<ul>");
		if (inMenu != "-1") {
			menu[i].parentNode.className = "subMenu";
		}
	}
}


// Adds the class "current" to the currently selected page
function highlightCurrent(thismenu) {
	if(thismenu.getElementsByTagName("A")){
		thismenu.getElementsByTagName("A")[0].className = "current";
	}
}


// Handler when left menu clicked
function onMenuClick(e) {
	if (!e) {
		e = window.event;
	}
	var ele = e.srcElement;
	if (!ele) {
		ele = e.target;
	}
	if (!ele) return;
	if (ele.nodeName == "A" && (ele.href == window.location.href || ele.href == window.location.href + "#")) {
		e.cancelBubble = true;
		setCurrentMenuItem(ele.parentNode);
	}
}


// The following separates the default  #primaryMenu display value (none) from the main.css to enable
//	 full menu accesibility should JavaScript be disabled
document.write("<style type='text/css' media='screen'>");
document.write("#primaryMenu ul ul, #primaryMenu ul ul ul, #primaryMenu ul ul ul ul { display: none; }");
document.write("</style>");
