var nav; // id of the complete navigation <ul id="nav">
var menus; // array of ids of all submenus (<ul>) within nav
var links; // array of ids of all list items (<li>, each containing <a>) within nav
var navStatus; // array of all currently-showing submenus
var navLevel; // index of the submenu containing the currently-rolled-over item, in the above array, or -1 if the mouse is not over the menu
var navTimeout;

function initNav() { // get complete structure of nav menu; assign ids to all submenus and event handlers to all links
 nav = document.getElementById("nav");
 menus = nav.getElementsByTagName("ul");
	links = nav.getElementsByTagName("li"); // each <li> contains a single link; this code uses the id of the <li>, not the <a> inside it
 navStatus = [nav];
 navLevel = -1;
 for (linkIndex=0; linkIndex<links.length; linkIndex++) {
		linkNode = links[linkIndex];
		linkNode.firstChild.onmouseover = function() {updateNav(this.parentNode);}
		linkNode.firstChild.onmouseout = function() {updateNav();}
	}
	hideOldMenus();
}

function updateNav(currentMenuNode) {
	if (currentMenuNode != undefined) { // rollover: show required submenus only
	 navStatus = []; // recalculate the menu status
		searchMenuNode = currentMenuNode.parentNode;
		navLevel = 0;
		while (searchMenuNode != nav) { // get the level of the submenu containing this link
			searchMenuNode = searchMenuNode.parentNode.parentNode; // parent nav submenu (<ul>), not parent html element (<li>); <ul> inside <li> inside <ul> = 1 nav level, hence .parentNode twice
			navLevel++;
		}
		submenuNode = undefined; // find the first <ul> child of this menu, if any, to show the submenu of the current rollover...
		for (childIndex=0; childIndex<currentMenuNode.childNodes.length; childIndex++) {
   if ((currentMenuNode.childNodes[childIndex].tagName == "ul") || (currentMenuNode.childNodes[childIndex].tagName == "UL")) {
				submenuNode = currentMenuNode.childNodes[childIndex];
				break;
			}
		}
		if (submenuNode != undefined) {
	  submenus = currentMenuNode.parentNode.getElementsByTagName("ul") // ...but first hide any other submenus coming off the current one, that might still be showing from before
		 for (submenuIndex=0; submenuIndex<submenus.length; submenuIndex++) {
		 	submenus[submenuIndex].style.left = "-999em";
	 	}
	 	navStatus.push(submenuNode);
	 	submenuNode.style.left = "auto";
		}
		var searchMenuNode = currentMenuNode.parentNode;
  while (searchMenuNode != nav) { // scan up through all the parent submenus of this submenu and show them.
   navStatus.push(searchMenuNode);
   searchMenuNode.style.left = "auto";
 		searchMenuNode = searchMenuNode.parentNode.parentNode; // parent nav submenu (<ul>), not parent html element (<li>); <ul> inside <li> inside <ul> = 1 nav level, hence .parentNode twice
 	}
		navStatus.push(nav);
		navStatus.reverse();
	 if (navTimeout != undefined) {
			window.clearTimeout(navTimeout);
	  navTimeout = window.setTimeout(hideOldMenus, 500);
		}
 } else { // rollout: assign timeout to hide all submenus no longer needed
	 navStatus = [nav]; // assume that mouse has left the nav content entirely - this data will be recalculated if the mouse then rolls over another link
		navLevel = 0;
	 if (navTimeout == undefined) {
	  navTimeout = window.setTimeout(hideOldMenus, 500);
		}
	}
}

function hideOldMenus() { // hide all submenus that aren't needed any more
 for (menuIndex=0; menuIndex<menus.length; menuIndex++) {
		menuNode = menus[menuIndex];
		searchMenuNode = menuNode;
		menuLevel = 0;
		while (searchMenuNode != nav) { // get the level of the submenu containing this link
			searchMenuNode = searchMenuNode.parentNode.parentNode; // parent nav submenu (<ul>), not parent html element (<li>); <ul> inside <li> inside <ul> = 1 nav level, hence .parentNode twice
			menuLevel++;
		}
  if ((menuLevel > 0) && (menuLevel > navLevel)) {
			keepThisMenu = false;
		 for (navIndex=menuLevel; navIndex<navStatus.length; navIndex++) {
				if (navStatus[navIndex] == menuNode) {
     keepThisMenu = true;
     break;
    }
			}
 	 if (! keepThisMenu) {menuNode.style.left = "-999em";}
		}
	}
	navTimeout = undefined;
}
