// web_menu.js
// Author Harvey Starks

var currentSubMenuId = '';			// The currently displayed sub menu.
var webMenuIframeId = 'webMenuIframe';		// The id of the iframe to use.
var menuBackgroundColor = '#202060';		// The menu background color.
var menuTextColor = '#ffcc00';			// The menu text color.
var menuHighlightColor = '#ffcc00';		// The menu background highlight color.
var menuTextHighlightColor = '#202060'; 	// The menu text highlight color.
var isIECompatibleBrowser = true;		// Is the client IE compatible

// Function menberitemMouseOver is the onmouseover event handler 
// for the menu bar items. It highlights the menubar item and handles figuring 
// out the correct position and submenu (menuitemgroup) to display.
// Then calls the showSubMenu() function. If another submenu is diplayed 
// it is closed by calling the hideSubMenu() function. If the submenu is 
// already displayed the showSubMenu() method is not called. 
function menubaritemMouseOver(menubarItem, hasSubmenu) {
	var menuItemGroupLeft;
	var menuItemGroupTop;
	var menuItemChildren;
 
	isIECompatibleBrowser = isBrowserIECompatible();

	// Highlight the menubar item and get it's left and bottom 
	// values for displaying the submenu.
	menubarItem.style.backgroundColor = menuHighlightColor;



	if (isIECompatibleBrowser) {
		menuItemChildren = menubarItem.children;
		menuItemChildren[0].style.color = menuTextHighlightColor;
		menuItemGroupLeft = menubarItem.getBoundingClientRect().left - 3;
		menuItemGroupTop = menubarItem.parentElement.getBoundingClientRect().bottom
			+ document.body.scrollTop - 2;
	} else {
		menuItemGroupLeft = getNetscapePosX(menubarItem) - 1;
		menuItemGroupTop = getNetscapePosY(menubarItem) + menubarItem.parentNode.clientHeight;
		menuItemChildren = menubarItem.childNodes;
		menuItemChildren[1].style.color = menuTextHighlightColor;
		
	}
	
	subMenuId = 'sub' + new String(menubarItem.id).substring(3);

	// If the correct (or no) submenu is displayed don't hide it.
	if ('' != currentSubMenuId && subMenuId != currentSubMenuId) {
		hideSubMenu(currentSubMenuId);
	}

	// If menubarItem has a sub menu but it's already displayed
	// don't re-display it. Otherwise display it.
	if(hasSubmenu == 'true' && subMenuId != currentSubMenuId ) {
		// Get the submenu id and set it's attributes.
		showSubMenu(subMenuId, menuItemGroupTop, menuItemGroupLeft);
	}
	return true;
}

// Function menubaritemMouseOut is the onmouseout event handler for 
// the menu bar items. If the mouse exits the menu bar item anywhere 
// but along the bottom the current sub menu is closed. (This will be the 
// one for the menu bar item firing the event.)
function menubaritemMouseOut(menubarItem, hasSubMenu, firedEvent) {
	var menuItemChildren;

	// See if we exited the menubar item at the bottom. 
	
	if (isIECompatibleBrowser) {
		eventY = window.event.clientY;
		menubarBottom = menubarItem.parentElement.getBoundingClientRect().bottom - 2;
		window.event.cancelBubble = true;
	} else {
		eventY = firedEvent.pageY;
		menubarBottom = menubarItem.parentNode.clientTop +
			menubarItem.parentNode.clientHeight - 2;
		firedEvent.cancelBubble = true;
	}
	
	// If we didn't exit at the bottom hide the submenu.
	if (eventY < menubarBottom && hasSubMenu) {
		hideSubMenu(currentSubMenuId);
	}

	menubarItem.style.backgroundColor = menuBackgroundColor;


	if (isIECompatibleBrowser) {
		menuItemChildren = menubarItem.children;
		menuItemChildren[0].style.color = menuTextColor;
	} else {
		menuItemChildren = menubarItem.childNodes;
		menuItemChildren[1].style.color = menuTextColor;
	}

	return true;
}

// Function menuitemMouseOver is the onmouseover event handler for 
// sub menu items. It sets the background color to the highlight color.
function menuitemMouseOver(menuItem) {
	var menuItemChildren;

	menuItem.style.backgroundColor = menuHighlightColor;

	if (isIECompatibleBrowser) {
		menuItemChildren = menuItem.children;
		menuItemChildren[0].style.color = menuTextHighlightColor;
	} else {
		menuItemChildren = menuItem.childNodes;
		menuItemChildren[1].style.color = menuTextHighlightColor;
	}
	return true;
}

