/* hoverMenu - by Manuel Lamotte, mlamotte@gmx.de
 * copyright 2006, Naturheilpraxis Rotenbuehl
 * 
 * These functions are used to display a hover menu. 
 */

var timeBeforeShow = 200; 		// Milliseconds before showing the menu
var timeBeforeAutoHide = 0;	// Milliseconds to wait before auto hiding menu(1000 = 1 second)
// set (higher) time for auto hiding for IE6
if (navigator.userAgent.indexOf('MSIE 6.0')>=0 && navigator.userAgent.indexOf('Win')>=0) {
	timeBeforeAutoHide = 700;
}
var autoHideTimer = 0;		    // The auto hide timer.
var menuNode;
// detect IE browser
var browserIE = (navigator.userAgent.indexOf('MSIE')>=0 && navigator.userAgent.indexOf('Win')>=0 && navigator.userAgent.toLowerCase().indexOf('opera')<0)?true:false;

/*
 * Reset the auto hide timer.
 */
function stopAutoHide() {
	// Reset timer by set autoHideTimer to a negative value.
	autoHideTimer = -1;
}

/*
 * Start the timer to auto hide the menu.
 * This function will be called, if the mouse is outside
 * the hover menu.
 */
function initAutoHide() {
	// to start the timer set the variable autoHideTimer=0
	autoHideTimer = 0;
	if (autoHideTimer>=0) {
		autoHide();
	}
}

/*
 * Timer to call the hide menu function. In each loop
 * the timer will be increased until the timelimit is reached.
 */
function autoHide() {
	// timeBeforeAutoHide: time until menu disappears
	if (autoHideTimer>timeBeforeAutoHide) {
		
		// hide menu...
		hideMenu(menuNode);
	} else {
		// timelimit isn't reached, increase timer
		if(autoHideTimer>=0){
			autoHideTimer+=10;
			setTimeout('autoHide()', 10);
		}
	}
}	

/* 
 * Calculate top position for hover menu based
 * on the position of the parent menu.
 */
function getTopPos(inputObj) {		
 		var returnValue = inputObj.offsetTop;
 		
 		while((inputObj = inputObj.offsetParent) != null) {
 			returnValue += inputObj.offsetTop;
 		}
 		
 		return returnValue;
}

/* 
 * Calculate left position for hover menu based
 * on the position of the parent menu.
 */	
function getLeftPos(inputObj) {
 		var returnValue = inputObj.offsetLeft;
 		
 		while((inputObj = inputObj.offsetParent) != null) {
 			returnValue += inputObj.offsetLeft;
 		}
 		
 		// IE 7?
 		if (navigator.userAgent.indexOf("MSIE 7")) {
 			returnValue-=(document.body.clientWidth/2);
 			returnValue-=10;
 		} else {
 			// other browsers
 			returnValue-=(window.innerWidth/2)
 		}
 		// add half of content box width plus 10
 		// content box is 760
 		returnValue+=390;
 		
 		return returnValue;
}

/* 
 * Displays submenu with given id triggered by mouseover/mouseout event.
 */		
function showMenu(node) {
	// Look for subnode of type UL
	var sub  = getSubMenu(node);
	if (sub == null) {
		return;
	}
	sub.onmouseout = initAutoHide; // call initAutoHide on mouse out
	sub.onmousemove = stopAutoHide; // call stopAutoHide on mouse move on the link
	
	if (!sub || sub.style.display == 'block') {
		// reset timer
		stopAutoHide();
		
		// exit function because menu is already open
		return false;
	} else if (menuNode && sub != getSubMenu(menuNode)) {
		// if another menu is already open...
		// ... close the menu
		stopAutoHide();
		hideMenu(menuNode);
	}
	
	// save current menu node
	menuNode = node;
	
	// delay execution
	//delay(timeBeforeShow);
	
	sub.style.display='block';
	
	// set hover effect
	setHoverEffect(node, true);
	
	// position
	sub.style.top = getTopPos(node) + node.offsetHeight + 'px';
	sub.style.left = getLeftPos(node) + 'px';

	return false;
}

/*
 * Shows a hover effect. Necessary because of nested <a> elements.
 */
 function setHoverEffect(node, display, bgDefault) {
 	if (display) {
 		node.style.background='#990D17';
 		node.childNodes[0].style.color='#FFFFFF';
 	} else {
 		if (bgDefault!=null) {
 			node.style.background=bgDefault;
 		} else {
 			node.style.background='transparent';
 		}
 		node.childNodes[0].style.color='#965144';
 	}
 }
 
/*
 * Href to the link defined in the <a> node inside the given <li>.
 */
function doClick(node) {
	document.location.href=node.childNodes[0].href;
}

/* 
 * Closes the sub menu of the given node.
 */
function hideMenu(node) {
	
	// hide menu
	menuNode = false;
	if (node) {
		var sub = getSubMenu(node);
		sub.style.display = 'none';
		
		// hover effect
		setHoverEffect(node, false);
	}
	return false;
}

/*
 * Closes a still opened menu before the timeout does.
 */
function hideStillOpenMenu(node) {
	if (menuNode) {
		menuNode.style.background='transparent';
		menuNode.childNodes[0].style.color='#965144';
		var sub = getSubMenu(menuNode);
		sub.style.display = 'none';
		menuNode = false;
	}
}

/*
 * Gets subnode of type UL for given node.
 */
function getSubMenu(node) {
	var subNode = node.firstChild;
	
	while (subNode) {
		if (subNode.nodeName.toUpperCase() == 'UL') {
			// subnode of type UL found
			return subNode;
		}
		// read next node
		subNode = subNode.nextSibling;
	}
	// nothing found
	return null;
}

/* 
 * Delay execution for given milliseconds.
 */
function delay(msec) {
	
	var wait = parseInt(msec);
	var max  = 10000;
	if (wait == 'NaN' || wait < 0) {
		return false;
	}
	if (wait > max) {
		wait = max;
	}
	var start = new Date();
	var now   = new Date();;
	while (now.getTime() - wait < start.getTime()) {
		now = new Date();
	}
	return false;
}