/* the menu javascript */
/* created 06-12-12    */
/* scripted by V Poile */
/* 
	Support:
	Currently supported in and tested on:
		Firefox 1.5 - 2.0, K-Meleon 1.0, Mozilla 1.7.13 (should theoretically work on all Gecko 1.8 Trunk Browsers)
		IE 6 - 7, (limited support in IE6 - everything works but on hover support doesnt exist so no color change)
		Opera 9, 
		Netscape Navigator 8.1.2,
*/

//css based constants... 
//menu div width
var menuDivW = 150;	//menu div width px
var bodyW = 760; 		//body width in px
var timeToWait = 4; 	//wait # seconds before closing menu in seconds

//attach a listener to window obj for resizing
	var paddingToLeft = 0;
function onLoaded() {
	//add on resize event listener
	window.onresize = calcPaddingToAdd;
	calcPaddingToAdd();
}
function calcPaddingToAdd(){
	if (!f) {var f = window.event; }
	
	var winWidth=document.body.clientWidth;
	winWidth = (winWidth < bodyW) ? bodyW : winWidth;
	var bodWidth=bodyW; // set this to the width of the body element
							// this would be the same as winWidth if % based template (%assumption%)
	paddingToLeft = ((winWidth-bodWidth) / 2); // only interested in width on left
	if(currentlyOpenMenu !=null){
		currentlyOpenMenu.style.visibility = 'hidden';
	}
}

function doSomething(e) {
	if (!e) {var e = window.event; }
	//element mouse just left
	var tg = (window.event) ? e.srcElement : e.target;
	//if target was not an LI then ignore and continue showing menu
	//if ie6 or earlier then do this:
	if(navigator.userAgent.match("MSIE 7") || navigator.userAgent.match("Gecko")) {
		//if ie7 or other browser do this
		if(tg.nodeName !='LI'){ return;}
	}
	else {
		if(tg.nodeName !='DIV'){ return;}
	}
	//otherwise continue to figure out if we should close the menu
	//element we have just entered
	var reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;
	if(reltg.nodeName == 'LI') { return;}
	if(reltg.nodeName == 'A') { return;}
	currentlyOpenMenu.style.visibility = 'hidden';
}

var currentlyOpenMenu;
function openmenu(callerID, menuID) {
	//position menu first 
	var callItem = document.getElementById(callerID);
	var menuItem = document.getElementById(menuID);
	currentlyOpenMenu = menuItem;
	var leftPos = callItem.offsetLeft;
	//need to add an 'if ie' statement here to correct the horizontal placement in ie systems
	var pos = leftPos+paddingToLeft;
		var rpos = 0;
		if(pos > ((bodyW+paddingToLeft)-menuDivW)) { 
			var rpos = pos+menuDivW;
			rpos = rpos-(bodyW+paddingToLeft);
		}
		menuItem.style.left=(leftPos+paddingToLeft-rpos)+'px';
	menuItem.onmouseout=doSomething;			//add onmouseout event listener
	menuItem.onmouseover=stopTimer;
	startCloseTimer(true);						//start count down timer to cause menu to close
	menuItem.style.visibility='visible';	//the open it
}

var timeTillAutoClose = 0;
var cTime;
function startCloseTimer(start){
	if(start ==true){
		timeTillAutoClose =timeToWait;
	}
	if(timeTillAutoClose == 0){
		currentlyOpenMenu.style.visibility = 'hidden';
		timeTillAutoClose = 0;
		clearTimeout(cTime);
		return;
	}
	if(timeTillAutoClose > 0){
		cTime = setTimeout('startCloseTimer(false)', 1000);
		timeTillAutoClose--;
	}else{ return;}
}

function stopTimer(){
	if(timeTillAutoClose > 0){
		timeTillAutoClose = 0;
		clearTimeout(cTime);
	}
}

function closeAllMenus(numberOfMenus){
	for(var i = 0; i< numberOfMenus; i++){
		//assuming that tnd01_div is the home link (not in dbh)
		document.getElementById('tnd0'+(i+2)+'_div').style.visibility='hidden';
	}
	if(timeTillAutoClose > 0){
		timeTillAutoClose = 0;
		clearTimeout(cTime);
	}	
}