// Function menuitemMouseOut is the onmouseout event handler for 
// sub menu items. It sets the background color to the non-highlighted 
// background color.
function menuitemMouseOut(menuItem) {
	var menuItemChildren;

	menuItem.style.backgroundColor = menuBackgroundColor;

	if (isIECompatibleBrowser) {
		menuItemChildren = menuItem.children;
		menuItemChildren[0].style.color = menuTextColor;
	} else {
		menuItemChildren = menuItem.childNodes;
		menuItemChildren[1].style.color = menuTextColor;
	}
	return true;
}

// Function menuItemGroupMouseLeave is the onmouseleave event handler for 
// menu items groups (whole submenu). It hides the submenu (menuitemgroup)
// firedEvent is only valid with firefox browser 1.5+ 
function menuItemGroupMouseOut(menuItemGroup, firedEvent) {
	var tempLeft = parseInt(menuItemGroup.style.left) + 1;
	var tempTop = parseInt(menuItemGroup.style.top);
	var tempRight; 
	var tempBottom;
	var pointerX;
	var pointerY;
	var tempEvent;
	var dsocLeft;
	var dsocTop;

	if (isIECompatibleBrowser) {
		tempEvent = window.event;
		dsocLeft = document.documentElement.scrollLeft;
		dsocTop = document.documentElement.scrollTop;
	} else {
		tempEvent = firedEvent;
		dsocLeft = window.scrollX;
		dsocTop = window.scrollY;
	}
	
	tempRight = menuItemGroup.clientWidth + tempLeft - 1; 
	tempBottom = menuItemGroup.clientHeight + tempTop - 1;
	pointerX = tempEvent.clientX + dsocLeft;
	pointerY = tempEvent.clientY + dsocTop;
	tempEvent.cancelBubble = true;

	if (pointerX <= tempLeft || pointerX >= tempRight 
			|| pointerY <= tempTop || pointerY >= tempBottom) {
		hideSubMenu(menuItemGroup.id);
	}
	
	return true;
}

// Function showSubMenu is the method used for displaying the sub menus.
// (menuitemgroups) The proper positions and sizes are set on the the 
// sub menu and iframe. The z-index of the sub menu is set to 1 above 
// the iframe so that the menu will be drawn on top of the iframe. The 
// iframe and sub menu are then made visible and the currentSubMenuId 
// is populated with the id of the sub menu being displayed.
function showSubMenu(subMenuId, menuItemGroupTop, menuItemGroupLeft) {

	// Get object references to the sub menu and iframe
	var subMenu = document.getElementById(subMenuId);
	var menuIframe = document.getElementById(webMenuIframeId);

	// Setup Iframe and menu positions and z-index of the menu 
	// item group. 
		subMenu.style.left = (menuItemGroupLeft + "px");
		subMenu.style.top = (menuItemGroupTop + "px");
	if (isIECompatibleBrowser) {
		var dsocLeft = document.documentElement.scrollLeft;
		var dsocTop = document.documentElement.scrollTop;
		subMenu.style.left = (parseInt(subMenu.style.left) + dsocLeft + "px");
		subMenu.style.top = (parseInt(subMenu.style.top) + dsocTop + "px");
		menuIframe.style.width = subMenu.offsetWidth;
		menuIframe.style.height = subMenu.offsetHeight;
	} else {
		menuIframe.style.width = ((subMenu.offsetWidth - 4) + "px");
		menuIframe.style.height = ((subMenu.offsetHeight - 4) + "px");
	}

	menuIframe.style.left = subMenu.style.left;
	menuIframe.style.top = subMenu.style.top;
	subMenu.style.zIndex = menuIframe.style.zIndex + 1; 
	
	// Display the submenu and set currentSubMenuId.
	menuIframe.style.visibility = 'visible';
	subMenu.style.visibility = 'visible';
	currentSubMenuId = subMenuId;
}

// Function hideSubMenu is the method used for hiding the sub menus.
// After the sub menu is hidden, the currentSubMenuId is set to ''.
function hideSubMenu(menuItemGroupId) {
	if ('' != menuItemGroupId) {
	
		// Get object references to the sub menu and iframe
		submenu = document.getElementById(menuItemGroupId);
		menuIframe = document.getElementById(webMenuIframeId);
		
		// hide the submenu and iframe then set currentSubMenuId to ''.
		menuIframe.style.visibility = 'hidden';
		submenu.style.visibility = 'hidden';
		currentSubMenuId = '';
	}
}

// Function isBrowserIECompatible determines whether or not the browser is 
// Microsoft Ineternet Explorer compatible. If so returns true, otherwise false.
function isBrowserIECompatible() {
	if ('Microsoft Internet Explorer' == window.navigator.appName) {
		return true;
	}

	return false;
}

// Function calculates the absolute X value of an element
// for Netscape compatible browsers.
function getNetscapePosX(obj) {
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

// Function calculates the absolute Y value of an element
// for Netscape compatible browsers.
function getNetscapePosY(obj) {
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}
