/* 

Halstrom: This JavaScript enhances the menu to allow the keyboard to tab through the popup menu items.
There is also a fix that repairs a CSS rendering bug in IE when the menu needs to
display on top of a <select> (dropdown list) element.

Compatibility:

Browsers            - Tested on IE6, Firefox 1.5, Opera 8.5.
CSS Disabled        - will degrade in an ADA-compliant fashion when CSS is disabled (all browsers).
JavaScript Disabled - Popup effect does not work without JavaScript, however links still work so the entire menu 
                      heirarchy is available (as long as submenus are enabled).

*/

KeyHandler = function() 
{
	var mcEls = document.getElementById("MainMenuWrapper").getElementsByTagName("A");
	for (var i=0; i<mcEls.length; i++) 
	{
		mcEls[i].onfocus=function() 
		{
		    // ensure last mouse-hovered menu item is cleared
			if(lastMouseItem!=null) { lastMouseItem.className = lastMouseItem.className.replace(new RegExp("( ?|^)MenuHover\\b"), ""); }
			lastKeyItem = this.parentNode;
			
			this.parentNode.className+=(this.parentNode.className.length>0? " ": "") + "MenuHover"; //li < a:focus
			if(this.parentNode.parentNode.parentNode.nodeName == "LI") 
			{
			    lastKeySubItem = this.parentNode.parentNode.parentNode;
			    
				this.parentNode.parentNode.parentNode.className+=(this.parentNode.parentNode.parentNode.className.length>0? " ": "") + "MenuHover"; //li < ul < li < a:focus
				HideSelects(this.parentNode.parentNode.parentNode);
			}
			else
			{
			    HideSelects(this.parentNode);
			}					
		}
		
		mcEls[i].onblur=function() 
		{
		    this.parentNode.className=this.parentNode.className.replace(new RegExp("( ?|^)MenuHover\\b"), "");
			if(this.parentNode.parentNode.parentNode.nodeName == "LI") 
			{
				this.parentNode.parentNode.parentNode.className=this.parentNode.parentNode.parentNode.className.replace(new RegExp("( ?|^)MenuHover\\b"), "");				
			}
			ShowSelects();
		}
	}
	return true;
}

MouseHandler = function() 
{
    var rootItems = document.getElementById("MainMenuWrapper").getElementsByTagName("LI");

    for (var i=0; i<rootItems.length; i++)
    {
        rootItems[i].onmouseover=function()
        {
            // ensure last keyboard-focused menu item is cleared
            if(lastKeyItem!=null)    { lastKeyItem.className = lastKeyItem.className.replace(new RegExp("( ?|^)MenuHover\\b"), "");  document.getElementById("MainMenuWrapper").focus(); lastKeyItem=null;}
            if(lastKeySubItem!=null) { lastKeySubItem.className = lastKeySubItem.className.replace(new RegExp("( ?|^)MenuHover\\b"), ""); lastKeySubItem=null; }
            lastMouseItem = this;
            
            this.className += " MenuHover";
            HideSelects(this);
        }

        rootItems[i].onmouseout=function()
        {
            this.className = this.className.replace(new RegExp("( ?|^)MenuHover\\b"), "");
            ShowSelects();
        }
    }
    return true;
}

function ShowSelects()
{
    if(!hideSelects) { return true; }
    
    sel = document.getElementsByTagName("SELECT");
    for (var i=0; i<sel.length; i++) 
    {
        sel[i].style.visibility = 'visible'; 
    }
    return true;
}

function HideSelects(rootItem)
{
    if(!hideSelects) { return true; }
    
    if(rootItem.parentNode.parentNode.id=="MainMenuWrapper")
    {
        sel = document.getElementsByTagName("SELECT");
        submenu = rootItem.getElementsByTagName("UL");

        if(submenu.length && sel.length)
        {
            for (var i=0; i<sel.length; i++)
            {
	            sel[i].style.visibility = ElementsOverlap(sel[i], submenu[0]) ? 'hidden' : 'visible';
            }
        }
	}       
    return true;
}

function ElementsOverlap(obj, eMenu)
{
 	var topObj = parseInt(ElementTop(obj));
	var leftObj = parseInt(ElementLeft(obj));
	var topMenu = parseInt(ElementTop(eMenu));
	var leftMenu = parseInt(ElementLeft(eMenu));	

	if (topObj > (topMenu + parseInt(eMenu.offsetHeight)))
		//if element is below bottom of panel then do nothing
		return false;

	if (leftObj > (leftMenu + parseInt(eMenu.offsetWidth)))
		//if element is to the right of panel then do nothing
		return false;

	if ((leftObj + parseInt(obj.offsetWidth)) < leftMenu)
		//if element is to the left of panel then do nothing
		return false;

	if ((topObj + parseInt(obj.offsetHeight)) < topMenu)
		//if element is to the top of panel then do nothing
		return false;

	return true;	
}

function ElementTop(eSrc)
{
	var iTop = 0;
	var eParent;
	eParent = eSrc;
	while (eParent.tagName.toUpperCase() != "BODY")
	{
		iTop += eParent.offsetTop;
		eParent = eParent.offsetParent;

		if (eParent == null)
			break;
	}
	return iTop;
}

function ElementLeft(eSrc)
{	
	var iLeft = 0;
	var eParent;
	eParent = eSrc;
	while (eParent.tagName.toUpperCase() != "BODY")
	{
		iLeft += eParent.offsetLeft;
		eParent = eParent.offsetParent;
		if (eParent == null)
			break;
	}
	return iLeft;
}

var hideSelects = false;
var lastMouseItem = null;
var lastKeyItem = null;
var lastKeySubItem = null;

// Browser Detection Script:
// - Determines the correct way to attach the JavaScript enchancements
// - Sets a flag to enable the <select> CSS fix for IE

if(window.addEventListener)
{
    // gecko, safari, konqueror and standard
    window.addEventListener('load', MouseHandler, false);
    window.addEventListener('load', KeyHandler, false); 
}
else if(document.addEventListener)
{
    // opera 7
    document.addEventListener('load', MouseHandler, false); 
    document.addEventListener('load', KeyHandler, false); 
}
else if(window.attachEvent) 
{ 
    // win/ie
    hideSelects = true;
    window.attachEvent('onload', MouseHandler);
	window.attachEvent('onload', KeyHandler);
} 
else
{ 
    // mac/ie5
	if(typeof window.onload == 'function')
	{
		var existing = onload;
		window.onload = function() {
			existing();
			hideSelects = true;
			MouseHandler();
			KeyHandler();
		}
	} 
	else 
	{
        window.onload = function() { 
            hideSelects = true;   
            MouseHandler();
            KeyHandler();
		}
	}
}