
// JavaScript menu functions

// If it's a modern browser, write out the main CSS file and set up the menus 
// otherwise just the default basic CSS file will be used.

var isModernBrowser = (document.createElement && document.getElementsByTagName)
var blnPopped = false;

if (isModernBrowser) {
   var menuHorizOffset = 120;
   var menuVertOffset = 0;
   var cssPath = '/main.css';
   var arMenus = new Array('mnu-66','mnu-65','mnu-64','mnu-63','mnu-62','mnu-56','mnu-67','mnu-61','mnu-44','mnu-37','mnu-47','mnu-60','mnu-55','mnu-48','mnu-59','mnu-53');
   var strAncestors = ''; // CSV list of currently-opened menus
   var currentX = 0;      // X & Y coords of an object in pixels
   var currentY = 0;

   document.write ('<style type="text/css">@import url(' + cssPath + ');</style>');
   window.onload = initialiseMenus; 
}

function initialiseMenus() {

	// Sets event handlers for menus
    var arAnchors = document.getElementsByTagName('a');
    for (var i = 0; i < arAnchors.length; i++){
		// check it's a menu link
		if (arAnchors[i].id.indexOf('mnu-') > -1){
			arAnchors[i].onmouseover = menuLinkHover;
			arAnchors[i].onclick = linkClick;
	    }
		// it's a page link
	    if (arAnchors[i].id.indexOf('pg-') > -1){
			arAnchors[i].onmouseover = pageLinkHover;
		}
	}
	document.onclick = hideAllMenus;
	document.getElementById('menuholder').onmouseover = resetMenus;
}

function resetMenus() {

	// clears menus if mouse rolls over main main holder div

	if (blnPopped) {
		hideAllMenus();
	}
}

function menuLinkHover(e) {

    // Hover event handler for menu item links.

	stopBubble(e || window.event);

    var arIDs = this.id.split("-");  
	var parentID = "mnu-" + arIDs[1];
	var targetID = "mnu-" + arIDs[2];

	var arXY = getPosXY(this);
	currentX = arXY[0];
	currentY = arXY[1];

	hideAllMenus();
	showHide(targetID, parentID); 
}

function pageLinkHover(e) {

    // Hover event handler for page links.

	stopBubble(e || window.event);

    var arIDs = this.id.split("-");  
	// get parent menu group ID
	var parentID = "mnu-" + arIDs[1];
	
	var arAnchors = document.getElementsByTagName('a');
    for (var i = 0; i < arAnchors.length; i++){
		if (arAnchors[i].id.indexOf(parentID) > -1){
			showHide(arAnchors[i].id, parentID)
	    }
	}
		
	
}

function linkClick(e){

    // Click event handler for menu items.

	stopBubble(e || window.event);

    if (this.href) {  
		hideAllMenus();
	}
}

function showHide(IDtoToggle, ParentID) {

	// Shows or hides the current menu plus its immediate ancestors.
	// Maintains a CSV string of ancestor menus IDs to be kept visible

   //debugger

   blnPopped = true;

   if (ParentID == 'mnu-0') {
	   // top level menu, no parents
	   strAncestors = '';
   } 
   else { 
	   // Add parent to ancestor list if it's not in there already
	      strAncestors += ParentID + " ";
   }

	// Now traverse the array of menus, hiding or showing menus as appropriate
	for (var i = 0; i < arMenus.length; i++) {

		var oMenu = document.getElementById((arMenus[i])); 
		if ((oMenu.id != IDtoToggle)) {
			// This is not the next target menu but it might be an ancestor,
			// so show any ancestors and hide all the rest
			if (strAncestors.indexOf(arMenus[i]) > -1) {
				oMenu.style.display = "block"; 
			} 
			else {
				oMenu.style.display = "none"; 
			}
		} 
		else {
			// This is the ID of the next menu to show or hide
			oMenu.style.top = (currentY + menuVertOffset) + "px";
			oMenu.style.left = (currentX + menuHorizOffset) + "px";
			oMenu.style.display = "block";
		}
	}
}

function hideAllMenus() {
	for(var i = 0; i < arMenus.length; i++) {
		var oMenu = document.getElementById(arMenus[i]);
		oMenu.style.display = "none";
	}
	strAncestors='';	
}

function getPosXY(obj) {

	// Returns array of x & y position in pixels

	var curtop = 0;
	var curleft = 0;
    var arXY = new Array();

	while (obj.offsetParent) {
		curtop += obj.offsetTop;
		curleft += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	arXY[0] = curleft;
	arXY[1] = curtop;
	return arXY;
}

function stopBubble(e){
	
	// www.quirksmode.com. 
	// Stop event bubbling up the DOM

	if (e.stopPropagation) {
		// W3C browsers
		e.stopPropagation;
	}
	// MS browsers
	e.cancelBubble = true;
}

