/* 
PROJECT-SPECIFIC CONFIGURATION:
Add one line to showMenu function for each menu to be used, using the following syntax:

	setElementProperty('menunameMenu', 'display', 'none');

To avoid menus overlapping right side of window, change "190" in the WIDTH CHECK 
portion of the showMenu function (twice) to equal the width of your drop-down menus
*/

var timer = "";
var openMenu = null;
var hoverImg = null;			// added for image hover trigger
var canMenuBeClosed = true;

function clearTimer(){
	window.clearTimeout(timer);
}

function tryToClose(){
   	if (hoverImg != null) { 
		imageOff(hoverImg);
		hoverImg = null;
	}
	var funcToCall = "canMenuBeClosed = true"
	timer = window.setTimeout(funcToCall, 400);
}

function showMenu(id, objPos){
	/* IMAGE SWITCH:
	this section added to change menu trigger image to 'off' state if moving
	directly from one image to the next 
	*/
   	if (hoverImg != null) { 
		// window.alert('image is ' + hoverImg); 
		imageOff(hoverImg);
		hoverImg = null;
	}
	/* end IMAGE SWITCH addition */
	
	if(document.getElementById){
		clearTimer();
		setElementProperty('solutionsMenu', 'display', 'none');
		setElementProperty('productsMenu', 'display', 'none');
		setElementProperty('marketsMenu', 'display', 'none');
		setElementProperty('servicesMenu', 'display', 'none');
		setElementProperty('resourcesMenu', 'display', 'none');
		setElementProperty('companyMenu', 'display', 'none');
		setElementProperty('investorMenu', 'display', 'none');
		
		openMenu = id + 'Menu';
		hoverImg = id + 'Img';
		canMenuBeClosed = false

		var x = 0;
		var y = 0;

		var agent = navigator.userAgent.toLowerCase();
		if (agent.indexOf("msie") != -1) {
			var idxIE = agent.indexOf("msie");
			var verIE = parseInt(agent.substr(idxIE+5));
			if (verIE < 8) {
				x = getElementLeft(objPos) + 1;
				y = getElementBottom(objPos) + 1;
			} else {
				x = getElementLeft(objPos);
				y = getElementBottom(objPos);
			}
		} else {
			if (agent.indexOf("opera") != -1) {
				x = getElementLeft(objPos);
				y = getElementBottom(objPos) - 2;
			} else {
				x = getElementLeft(objPos);
				y = getElementBottom(objPos);
			}
		}
		
/* WIDTH CHECK:
added code to determine width of browser window and adjust x 
subtract specified width of drop-down menus (replace value of 
menuWidth below)

adjust for body padding and/or margins as needed for Mac IE
*/
		var divWidth = 190;
		var winWidth = 0;

		if (typeof(window.innerWidth) == 'number') { // Non-IE
			winWidth = window.innerWidth;
		} else {
			if (document.documentElement && document.documentElement.clientWidth) { // IE 6+ in 'standards compliant mode'
				winWidth = document.documentElement.clientWidth;
			} else { 
				if (document.body && document.body.clientWidth) { // IE 4 compatible
					winWidth = document.body.clientWidth;

					var sidePad = 15;
					var topPad = 15;
					x = x + sidePad; 		// adjust for side padding
					y = y + topPad;			// adjust for top padding
				}
			}
		}
		
		if (x + divWidth > winWidth) {
			x = winWidth - divWidth;
		}
/* end WIDTH CHECK added code */
		
		setElementProperty(openMenu, 'display', 'block');
		setElementProperty(openMenu, 'left', x + "px");
		setElementProperty(openMenu, 'top', y + "px");
		imageOn(hoverImg);				// added to trigger img hover state
	}
}

function hideMenu(id){
	setElementProperty(id, 'display', 'none');
	openMenu = null;
}

function getMousePos(event){
	var x, y;
	if(window.event){
		x = window.event.clientX;
		y = window.event.clientY;
		if (document.documentElement && document.documentElement.scrollTop){
			y+=document.documentElement.scrollTop;
		} else {
			y+=document.body.scrollTop;
		}
	} else {
		x = event.pageX;
		y = event.pageY;
	}

	if(openMenu != null){
		var testInside = isInside(x, y, openMenu);
		if(!testInside && canMenuBeClosed == true){
			hideMenu(openMenu);
		}
		if(testInside){
			canMenuBeClosed = true;
			clearTimer();
		}
	}
}

function isInside(xMouse, yMouse, id){
	if( (id != null) && (xMouse >= getElementLeft(id)) && (xMouse <=getElementRight(id)) && (yMouse >= getElementTop(id)) && (yMouse <= getElementBottom(id)) ){
		return true;
	} else {
		return false;
	}
}

window.onload = function() {
	document.onmousemove = getMousePos;
}

/* 
	This function changes the display property of an element
*/

function switchDisplay(elm){
	var elm = elm.parentNode.nextSibling;
	while(elm.nodeType == 3){
		elm = elm.nextSibling;
	}
	
	var elmStyle = getElementProperty(elm, "display")

	if((elmStyle == "none") || (elmStyle == "") || (elmStyle == null)){
		setElementProperty(elm, "display", "block");
		return;
	}
	if(elmStyle == "block"){
		setElementProperty(elm, "display", "none");
		return;
	}
}
