var hideInterval = null;
var hideObjects = new Array();

//  this function finds the first UL inside the LI control and sets the style to display none
hideSubMenu = function(objThis)
{			
    if (objThis.childNodes == null) return;
    
    var firstAtag = true;
    
    for(var i = 0; i < objThis.childNodes.length; i++)
    {
		if (objThis.childNodes.item(i).nodeName == "A" && firstAtag)
		{
			if (objThis.childNodes.item(i).className == "alevel2ParentSelected")
			{
				objThis.childNodes.item(i).className = "alevel2Parent";
			}
			
			firstAtag = false;
		}
		
        if(objThis.childNodes.item(i).nodeName == "UL")
        {				
            if (this.hideInterval == null)
            {
				hideObjects = [];
				hideInterval = window.setTimeout("doHide()", 300);
			}
            
            var hideUL = objThis.childNodes.item(i);
            
            this.hideObjects.push({element:hideUL});
            
            break;
        }			
    }	
}

//  this function finds the first UL inside the LI control and sets the style to display block
showSubMenu = function(objThis)
{
    if (hideInterval != null)
    {
		clearTimeout(hideInterval);
		hideInterval = null;
		doHide();
    }
    
    if (objThis.childNodes == null) return;
    
    var firstAtag = true
    
    for(var i = 0; i < objThis.childNodes.length; i++)
    {
		if (objThis.childNodes.item(i).nodeName == "A" && firstAtag)
		{
			if (objThis.childNodes.item(i).className == "alevel2Parent")
			{
				objThis.childNodes.item(i).className = "alevel2ParentSelected";
			}
			
			firstAtag = false;
		}
		
		if (objThis.childNodes.item(i).nodeName == "UL")			
        {
            objThis.childNodes.item(i).style.display = "block";
            break;
        }		
    }
    
    return;
}

doHide = function()
{

	var obj;
	
	for(var i = 0; i < hideObjects.length; i++)
	{
		obj = hideObjects[i];
		obj.element.style.display = "none";
	}
	
	hideInterval = null;
}

SetParentSelectedBackground = function(objId)
{
	var obj = document.getElementById(objId);
	
	if (obj == null) return;
	
	obj.className = "alevel2ParentSelected";
}


SetParentUnSelectedBackground = function(objId)
{
	var obj = document.getElementById(objId);
	
	if (obj == null) return;
	
	obj.className = "alevel2Parent";
}

// this function is called within each link to make sure the left root menu show as mouseover when list items are moused over
ImageSwap = function(imgId, src)
{
	//  make sure the browser can handle image swapping
	if (!document.images) return;
	
	var img = document.getElementById(imgId);

	if (img != null)
	{
		img.src = src;
	}
}


// This function is used to overcome the windows 'bug' that prevents
// DHTML (i.e the flyout menu) rendering in front of windows dropdown
// objects.  It works by inserting hidden iframes, that can render in 
// front of the dropdowns, behind the menu layer.
HiddenIframes = function() 
{
	var lineHeight = 24;
	
	var sElmID = "LeftNavContainer";
	
	
	var ULlist = document.getElementById(sElmID).getElementsByTagName('ul');
	var firstTime = true;
	
	//  comment out this block when sorting for firefox 
	var LIlist = document.getElementById(sElmID).getElementsByTagName('li');
	var startTop2 = 0;
	var startTop3 = 0;
	
	//  loop through all the LI tags. and set an absolute top within the UL.
	for (var i = 0; i < LIlist.length; i++)
	{ 
		var innerHref = LIlist[i].innerHTML.toString().substring(0,20); //  use this to get the firstChild 'a' className
		var innerImg = null;
		var startNewLevel3 = false; //  used to identify that any sublevel height will need to start from 0
		
		if (innerHref.indexOf("alevel2") > 0)
		{
			LIlist[i].style.top = startTop2 + "px";
			startTop2 += lineHeight;
			startNewLevel3 = true;
		}
		else if(innerHref.indexOf("alevel3") > 0)
		{
			LIlist[i].style.top = startTop3 + "px";
			startTop3 += lineHeight;
		}
		
		if (innerHref.indexOf("alevel1") > 0) //If it's a level 1 - i.e. new main menu item reset the top
		{
			startTop2 = 0;
			startTop3 = 0;
		}
		else if(startNewLevel3)
		{
			startTop3 = 0;
			startNewLevel3 = false;
		}
	}
	
	//  comment Block ends here
		
	//  loop UL's and add iframe
	for (j = 0; j < ULlist.length; j++)
	{
		var frameHeight2 = 0;
		var frameHeight3 = 0;
		
		if (!firstTime) //  the very first UL is the outer one and we need to work on the contents within
		{
			//  get the no. of a level2 tags within UL by doing a split on level2
			var level2Array = ULlist[j].innerHTML.toString().split("alevel2");
			var level3Array = ULlist[j].innerHTML.toString().split("alevel3");
			
			frameHeight2 = (level2Array.length - 1) * (lineHeight) + 1 -(level2Array.length) + level2Array.length;
			frameHeight3 = (level3Array.length - 1) * (lineHeight) + 1 -(level3Array.length) + level3Array.length;

			ULlist[j].innerHTML = ('<iframe class="LeftNavMenuIFrame" scrolling="no" src="about:blank" frameborder="0"></iframe>' + ULlist[j].innerHTML);

			var obj = ULlist[j].firstChild; //  the iframe		
			
			if (frameHeight2 > 1)
			{
			    obj.style.height = frameHeight2 + "px";
			}
			else
			{
			    obj.style.height = frameHeight3 + "px";
			}
			
			
		    ULlist[j].style.zIndex = "99";
		}
		else
		{
			firstTime = false;
		}
	}
}