/* Global Variables*/
var hideTimer=0, showTimer=0;
var lastObj=null;



function showLeftSubNav(e,obj) { 
	if (obj == lastObj) {
		clearTimeout(hideTimer);
		hideTimer=0;
	} else if (lastObj != null) {
		hideSM(lastObj);	
	}
   
	showSM(obj);
}

function hideLeftSubNav(e,obj) {
	
	if (window.event) {  /* IE hack*/
		current = obj;
		related = window.event.toElement;
	}
	else {
		current = e.currentTarget;
		related = e.relatedTarget;
	}	
	
	if (current != related && !contains(current, related)) {
		lastObj=obj;
	    hideTimer=setTimeout(function(){hideSM(obj)},100);
    }
	
	
 
}

function showSM(obj) {
	
	if (typeof obj.style == "undefined") return;
	
	if (obj.style.visibility == "visible") return;            // make sure you can scroll with in the menu, with out position change firing
	
	if (obj.getAttribute("elementY") == null) {
	   obj.setAttribute("elementY",getElementY(obj));   
	}			
	  
	var ulCoorY = obj.firstChild.offsetHeight + parseInt(obj.getAttribute("elementY"));
	var pageCoorY = viewportHeight() + getPageScrollY();
	
	if(pageCoorY < ulCoorY) {
		    
	        //check for if menu above browser view
	        if ( obj.getAttribute("elementY") - obj.firstChild.offsetHeight < getPageScrollY()) {
		        obj.style.top="-" + ((obj.firstChild.offsetHeight - obj.parentNode.offsetHeight) - (getPageScrollY() - (obj.getAttribute("elementY") - obj.firstChild.offsetHeight)))  + "px";
		        
	       }
	       else {  //check for if menu below browser view
		       obj.style.top="-" + (obj.firstChild.offsetHeight - obj.parentNode.offsetHeight) + "px";
	        }
		}
	else {
		obj.style.top="0px";
		}
		
	   
	obj.style.visibility="visible";
	obj.parentNode.className = "currentMenu nav-menu-item"; 	
}

function hideSM(obj) {
	if (typeof obj.style == "undefined") return;
	
	if (obj.style.visibility == "hidden") return; 
	 
	obj.style.visibility="hidden"; 
	obj.parentNode.className = "nav-menu-item";
	lastObj=null;
}

function showSubheaderMenu(id) { 

    var subMenu = document.getElementById("subheader-Submenu" + id);
    var primaryAnchor = document.getElementById("subheader-Submenu-item" + id);
    
    if(subMenu) {
       primaryAnchor.className = "subheader-Submenu-active"; 
       subMenu.className = "show-subMenu";   
       }
}

function hideSubheaderMenu(id) { 
    
    var subMenu = document.getElementById("subheader-Submenu" + id);
    var primaryAnchor = document.getElementById("subheader-Submenu-item" + id);
    
    if(subMenu) {
	    primaryAnchor.className = "subheader-Submenu-nonactive";  
	    subMenu.className = "hide-subMenu";  
    }
 
}

function contains(a, b) {

  /* Return true if node a contains node b. */
  
	if(b) {
	while (b.parentNode)
		if ((b = b.parentNode) == a)
			return true;
	}    
	
	return false;
}

function getElementY(obj) {
	if(isSafari() && obj.tagName=='TR') {
		obj=obj.firstChild;
	}
	var top=obj.offsetTop;
	var op=obj.offsetParent;
	while (obj.parentNode && document.body != obj.parentNode) {
		obj=obj.parentNode;
		if (!isNaN(obj.scrollTop)) {
			top -= obj.scrollTop;
		}
		if (op == obj) {
			if (isSafari() && obj.tagName=='TR') {
				top += obj.firstChild.offsetTop;
			} else {
				top += obj.offsetTop;
			}
			op=obj.offsetParent;
		}
	}
	return top;
}

function getPageScrollY() {
	return document.body.scrollTop || document.documentElement.scrollTop;
}

function isSafari() {
	return (navigator.userAgent.toLowerCase().indexOf("safari") != -1);	
}

function viewportHeight() {
return window.innerHeight || document.documentElement.clientHeight;
} 

function subNavHeight(id) {
	
	if (document.getElementById("subFrame-" + id).style.height == "undefined") return;
	
	document.getElementById("subFrame-" + id).height = document.getElementById("nav-submenu-ul-" + id).offsetHeight;
	

	
}