// search for monkey to know what font colour to change
var theTimer = 0;  // timer for main menu
var leftTimer = 0;  // timer for left menu
var subMenuStatus = 0;  // 0 = sub menu on. 1 = sub menu is off
var menuItemOnId = ""; // id of the currently selected menu item




var mainMenuTopOffset = 20;
var subMenuLeftOffset = 144;
var subMenuTopOffset = -1;

function deHighlightMenu(menu){
  try{ 
	menuSelected = "";
	if(menu==menuSelected){
	}
	else{
	     menuColor = "#236A0F"; // cant find where this shows up
         menuFontWeight = "strong"; // cant find where this shows up
		 document.getElementById(menu).style.color = menuColor;
		 
	}	
   }
   catch(e){
    alert("deHighlightMenu:" + e);
   }	 	 

}

// this function is from http://www.xs4all.nl/~ppk/js/findpos.html
function findPosX(obj)
{
        var curleft = 0;
        if (obj.offsetParent)
        {
                while (obj.offsetParent)
                {
                        curleft += obj.offsetLeft
                        obj = obj.offsetParent;
                }
        }
        else if (obj.x)
                curleft += obj.x;
        return curleft;
}
// this function is from http://www.xs4all.nl/~ppk/js/findpos.html
function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

function menuOn(menu){
    try{
       posX = findPosX(document.getElementById("a" + menu));
       posY = findPosY(document.getElementById("a" + menu));       
       document.getElementById(menu).style.left = posX + 'px';
       document.getElementById(menu).style.top = posY + mainMenuTopOffset + 'px';
       document.getElementById(menu).style.visibility='visible';
	   highlightMenu(menu);
	}
	catch(e){
	   alert(e + " menuOn() menu: " + menu);
	}      
}

function setSubMenuOn(){
   subMenuStatus = 1;  
}

function setSubMenuOff(){
   subMenuStatus = 0;
}

function menuItemOn(id){  

// get the dehighlight color
 menuSelected = "";
 deHighlightColor = "#0042EF";  // monkey hover (color once mouse taken away) change 
 if(menuItemOnId == menuSelected){
     deHighlightColor = "pink";  // not sure what this is for
 }	 
 
 // deselect last selected menu item if there is no sub menu showing
 if(menuItemOnId != "" && subMenuStatus == 0){
    document.getElementById("a" + menuItemOnId).style.color = deHighlightColor;
 }

 // highlight selected item
 document.getElementById("a" + id).style.color = "#666666";  //monkey hover
// document.getElementById("a" + id).style.background = "orange";  //active link colour 

 menuItemOnId = id;
     
}

function subMenuItemOn(id){
   document.getElementById("a" + id).style.color = "pink";//monkey not sure where this is
}

function subMenuItemOff(id){

   // get the dehighlight color
   menuSelected = "";
   deHighlightColor = "#cccccc";
   if(menuSelected == id){
     deHighlightColor = "#F71D30";
   }
   document.getElementById("a" + id).style.color = deHighlightColor;

}   

function menuItemOff(id){

}

function showSubMenu(menu){
   
   // hide last sub menu 
  // hideSubMenu("");

   
   try{
      // show submenu if there is on
      posLeft = findPosX(document.getElementById("a" + menu));
      posLeft += subMenuLeftOffset;
      posTop = findPosY(document.getElementById("a" + menu));
      posTop += subMenuTopOffset;      
      document.getElementById(menu + "Menu").style.left = posLeft + 'px';
      document.getElementById(menu + "Menu").style.top = posTop + 'px';;
      document.getElementById(menu + "Menu").style.visibility = "visible";

   }
   catch(e)
   {
      alert("showSubMenu:" + e + " menu:" + menu);
   }
 
    
}

function hideSubMenu(id){

  for(i=0;i < subMenus.length;i++){
    document.getElementById(subMenus[i]).style.visibility = "hidden";
  }

    // get the dehighlight color
   menuSelected = "";
   deHighlightColor = "#0042EF"; // monkey when first roll over link colour
   if(menuSelected == menuItemOnId){
     deHighlightColor = "#F71D30";
   }
   try{
      document.getElementById("a" + menuItemOnId).style.color = deHighlightColor;
   }
   catch(e){
    //  alert("hideSubMenu:" + e);
   }
}

function hideMainMenu(id){

  for(i=0;i < mainMenus.length;i++){
    document.getElementById(mainMenus[i]).style.visibility = "hidden";
  }
 
}

function subMenuOn(menu){
 							
  try{
	// unhide and highlight selected menu
	alert(menu);
    document.getElementById(menu).style.visibility='visible';
	subMenuStatus = 1;
   }
   catch(e){
     alert("subMenuOn" + e);
   }	
}

function subMenuOff(menu){
 	
	// unhide and highlight selected menu
    document.getElementById(menu + "Menu").style.visibility='hidden';
	deHighlight(document.getElementById("a" + menu));
	resetLeftSubMenuArrow();
			
}


function menuOff(){

    hideSubMenu("");
    hideMainMenu("");		
	
	subMenuStatus = 0;
	
}

function menuOffTimer(){
  try{
   clearTimer();
   theTimer = setTimeout("menuOff()",1000); 
   }
   catch(e){
      alert("menuOffTimer:" + e);
   }   
}

function clearTimer(){
 
   clearTimeout(theTimer);

}

function highlightMenu(menu){
  try{
    menuSelected = "";
	if(menu!=menuSelected){
	   document.getElementById(menu).style.color = "#000000";
	}
   }
   catch(e){
       alert("highlightMenu:" + e);
   }	
}

function highlight(obj){
   try{	
	obj.style.color = "#6FA395"; //no idea
   }
   catch(e){
    alert("highlight:" + e);
   }	
	// document.getElementById("td" + menu).style.backgroundColor = "blue";
}

function deHighlight(obj){
	if(subMenuStatus==0){
	   obj.style.color = "#D48E9D"; // no idea
	}   
	subMenuStatus = 0;
   	
}
