// JavaScript Document
var timeout	= 100;
var closetimer	= 0;
var ddmenuitem	= 0;

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
    		oldonload();
	    	func();
		}
	}
}

function getNext(inNode,nt) {
	// NodeType properties
	// 1 - Element, 2 - Attribute, 3 - Text
	// will return null if we run out of nodes
	var curNode;
	curNode = inNode.nextSibling;
	while (curNode) {
		if (curNode.nodeType == nt) {
			break;
		}
		curNode = curNode.nextSibling;
	}
	return curNode;
}

// insert HTML hooks so dropdown menu will work
function insertFunctionality() {
	// make sure we have a JS enabled browser
	if (!document.getElementById) return;
	if (!document.getElementsByTagName) return;
	
	//grab the url so we can highlight the current page
	var url = document.location.href;
	var curArea = 'qq';
	if (url.indexOf('/Products',0)>-1) {
		curArea = 'Products';
	}
	if (url.indexOf('/Support-Services',0)>-1) {
		curArea = 'Support-Services';
	}
	if (url.indexOf('/Client-Results',0)>-1) {
		curArea = 'Client-Results';
	}
	if (url.indexOf('/News-Events',0)>-1) {
		curArea = 'News-Events';
	}
	
	//cycle through the elements in our dropdown menu
	var ddElementStart = document.getElementById("navigationDropdown");
	var ddElementList = ddElementStart.getElementsByTagName("*"); //seems to return all elements, even if they dont have a tag, hooray for us.
	
	for (var xx=0; xx<ddElementList.length; xx++) {
		var curClass = ddElementList[xx].getAttribute("class");
		if (curClass != null && curClass.toLowerCase() == 'anchortitle') {
			ddElementList[xx].onmouseover = function() { 
				mopen(this);
			}
			ddElementList[xx].onmouseout = function() {
				mclosetime();
			}
			var curElemAttr = ddElementList[xx].getAttribute('href');
			if (curElemAttr.indexOf(curArea,0)>-1) {
				ddElementList[xx].setAttribute('id','hiLiteIfPage');
			}
		}
		if (curClass != null && curClass.toLowerCase() == 'dropdiv') {
			ddElementList[xx].onmouseover = function() { 
				mcancelclosetime();
			}
			ddElementList[xx].onmouseout = function() {
				mclosetime();
			}
		}
	}
}

// open hidden layer
function mopen(id) {	
	// cancel close timer
	mcancelclosetime();

	// close old layer
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';

	//dduitem = id.getfirstChild;
	ddmenuitem = getNext(id,1);
	if (ddmenuitem) {
		ddmenuitem.style.visibility = 'visible';
	}
}
// close showed layer
function mclose() {
	if(ddmenuitem) ddmenuitem.style.visibility = 'hidden';
}

// go close timer
function mclosetime() {
	closetimer = window.setTimeout(mclose, timeout);
}

// cancel close timer
function mcancelclosetime() {
	if(closetimer) 	{
		window.clearTimeout(closetimer);
		closetimer = null;
	}
}

addLoadEvent(insertFunctionality);

