// Jason's Hovering Submenu Code
// 2006-10-06 NEW n-tier menu popup code
// 2006-11-13 Added auto-unpop code, no longer need to add onmouseout function to calling element
// 2008-05-15 Auto positioning of menus is now optional, update "jsmAutoPosition" to change. Allows for positioning via CSS where applicable

var jsmLastPop = new Array();
var jsmSrc = new Array();
var jsmHide = new Array();
var jsmTimeout = 200;

var jsmAutoPosition = false; // toggle auto positioning of menu elements
	
	// utility functions - jason
	function $(name) { return byId(name); }
	function get(name) { return byId(name); }
	function byId(name) { return document.getElementById(name); }
	function addClass(element, classname) { if(element != null) element.className += ' ' + classname; }
	function remClass(element, classname) { if(element != null) { while(element.className.indexOf(classname) != -1) { element.className = element.className.replace(classname, ""); } } }
	/* calculate the absolute position of element relative to all parent elements returns a string containing "left|top|width|height" */
	function getRealPosition(element) {	if(element != null) { var x = element.offsetLeft; var y = element.offsetTop; offset = element.offsetParent; while(offset.offsetParent) { x += offset.offsetLeft; y += offset.offsetTop; offset = offset.offsetParent; } var h = element.offsetHeight; var w = element.offsetWidth; } return (x + "|" + y + "|" + w + "|" + h); }

	function hover (src) { addClass(src, 'menu_hover'); }
	function unhover(src) { remClass(src, 'menu_hover'); }

	function pop(src, popup) {
		var max = jsmLastPop.length;
		var current = 0;
		// check if the current menu is a child of a previous menu
		for(i=0;i<max;i++) { if(src.parentNode == jsmLastPop[i]) { current = i+1; break; } }
		if(jsmLastPop[current] != null) { 
			jsmLastPop[current].style.display = 'none';
			//jsmLastPop[current].style.visibility = 'hidden';
			unhover(jsmSrc[current]);
		}

		jsmHide[current] = false;
		jsmLastPop[current] = get(popup);
		jsmLastPop[current].style.display = 'block';
		// Position menu relative to parent element - if positioning is enabled
		
		if(src.parentNode && jsmAutoPosition) {
			pos = getRealPosition(src).split("|");
			var x = pos[0]; y = pos[1]; w = pos[2]; h = pos[3];
			if (src.parentNode.className.indexOf("jsm-menu-inline") >= 0) {
			    if (src.parentNode.className.indexOf("jsm-menu-inline-up") >= 0) {
					var left = parseInt(x) + "px";
					var top = parseInt(y) - jsmLastPop[current].offsetHeight +"px";
	                jsmLastPop[current].style.left = left;
	                jsmLastPop[current].style.top = top;
			    } else {
					var left = parseInt(x) + "px";
					var top = parseInt(y) + parseInt(h)+"px";
	                jsmLastPop[current].style.left = left;
	                jsmLastPop[current].style.top = top;
				}
		    } else {
				var left = parseInt(w) + "px";
				var top = parseInt(y) - parseInt(h) + "px";
				jsmLastPop[current].style.left = left;
                jsmLastPop[current].style.marginTop = -parseInt(h)+"px";
		    }
		}
		
		jsmSrc[current] = src;
		src.onmouseout = function(){
		    unpop(src, popup);
		}
		hover(src);
	}
	
	function unpop(src, popup) {
		var max = jsmLastPop.length;
		var current = 0;
		// check if the current menu is a child of a previous menu
		for(i=0;i<max;i++) { if(src.parentNode == jsmLastPop[i]) { current = i+1; break; } }
		jsmHide[current] = true;
		setTimeout("hidePop('" + current + "');", jsmTimeout);
/* 		unhover(src); */
	}
	
	function hidePop(index) {
		if (jsmHide[index]) {
			if (jsmLastPop[index] != null) {
				jsmLastPop[index].style.display = 'none';
				//jsmLastPop[index].style.visibility = 'hidden';
			}
			unhover(jsmSrc[index]);
		}
	}
	
