//which id is highlighted if any
var highlighted = "";

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

// Detect if the browser is IE or not.
// If it is not IE, we assume that the browser is NS.
var IE = document.all?true:false

// Main function to retrieve mouse x-y pos.s

function getMouseXY(e) {
  if (IE) { // grab the x-y pos.s if browser is IE
    tempX = event.clientX + document.body.scrollLeft;
    tempY = event.clientY + document.body.scrollTop;
  } else {  // grab the x-y pos.s if browser is NS
    tempX = e.pageX;
    tempY = e.pageY;
  }  
  // catch possible negative values in NS4
  if (tempX < 0){tempX = 0}
  if (tempY < 0){tempY = 0}  
  // show the position values in the form named Show
  // in the text fields named MouseX and MouseY
  if(activeNode)
  {
	if(isMouseOut(tempX, tempY, activeNode))
	{
		if(getNodeType(activeNode) == "1")
		{
			swapCssClass(activeNode, false);			
			activeNode = null;
		}
		else if(getNodeType(activeNode) == "2")
		{
			hideNode(activeNode);
			activeNode = null;
		}
	}
  }
  return true;
}
document.onmousemove=getMouseXY;


	var activeNode;
	       
        //preload the images
        function preloadImages()
        {
			if (document.images)
			{
				pic1= new Image(169,29); 
				pic1.src="../images/selected.jpg"; 

				pic2= new Image(169,29); 
				pic2.src="../images/notselected.jpg"; 

			}
        }
        
        // This will swap the class of the element from <WHATEVER> to <WHATEVER>Selected 
        // or if the class is <WHATEVER>Selected, it'll make it <WHATEVER>
        // depending if you pass it true or false for the "on" variable
        function swapCssClass( oElement, on)
        {
			var className = oElement.className;
			var newClassName = className;
			var classNameLen = oElement.className.length;
			if((classNameLen >= 6) && highlighted == "")
			{
				if(oElement.className.substring(6).toLowerCase() == "highlighted")
				{
					highlighted = oElement.id;
				}
			}
			if(oElement.className.substr(0,6).toUpperCase() == "LEVEL1")
			{
				if(on)
				{
					if(oElement.className.substr(6, 4).toUpperCase() == "PLUS")
					{
						if(classNameLen < 16)
						{
							newClassName = className + "Selected";
						}
						if(!className.substr(className.length - 8, 8).toUpperCase() == "SELECTED")
						{
							newClassName = className + "Selected";
						}
					}
					else
					{
						newClassName = "Level1Selected";
					}
					/*if(className.length > 8)
					{
						if((className.substr(className.length - 8, className.length).toUpperCase() == "SELECTED"))
						{
							newClassName = className;
						}
					}*/
				}
				else
				{
					if(className.length > 8)
					{
						if((className.substr(className.length - 8, className.length).toUpperCase() == "SELECTED"))
						{
							newClassName = className.substr(0, className.length - 8);
						}
						if(highlighted != "")
						{
							if(oElement.id == highlighted)
								newClassName = newClassName + "Highlighted";
						}
					}
				}
				oElement.className = newClassName;
			}
        }

        //gets you the Y coordinate of the element
        //useful for using absolute positioning
        function getY( oElement )
        {
                var iReturnValue = 0;
                while( oElement != null ) {
                iReturnValue += oElement.offsetTop;
                oElement = oElement.offsetParent;
                }
                return iReturnValue;
        }

        //gets you the X coordinate of the element
        //useful for using absolute positioning
        function getX( oElement )
        {
                var iReturnValue = 0;
                while( oElement != null ) {
                iReturnValue += oElement.offsetLeft;
                oElement = oElement.offsetParent;
                }
                return iReturnValue;
        }


		//show the current node by making it "block viewable"
		//make sure the position is correct
        function showNode(parentElement, element)
        {
			activeNode = element;
			imageDiv = document.getElementById(parentElement.id + "i");
			
            element.style.display = "block";
            
            if(element.nodeName != "A")
            {
		            swapCssClass(parentElement, true);
		            
                    element.style.position = "absolute";
                    element.style.left = parentElement.offsetWidth + getX(parentElement) ;
                    element.style.top = (getY(parentElement)) - 1;
                    
                    imageDiv.style.left = -1;
                    imageDiv.style.top = -1;
            }
        }
        

        function hideNode(element)
        {
			var parentnode = document.getElementById(element.id.substr(0, element.id.length - 1));
            if((parentnode.className == "Level1Selected")||(parentnode.className.substr(6, 4).toUpperCase() == "PLUS"))
            {
				swapCssClass(parentnode, false);
			}
            
            if(element.nodeName == "DIV")
            {
                    element.style.display = "none";
            }
        }

	function getNodeType(elm)
	{
		var nodeType; //0 = menu (base level with submenu), 1 = base level w/o submenu, 2 = submenu 
		if((elm.id.substring(elm.id.length - 1)) == "t")
		{
			nodeType = 2;
		}
		else
		{
			if(document.getElementById(elm.id + "t"))
				nodeType = 0;
			else
				nodeType = 1;
		}
		return nodeType;	
	}

		function isMouseOut(xCoord, yCoord, elm)
		{
			var x = getX(elm);
			var y = getY(elm);
			var xEnd = elm.offsetWidth + x;
			var yEnd = elm.offsetHeight + y;
			var nodeType = getNodeType(elm);
			if(nodeType == 1)
			{
				if((xCoord < x)||(xCoord > xEnd)||(yCoord < y)||(yCoord > yEnd))
				{
					return true;
				}
			}
			else if(nodeType == 2)
			{
				var parentnode = document.getElementById(elm.id.substr(0, elm.id.length - 1));
				var parentX = getX(parentnode);
				var parentY = getY(parentnode);
				var parentXEnd = parentX + parentnode.offsetWidth;
				var parentYEnd = parentY + parentnode.offsetHeight;

				if(xCoord < x)
				{
					if((xCoord >= parentX) && (yCoord >= parentY) && (xCoord <= parentXEnd) && (yCoord <= parentYEnd))
					{
						return false;
					}
					else
					{
						return true;
					}
				}
				if((xCoord > xEnd)||(yCoord < y)||(yCoord > yEnd))
				{
					return true;
				}
			}
			return false;
		}
			
			
		
        function hideAllNodes()
        {
			if(activeNode)
			{
				if((activeNode.id.substring(activeNode.id.length - 1)) != "t")
				{
					swapCssClass(activeNode, false);			
				}
				else
					hideNode(activeNode);
			}
        }

		function getSubMenu(element)
		{
			var elm;
			hideAllNodes();
			elm = document.getElementById(element.id + "t");
			if(elm)
			{
				showNode(element, elm);
				activeNode = elm;			
			}
			else
			{
				swapCssClass(element, true);
				activeNode = element;			
			}
		}
        
        
