// 
// FILE LOCATION: /shared/scripts/menu.js
// DESCRIPTION: This script controls the behaviour of the left-side expand-collapse menu (#primaryMenu)
// DATE of LAST EDIT: June 20, 2008 
// CHANGE LOG: 
//
// 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")[0];
	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";
	}
}

window.onload = function () {
	var pageUrl = this.location;		// Gets page URL
	checkMenu(pageUrl);			// Calls menu checker function
}

// 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 { display: none; }");
document.write("</style>");
