// Menu Javascript K Burton 17/12/99
// updated for netgem 6/4/04
// updated for firefox 20/2/05
// updated for IE7  8/10/07
// Checked for Firefox, netgem 8/10/07
// Checked for Opera 9.23 12/10/07
// Checked for Sarfai 3.03 12/10/07

var rgbMenuColour;
var rgbMenuBackground;
var rgbMenuActiveColour;
var rgbMenuActiveBackground;
var iTotalMenuItems;

var isNav = (navigator.appName.indexOf("Netscape") != -1);
var isNetgem = (navigator.platform.indexOf("Netgem") != -1);

var iMenuDelay = 200;
var bMenuActive = false;
var bMenuCancel = false;
var iActiveSubMenu = 0;
var iLayerNoTemp = 0;
var bIsReady = false;

// set up navigator to accept mouse clicks on document
function setUp(mColour,mBackground,aColour,aBackground,NoItems)
{
	rgbMenuColour = mColour;
	rgbMenuBackground = mBackground;
	rgbMenuActiveColour = aColour;
	rgbMenuActiveBackground = aBackground;
	iTotalMenuItems = NoItems;
	bIsReady = true;  // menu lays must have finished loading
	
	// netgem bug fix ( won't allow clear background to be set!)
	if (isNetgem)
		rgbMenuBackground = "#33BBBB";
			
	if (document.getElementById &&
         document.getElementById("M1") &&
         document.getElementById("M1").style)
   {
   		for (var i = 1; i <= iTotalMenuItems; i++)
		{
			document.getElementById('M'+i).style.backgroundColor = rgbMenuBackground;
			document.getElementById('M'+i).style.color = rgbMenuColour;
		}
   } else {
 	if (isNav) 
 	{

		document.captureEvents(Event.MOUSEUP);
		document.onmouseup = function (event) { ClearMenu(); };

	 }
	else
	{
		for (var ii = 1; ii <= iTotalMenuItems; ii++)
		{
			document.all['M'+ii].style.backgroundColor = rgbMenuBackground;
			document.all['M'+ii].style.color = rgbMenuColour;
		}
	}
	}
}

// get around bug in Navigator when resized
function Resized()
{
  if(isNav) window.location.reload();
}

// delay showing menu for .20 sec so that the menu isn't shown if the mouse is just passing by.
function ActivateMenu(theLayerNo)
{

    if(bIsReady)  // make sure menus have loaded
    {
	if(bMenuActive)
		showLayer(theLayerNo);
	else
	{
		iLayerNoTemp=theLayerNo; // theLayerNo goes out of scope
		bMenuCancel = false;
		setTimeout('showLayer(iLayerNoTemp)',iMenuDelay);
	}
    }
	//showLayer(theLayerNo); Why was this here?
}


function CancelMenu()
{
	if(!bMenuActive)	
	   bMenuCancel = true;
}

// clear any menu layers open from the screen
function ClearMenu()
{	

  if (bMenuActive == true)
  {
	hideLayer();
	bMenuActive = false;
	iActiveSubMenu = 0;
  }
  window.status="";
  return true;	
}


// Show the menu layer
function showLayer(theLayerNo)
{
  var theLayer = 'Layer' + theLayerNo;
  var theMenu = "M" + theLayerNo;
  var theXCoord = 12;
  var theYCoord = 0;
  var defaultParentHeight = 108; // for IE4 
  var iVerticalSpace = 2;
  var iOffsetWidthExtra = 9;
 
  if (isNetgem) // change menu position for netgem
      iVerticalSpace = 110;

	
  if(!bMenuCancel)
  {
	if(bMenuActive)
		hideLayer();

	if (document.getElementById &&
         document.getElementById(theLayer) &&
         document.getElementById(theLayer).style)
   {
		// calculate the x coord
		for(var i = 1; i < theLayerNo; i++)
		{
			theXCoord += (document.getElementById('M'+ i).offsetWidth+iOffsetWidthExtra);
		}

		// Calculate the Y coord
		theYCoord = document.getElementById(theMenu).parentNode.offsetTop ;

		if (theYCoord == 0)
			theYCoord = defaultParentHeight; 

		theYCoord += (document.getElementById(theMenu).offsetHeight + document.getElementById(theMenu).offsetTop + iVerticalSpace);

   		var theElement =	document.getElementById(theLayer);
		var theMenuElement = document.getElementById(theMenu);
		
		theMenuElement.style.backgroundColor = rgbMenuActiveBackground;
		theMenuElement.style.color = rgbMenuActiveColour;
		theElement.style.left = theXCoord;
		theElement.style.top = theYCoord;
	 	theElement.style.visibility = "visible";
	} 
	else 
	{
		if (isNav)
 		{
			document[theLayer].visibility="visible";
   			//document[theLayer].left = MouseX;
 		}
 		else
 		{
			document.all[theMenu].style.backgroundColor = rgbMenuActiveBackground;
			document.all[theMenu].style.color = rgbMenuActiveColour;

			// calculate the x coord
			for(var ii = 1; ii < theLayerNo; ii++)
			{
				theXCoord += (document.all['M'+ ii].offsetWidth+iOffsetWidthExtra);
			}

			// Calculate the Y coord
			theYCoord = document.all[theMenu].parentElement.offsetTop ;

			if (theYCoord == 0)
				theYCoord = defaultParentHeight; 

			theYCoord += (document.all[theMenu].offsetHeight + document.all[theMenu].offsetTop + iVerticalSpace);

	 		document.all[theLayer].style.visibility = "visible";
        	document.all[theLayer].style.left = theXCoord;
			document.all[theLayer].style.top = theYCoord;
		}
	}
	bMenuActive = true;
	iActiveSubMenu = theLayerNo;
   	window.status = "Click document background to clear menu";
  }
  return true;
}

// hide menu layers
function hideLayer()
{
 
    var theMenu  = 'M' + iActiveSubMenu;
    var theLayer = 'Layer' + iActiveSubMenu;
	
	if (document.getElementById &&
         document.getElementById(theLayer) &&
         document.getElementById(theLayer).style)
   {
 		var theElement =	document.getElementById(theLayer);
		var theMenuElement = document.getElementById(theMenu);
		
		if (theMenuElement)
		{		
			theMenuElement.style.backgroundColor = rgbMenuBackground;
			theMenuElement.style.color = rgbMenuColour;
		}
	 	theElement.style.visibility = "hidden";
	} else
	{

    	if (isNav)
    	{
			document[theLayer].visibility="hidden";
    	}
    	else
    	{
			document.all[theMenu].style.backgroundColor = rgbMenuBackground;
			document.all[theMenu].style.color =  rgbMenuColour;
 			document.all[theLayer].style.visibility="hidden";
    	}
	}
  return true;
}
