//**************************************************************************************************
//
// Name:		SubMenus
//
// Description:	Routines related to submenu processing.
//
// Notes:		Based on style sheet and coded downloaded from internet.
//
// Author:		
//
// Modified:	
//				Colin Finnis						23-Jan-2009
//
//**************************************************************************************************

var menuids=["vertSubUL1"]						// Enter id(s) of UL menus, separated by commas

//**************************************************************************************************
//
// Name:		buildSubMenus
//
// Description:
//				This functions is called when the pages is loaded to dynamically set up the sub
//				memu styles for the basic html definition of the required menu.
//
// Return:
//				none
//
// Parameters:
//          	none
//
// Inputs:
//				none
//
//**************************************************************************************************

function buildSubMenus()
{
	// Initial loop which allows for multiple menus
	
	for (var i=0; i<menuids.length; i++)
	{
		// Select all the ul elements within the element with the relevant ID i.e. select all
		// sub menus.
		
  		var ultags=document.getElementById(menuids[i]).getElementsByTagName("ul");
		
		// Loop through the selected ul elements (which are all sub menus at various levels)
		// and set up their parents style.
		
    	for (var t=0; t<ultags.length; t++)
		{
		
			// We have first level sub menu set up the primary level entry accordingly.
			
			if (ultags[t].parentNode.parentNode.id==menuids[i])
			{
				// This is the first level of sub menus set the relevant style .
				
    			ultags[t].parentNode.getElementsByTagName("a")[0].className="subStyle";			

				// If we have border subtract it from the offset width so that the sub menu sits
				// over the right border and tight against the menu entry.
				//
				// Note: There appears to be some sort of rendering problem. If an li has a
				//       border on the left the offset width is correctly set however the
				//       element is rendered short by the width of the left border.
					
				ultags[t].style.left = (Number(ultags[t].parentNode.offsetWidth) - (getParentLBdrWidth(ultags[t]) * 2)) + "px";
			}
			else
			{
				// We have further levels of sub menus set the relevant style in the parent.
				// Then set its offset position so the borders overlapp.
				
    			ultags[t].parentNode.getElementsByTagName("a")[0].className="subStyle2";
				ultags[t].style.left = (Number(ultags[t].parentNode.offsetWidth) - getParentLBdrWidth(ultags[t]) - 1) + "px";
			}
			// Now set up the action routines for for mouse over to make sub menu apppear
			// disappear.
			
    		ultags[t].parentNode.onmouseover=function()
			{
    			this.getElementsByTagName("ul")[0].style.display="block"
    		}
    		ultags[t].parentNode.onmouseout=function()
			{
    			this.getElementsByTagName("ul")[0].style.display="none"
    		}
    	}
		// Finally loop through all sub menus again and use "display:none" to hide menus
		// (to prevent possible page scrollbars
			
		for (var t=ultags.length-1; t>-1; t--)
		{
			ultags[t].style.visibility="visible";
			ultags[t].style.display="none";
		}
	}
}

//**************************************************************************************************
//
// Name:		getParentLBdrWidth
//
// Description:
//				This functions is called when the pages is loaded to dynamically set up the sub
//				memu styles for the basic html definition of the required menu.
//
// Return:
//				A numeric value equating to the left hand border width.
//
// Parameters:
//          	element	-	The child of the element with the border we need ascertain the width of.
//
// Inputs:
//				none
//
//**************************************************************************************************

function getParentLBdrWidth(element )
{
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		// For Internet Explorer we use current style from the parent node.

		var bdrWidth = element.parentNode.currentStyle.borderLeftWidth.replace('px', '');
	}
	else
	{
		// For all other browsers we use client left under offset parent which IE dosnt seem
		// interested in populating.
		
		var bdrWidth = element.offsetParent.clientLeft;
	}
	
	// Convert what we have to numeric form if not numeric return 0.
	
	if (isNaN(bdrWidth) == false)
	{
		bdrWidth = Number(bdrWidth);
	}
	else
	{
		bdrWidth = Number(0);
	}
	return bdrWidth;
}

//**************************************************************************************************
//
// Name:		Initialisation code
//
// Description:	Set up event handlers to initialise menus.
//
// Parameters:
//          	
// Inputs:
//
//**************************************************************************************************

if (window.addEventListener)
{
	window.addEventListener("load", buildSubMenus, false);
}
else if (window.attachEvent)
{
	window.attachEvent("onload", buildSubMenus);
}

