
var listenerDelay=0;		// milliseconds to check where the cursor move to
var hideMenuDelay = 0;
var childNodesStart =1;		// IE starts at 0 while firefox starts at 1
startList = function()
{
	var id=0;
	if (document.getElementById)
	{
		navRoot = document.getElementById("navPrimary");

		navRoot = navRoot.childNodes[childNodesStart]; // IE starts at 0

		for (i=0; i<navRoot.childNodes.length; i++)
		{
			node = navRoot.childNodes[i];
		
			if (node.nodeName=="LI")
			{
				// assign unique id per node
				node.id = i +1;

				// assign unique id per sub node
				subnavRoot = node.childNodes[2];
				if (subnavRoot != null){
					for (j=0; j<subnavRoot.childNodes.length; j++){
						if(subnavRoot.childNodes[j] !=null){
							subnavRoot.childNodes[j].id = "900" + i + ((node.id) + (j));
						}
					}
				}
				
				node.onmouseover=function()
				{
					//hide all menu 
					hideAllMenu();
					
					//show sub menu
					topNodeMouseOver(this.id);
  				}
				
  				node.onmouseout=function()
				{
					// hide this menu after delay reached
					setTimeout("hideMenuById(" + this.id + ",1,true);",hideMenuDelay);
				}
  			}
		}
 	}
}

// Top Menu mouse over behavior
function topNodeMouseOver(elementId){
	
		var obj = document.getElementById(elementId);
		
		var currentTopMenuNode = document.getElementById("currentTopMenuNode");
		currentTopMenuNode.value=obj.id;

		obj.parentNode.className+=" over";
		obj.className+=" over";
		
		
		//this is the javascript for the second level popup menu
		subnavRoot = obj.childNodes[2];
		
		if (subnavRoot != null)
		{
			for (j=0; j<subnavRoot.childNodes.length; j++)
			{
			
				subnode = subnavRoot.childNodes[j];
			
				
				if (subnode.nodeName=="LI")
				{
					subnode.onmouseover=function()
					{
						hide3rdLevelMenus();
						hideAllMenu();

	//					currentTopMenuNode.value=this.id;
						var currentSecondLevelNode  = document.getElementById("currentSecondLevelNode");
						currentSecondLevelNode.value=this.id;
						
	
						this.parentNode.className+=" over";
						this.className+=" over";
					}
					
					subnode.onmouseout=function()
					{
						var currentSecondLevelNode  = document.getElementById("currentSecondLevelNode");
						currentSecondLevelNode.value="";
						setTimeout("hideMenuById(" + this.id + ",2,true);",hideMenuDelay);
					}
					
				}
			}
		}
}

// Hide all menus
function hideAllMenu(){
	var currentSecondLevelNode  = document.getElementById("currentSecondLevelNode");
	var id=0;
	
	// hide all menus
	if ((document.getElementById) && (currentSecondLevelNode.value==""))
	{
		navRoot = document.getElementById("navPrimary");
		navRoot = navRoot.childNodes[childNodesStart]; // IE starts at 0

		for (i=0; i<navRoot.childNodes.length; i++)
		{
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI")
			{
					// second level
					if (node.className.indexOf("over") > -1) {
						node.className = node.className.replace("over","");
						node.parentNode.className= node.parentNode.className.replace("over","");
						node.parentNode.parentNode.className= node.parentNode.parentNode.className.replace("over","");
				    }

  			}
		}
	}
}

// Hide 3rd levels menus when switching in second level menu
function hide3rdLevelMenus(){
	var currentTopMenuNode= document.getElementById('currentTopMenuNode');
	var parentNode = document.getElementById(currentTopMenuNode.value);
	var subnavRoot = parentNode.childNodes[2];
	var childnode;
	if( subnavRoot !=null){
		for (j=0; j<subnavRoot.childNodes.length; j++){
			if(subnavRoot.childNodes[j] != null){
				childnode= subnavRoot.childNodes[j];
				if(childnode.childNodes.length > 2){
					childnode.parentNode.className = childnode.parentNode.className.replace("over","");
					childnode.className = childnode.className.replace("over","");
				}
			}
		}
	}
}


// Called by setTimeOut. Hides particular menu 
function hideMenuById(elementId,level, isTimeOut){
	
		var currentSecondLevelNode  = document.getElementById("currentSecondLevelNode");
		var obj = document.getElementById(elementId);
		// Get object to hide
		if (obj !=null ){

			// If there's delay, hide this object when there's no current second level node selected
			// otherwise, hide this menu.
			if (((isTimeOut==true) && (currentSecondLevelNode.value =="")) || (isTimeOut==false)){
				
				// set to default class name
				obj.className=obj.className.replace(" over", "");
				obj.parentNode.className=obj.className.replace(" over", "");
			
				// hide objects on the second level menus
				if (obj.childNodes.length >=2){
					subnavRoot = obj.childNodes[2];
					if (subnavRoot != null){
						// hide all sub nodes except the current selected sub node
						for (j=0; j<subnavRoot.childNodes.length; j++){
							
							// if subnode is not null
							if(subnavRoot.childNodes[j].className != null){
								// if not selected current item
								if (currentSecondLevelNode.value != subnavRoot.childNodes[j].id){
									// hide objects in the 3rd level
									subnavRoot.childNodes[j].className=subnavRoot.childNodes[j].className.replace(" over", "");
								}
							}
						}
					}
	
				}

			}

		}
}


window.onload=startList;