/*
Copyright (c) 2003 Penang Adventist Hospital.
by Soo Yee Seong, MIS Department.
Email: sooyeeseong@pah.com.my
*/

// Constants
var sImgPath = "/_Library/flyout_menu/images/flyout_arrow.gif"
var sMenuArrowClass = "PahMenuItemArrow"
var iMenuTopMod = -2
var iMenuBottomMargin = 3
var iMenuHideDelay = 300

// Variables
var ePopupMenu
var iTimer

/*--- Menu Setup Functions ---*/

function showMenu(eObj, sSubMenuID) {
	if (!sSubMenuID) return // Exit if no submenu specified
	if (!document.getElementById(sSubMenuID)) return // Exit if submenu element not found
	if (eObj && ePopupMenu && ePopupMenu.id == sSubMenuID) return // Exit if submenu already opened
	
	/*--- Setup & display submenu ---*/
	var eSubMenu = document.getElementById(sSubMenuID)
	
	// Attach events to submenu
	eSubMenu.onmouseover = cancelMenuUnload;
	eSubMenu.onmouseout = unloadMenu;
	
	if (eObj) {
		// Keep track of current popup menu
		ePopupMenu = eSubMenu;
		
		// Set menu properties
		eSubMenu.style.display = 'block';
		eSubMenu.style.zIndex = eObj.style.zIndex + 1;
		
		// Set submenu top & left position
		eSubMenu.style.left = GetElementPosLeft(eObj) + eObj.offsetWidth;
		
		// Check submenu buttom position more than screen height
		if (GetElementPosTop(eObj) + eSubMenu.offsetHeight > document.body.clientHeight)
			eSubMenu.style.top = document.body.clientHeight - eSubMenu.offsetHeight - iMenuBottomMargin;
		else
			eSubMenu.style.top =  GetElementPosTop(eObj) + iMenuTopMod
		
		// Show menu
		eSubMenu.style.visibility = 'visible';
	}
	
	/*--- Setup submenu item ---*/
	eNodes = eSubMenu.getElementsByTagName('DIV')
	for (i=eNodes.length-1; i>=0; i--) {
		// Assign events for menu item
		eNodes[i].onmouseover = menuOver;
		eNodes[i].onmouseout = menuOut;
		
		// Create arrow IMG for each menu item
		eImgs = eNodes[i].getElementsByTagName('IMG')
		if (eImgs.length > 0) break
		
		eALinks = eNodes[i].getElementsByTagName('A')
		if (eALinks.length > 0) {
			var oImg = document.createElement("IMG")
				oImg.src = sImgPath;
				oImg.style.position = 'absolute';
				oImg.style.top = eNodes[i].offsetTop + 5
				oImg.style.left = eNodes[i].offsetWidth - 8
			
			if (!eALinks[0].rel)
				oImg.style.visibility = "hidden";
			
			eNodes[i].appendChild(oImg);
		}
	}
}

function hideMenu() {
	if (ePopupMenu) {
		ePopupMenu.style.visibility = 'hidden';
		ePopupMenu.style.dislay = 'none';
		ePopupMenu = null
	}
}

function unloadMenu() {
	cancelMenuUnload()
	iTimer = window.setTimeout('hideMenu()', iMenuHideDelay)
}

function cancelMenuUnload() {
	window.clearTimeout(iTimer)
}

/*--- Menu Event Functions ---*/

function menuOver() {
	if (this) {
		// Cancel any menu unload process
		cancelMenuUnload()
		
		// Get submenu id from MenuALink
		var eSubMenu = getALinkNode(this)
		var sSubMenuID = (eSubMenu)? eSubMenu.rel: null
		
		// Hide last opened menu
		if (ePopupMenu) {
			if ((ePopupMenu.id != this.offsetParent.id) // if opened-menu is not the same as current over-menu
			&& (ePopupMenu.id != sSubMenuID)) // if submenu is not already opened
				hideMenu()
		}
		
		// Open submenu if submenu id specified
		if (sSubMenuID) {
			showMenu(this, sSubMenuID);
		}
	}
}

function menuOut() {
	if (this)
		// Remove highlight menu item Div
		this.style.backgroundColor = '';
}

/*--- Supporting Functions ---*/

function getALinkNode(eDivNode) {
	var eALinks = eDivNode.getElementsByTagName('A');
	if (eALinks.length > 0)
		return eALinks[0]
}

function GetElementPosTop(eElement) {
	var iTop = 0
	while (eElement) {
		iTop += eElement.offsetTop
		eElement = eElement.offsetParent
	}
	return iTop
}

function GetElementPosLeft(eElement) {
	var iLeft = 0
	while (eElement) {
		iLeft += eElement.offsetLeft
		eElement = eElement.offsetParent
	}
	return iLeft
}