/*******************************/
/****** REQUIRES PROTOTYPE *****/
/*******************************/

var frameHeight = -1;
var frameLeft = -1;
var frameRight = -1;
var timeout;

var Engine = {
	detect: function() {
	var UA = navigator.userAgent;
	this.isMac = /Mac/.test(UA);
	this.isKHTML = /Konqueror|Safari|KHTML/.test(UA);
	this.isGecko = (/Gecko/.test(UA) && !this.isKHTML);
	this.isOpera = /Opera/.test(UA);
	this.isMSIE  = (/MSIE/.test(UA) && !this.isOpera);
	this.isMSIE7 = this.isMSIE && !(/MSIE 6\./.test(UA) && !this.isOpera);
	}
}
Engine.detect();


function killNav() {
	timeout = window.setTimeout('actionKillNav();', 500);
}
function showNav(item, onStyle) {
	window.clearTimeout(timeout);
	actionKillNavItem(item);
	actionShowNav(item, onStyle);
}

function actionKillNav() {
	for (var i=0;i<navLoop;i++) {
		document.getElementById('mainNav' + i).style.zIndex = 4;
		if (document.getElementById('mainNav' + i).className != 'menuNavItem')
			document.getElementById('mainNav' + i).className = 'menuNavItem';
		document.getElementById('subnavdiv' + i).style.visibility = 'hidden';
		document.getElementById('SubNavFrame').style.display = 'none';
	}
}

function actionKillNavItem(item) {
	for (var i=0;i<navLoop;i++) {
		if (i != item) {
			document.getElementById('mainNav' + i).style.zIndex = 4;
			if (document.getElementById('mainNav' + i).className != 'menuNavItem')
				document.getElementById('mainNav' + i).className = 'menuNavItem';
			document.getElementById('subnavdiv' + i).style.visibility = 'hidden';
		}
	}
}


//Turn on specified menu
function actionShowNav(item, onStyle) {
	
	//killNav();	
	
	document.getElementById('mainNav' + item).style.zIndex = 8;
	if (document.getElementById('mainNav' + item).className != onStyle)
		document.getElementById('mainNav' + item).className = onStyle;
	
	var ofT = getOffsetTop(window.document.getElementById('mainNav' + item));
	var ofLi = getOffsetLeft(window.document.getElementById('mainNav' + item));
	var ofLb = getOffsetLeft(window.document.getElementById('PageHeaderInner')); 
	
	//var menuHeight = window.document.getElementById('subnavdiv' + item).clientHeight;
	//var subNavTop = ofT - 205 - menuHeight;
	
	//var headerWidth = window.document.getElementById('mainNav' + item).offsetWidth;
	//headerWidth = Math.ceil(headerWidth/2);
	
	var subNavTop = ofT - 53;
	var subNavLeft = (ofLi - ofLb) - 90;
	//var subNavLeft = (ofLi - ofLb) - 145 + headerWidth;
	
	if (subNavLeft > xMax) subNavLeft = xMax;
	
	if (Engine.isKHTML) {
	    subNavTop -= 3;
	}
	
	window.document.getElementById('subnavdiv' + item).style.top = subNavTop + 'px';
	window.document.getElementById('subnavdiv' + item).style.left = subNavLeft + 'px';
	
	window.document.getElementById('subnavdiv' + item).style.visibility = 'visible';
	
	/*
	try {
		if (window.navigator.userAgent.indexOf('IE') > -1 && hasSelect()) {
		    // set frame params if we don't have them
		    if (frameHeight < 0) frameDim();
			
			// if frame params are set
			if (frameLeft >= 0 && frameRight >=0) {
			    // if there's horizontal overlap
			    if (overlap(frameLeft, frameRight, subNavLeft, subNavLeft + window.document.getElementById('subnavdiv' + item).clientWidth)) {
			        window.document.getElementById('SubNavFrame').style.display = 'block';
			        window.document.getElementById('SubNavFrame').style.top = subNavTop;
			        window.document.getElementById('SubNavFrame').style.height = Math.min(frameHeight, window.document.getElementById('subnavdiv' + item).clientHeight-20);
    			    
			        // if the select is to the left of the nav
			        if (frameLeft < subNavLeft) {
			            window.document.getElementById('SubNavFrame').style.left = subNavLeft;
			            window.document.getElementById('SubNavFrame').style.width = frameLeft - subNavLeft;
			        }
			        // select matches or is to right of nav
			        else {
			            window.document.getElementById('SubNavFrame').style.left = frameLeft;
			            window.document.getElementById('SubNavFrame').style.width = window.document.getElementById('subnavdiv' + item).clientWidth - (frameLeft - subNavLeft);
			        }
                }
                else {
                    window.document.getElementById('SubNavFrame').style.display = 'none';
                }			    
			}
			else {
			    window.document.getElementById('SubNavFrame').style.display = 'block';
			    window.document.getElementById('SubNavFrame').style.width = window.document.getElementById('subnavdiv' + item).clientWidth;
			    window.document.getElementById('SubNavFrame').style.height = Math.min(frameHeight, window.document.getElementById('subnavdiv' + item).clientHeight-20);
			    window.document.getElementById('SubNavFrame').style.top = subNavTop;
			    window.document.getElementById('SubNavFrame').style.left = subNavLeft;
			}
		}
	} catch (e) {
	}
	*/
}

function getOffsetLeft (el) {
	var ol = el.offsetLeft;
	while ((el = el.offsetParent) != null)
		ol += el.offsetLeft;
	return ol;
}
function getOffsetTop (el) {
	var ot = el.offsetTop;
	while((el = el.offsetParent) != null)
		ot += el.offsetTop;
	return ot;
}

function doCheckOver(el) {
	if (!el.contains(event.fromElement))
		killNav();
}

/*
function overlap(minA, maxA, minB, maxB) {
    return ((minA >= minB && minA <= maxB) || (maxA >= minB && maxA <= maxB));
}
*/

/*
function hasSelect() {
	var selects = window.document.getElementsByTagName('select');
	return (selects.length > 0);
}
*/

/*
function frameDim() {
	var selects = window.document.getElementsByTagName('select');
	for (var i in selects) {
	    if (typeof(selects[i]) == 'object') {
            frameHeight = Math.max((getOffsetTop(selects[i]) - 91) + selects[i].clientHeight, frameHeight);
            frameLeft = Math.max(getOffsetLeft(selects[i]) - 7, frameLeft);
            frameRight = Math.max(getOffsetLeft(selects[i]) - 7 + selects[i].clientWidth, frameRight);
	    }
	}
}
*/