
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FILE LOCATION: /_shared/scripts/menu.js
DESCRIPTION: This script controls the behaviour of the left-side expand-collapse menu (#primaryMenu)
DATE of LAST EDIT: January 25, 2010
CHANGE LOG: 
Dec. 1, 2009
	-added support for default file names; current page/menu item comparison now disregards any default file names in the URL
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

//Find the root folder of the current site and saves it to the sSiteRoot variable

//--==configuration==--
var sSupportFolder = "_shared"; //set the name of the template support folder
var iActiveMenu = 0; //set the child index number of the <ul> under #primaryMenu that contains the dynamic menu
var refCSSId = "globalCSS"; //set the id of the link tag for the global CSS file to use as a reference to find the site root

var sSiteRoot = new String();
setPath();
function setPath() {
	var sLinkHref = document.getElementById(refCSSId).getAttribute("href");
	var sPathToRoot = sLinkHref.substring(0,sLinkHref.indexOf(sSupportFolder));
	var sLocHref = location.href.substring(0,location.href.lastIndexOf("/")+1);
	sSiteRoot = sLocHref + sPathToRoot;
}

// Preload menu bullet images
var oPreload = new Image();
var aImgSrc = new Array();
aImgSrc[0] = sSiteRoot + sSupportFolder + "/images/icons/blueBullet.gif";
aImgSrc[1] = sSiteRoot + sSupportFolder + "/images/backgrounds/leftColumn/arrowExpanded.gif";
aImgSrc[2] = sSiteRoot + sSupportFolder + "/images/backgrounds/leftColumn/arrowExpandedSubMenu.gif";
aImgSrc[3] = sSiteRoot + sSupportFolder + "/images/backgrounds/leftColumn/arrowCollapsed.gif";
aImgSrc[4] = sSiteRoot + sSupportFolder + "/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;
var sMenuLinkTemp = new String();
function checkMenu(pageUrl) {
	// Get all link elements in #primaryMenu 
	var menulinks = document.getElementById("primaryMenu");
	menulinks = menulinks.getElementsByTagName("ul")[iActiveMenu];
	menulinks = menulinks.getElementsByTagName("a");
	
	showSubs(menulinks);
	
	//list of default files to strip from the URLs for comparison
	var aDefault = new Array("index.","default.","welcome."); 
	
	// Go through all the links and check to see if they match the page url
	for (var i = 0; i < menulinks.length; i++) {
		
		sMenuLinkTemp = menulinks[i].href;
		//alert(pageUrl + "; " + sMenuLinkTemp);
		//strip default file references from menu link and page URLs
		for (var x = 0; x < aDefault.length; x++) {
			if (sMenuLinkTemp.indexOf(aDefault[x])>0)
				sMenuLinkTemp = sMenuLinkTemp.substring(0,sMenuLinkTemp.indexOf(aDefault[x]));
			if (pageUrl.indexOf(aDefault[x])>0)
				pageUrl = pageUrl.substring(0,pageUrl.indexOf(aDefault[x]));			
		}

		if (sMenuLinkTemp == 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>");

