
// 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) 

if (isModernBrowser)
{
   var menuHorizOffset = 70;
   var menuVertOffset = 20;
   var cssPath = '/main.css';
   var arMenus = new Array('mnu-17','mnu-16','mnu-15','mnu-14','mnu-12','mnu-10','mnu-8','mnu-9','mnu-18');
   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()
{
	// Set onclick event handler for anchor elements with ID value of '..mnu-...'
    var arAnchors = document.getElementsByTagName('a');
    for (var i = 0; i < arAnchors.length; i++)
		if ( arAnchors[i].id.indexOf('mnu') > -1)
		    arAnchors[i].onclick = menuClick;
}

function menuClick()
{
	
    // Click event handler for menu items.
    // Menu IDs in format 'mnu-n1-n2' where n1 is parent and n2 is next menu 
 
    // Extract parent & next menu ID values from the current ID
    var arIDs = this.id.split("-");  
	var parentID = "mnu-" + arIDs[1];
	var targetID = "mnu-" + arIDs[2];

    // Store the X & Y position of the current element to global variables
	var arXY = getPosXY(this);
	currentX = arXY[0];
	currentY = arXY[1];

    // Show or hide the next menu set accordingly

    oMenu = document.getElementById(targetID)
    if (arIDs[1] == 0) // it's a top-level menu
	    showHide(targetID, parentID, true); 
	else
	    showHide(targetID, parentID, false); 

    var oMenu = document.getElementById(targetID)
	oMenu.style.top = (currentY + menuVertOffset) + "px";
	oMenu.style.left = (currentX + menuHorizOffset) + "px";
}

function showHide(IDtoToggle, ParentID, blnIsTopLevelParent)
{
   // Shows or hides the current menu plus its immediate ancestors.
   // Maintains a CSV string of ancestor menus IDs to be kept visible

   var blnHasParent;

   if (blnIsTopLevelParent) 
   {
	   // top level menu, no parents
	   strAncestors = '';
	   blnHasParent = false;
   }
   else
   { 
	   blnHasParent = true;
	   // Add parent to ancestor list if it's not in there already
	   if  (strAncestors.indexOf(ParentID) == -1)
		   strAncestors += ParentID + ",";
       else
	   {
		  // Parent is already in the list, so truncate the list after that point
	      var posInString = strAncestors.indexOf(ParentID);
		  var lParent = ParentID.length + 1;  // include trailing comma
	      strAncestors = strAncestors.substr(0, posInString + lParent);
	   }
   }

   // Now traverse the array of menus, hiding or showing menus as appropriate
   for (var i = 0; i < arMenus.length; i++) 
   {
      // create an object variable for each menu item
	  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 = "none"; 
		   else
		       oMenu.style.display = "block"; 
	  }
	  else
	  {
		 // This is the ID of the next menu to show or hide
		 if (oMenu.style.display == "block")
		 {
		 	// If it's already visible, hide it (unless it's a top-level menu)
			 if (blnHasParent) 
				oMenu.style.display = "none";
		 }
		 else
		 {		
		    // It's currently hidden, so show it (setting X & Y relative to caller)
		    if (blnHasParent)
		    {
			   oMenu.style.top = (currentY + menuVertOffset) + "px";
			   oMenu.style.left = (currentX + menuHorizOffset) + "px";
		    }
		    oMenu.style.display = "block";
		 }
	  }
   }
}

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;
}

