// Changeable parameters
var hoverDelaySub = 70;				// in milliseconds, the submenu disappear delay
var hoverDelayCat = 50;				// in milliseconds, the top menu disappear delay

// Internal variables (don't edit or you will invoke the Wrath of the Webmaster)
var W3CDOM = (document.createElement && document.getElementsByTagName);
var nav, nav2, navChildren, navChildren2;
var menu, actuator, currentMenu = null; 							// The three vars used by the country menu
var activeArea;
window.onload = init;
var currentMenuNode = null;
var currentPageNode = null;
var timerRunningSub = false;
var timerIDSub = null;
var timerRunningCat = false;
var timerIDCat = null;
var timerRunningSubCountry = false;
var timerIDSubCountry = null;
var timerRunningCatCountry = false;
var timerIDCatCountry = null;

function DL_GetElementLeft(eElement) {
    var nLeftPos = eElement.offsetLeft;
    var eParElement = eElement.offsetParent;
    while (eParElement != null) {
        nLeftPos += eParElement.offsetLeft;
        eParElement = eParElement.offsetParent;
    }
    return nLeftPos;
}

function DL_GetElementTop(eElement) {
    var nTopPos = eElement.offsetTop;
    var eParElement = eElement.offsetParent;
    while (eParElement != null) {
        nTopPos += eParElement.offsetTop;
        eParElement = eParElement.offsetParent;
    }
    return nTopPos;
}

function triggerTimerSub() {
	if(navChildren == null) return;				// happens every now & then at page load
	//currentPageNode.className = "active";		// needs a way to determine the active page first
	for (var i = 0; i < navChildren.length; ++i) {
		navChildren.item(i).className = null;
	}
	currentPageNode.className = "active";
	
	if(navChildren2 == null) return;				// happens every now & then at page load
	for (var j = 0; j < navChildren2.length; ++j) {
		var oNode2 = navChildren2.item(j);
		if(oNode2.title == currentPageNode.firstChild.data) {
			oNode2.style.visibility = "visible";
			oNode2.style.display = "block";
		} else {
			oNode2.style.visibility = "hidden";
			oNode2.style.display = "none";
		}
	}
}

function stopTimer(timerRunning, timerID) {
	if(timerRunning) {
		clearTimeout(timerID);
		timerRunning = false;
	}
}

function stopTimerSub() {
	stopTimer(timerRunningSub, timerIDSub);
}

function startTimerSub() {
	timerRunningSub = true;
	timerIDSub = self.setTimeout("triggerTimerSub()", hoverDelaySub);
}

function resetTimerSub() {
	stopTimerSub();
	startTimerSub();
}

function triggerTimerCat(thisTitle) {
	if(navChildren2 == null) return;				// happens every now & then at page load
	// show only the currently hovered submenu
	for (var j = 0; j < navChildren2.length; ++j) {
		var oNode2 = navChildren2.item(j);
		if(oNode2.title == thisTitle) {
			oNode2.style.visibility = "visible";
			oNode2.style.display = "block";
		} else {
			oNode2.style.visibility = "hidden";
			oNode2.style.display = "none";
		}
	}
	
	// highlight only the currently hovered category
	for (var k = 0; k < navChildren.length; ++k) {
		if((navChildren.item(k).firstChild != null) && (navChildren.item(k).firstChild.data == thisTitle)) {
			navChildren.item(k).className = "active";
		} else {
			navChildren.item(k).className = null;
		}
	}
}

function stopTimerCat() {
	stopTimer(timerRunningCat, timerIDCat);
}

function startTimerCat(thisTitle) {
	timerRunningCat = true;
	timerIDCat = self.setTimeout("triggerTimerCat('" + thisTitle + "')", hoverDelayCat);
}

function resetTimerCat(thisTitle) {
	stopTimerCat();
	startTimerCat(thisTitle);
}

function triggerTimerSubCountry() {
	if(currentMenu != null) {
		currentMenu.style.visibility = "hidden";
		currentMenu = null;
		//alert("hiding menu: " + currentMenu);
	}
}

function stopTimerSubCountry() {
	stopTimer(timerRunningSubCountry, timerIDSubCountry);
}

function startTimerSubCountry() {
	timerRunningSubCountry = true;
	timerIDSubCountry = self.setTimeout("triggerTimerSubCountry()", hoverDelaySub);
}

function resetTimerSubCountry() {
	stopTimerSubCountry();
	startTimerSubCountry();
}

function triggerTimerCatCountry() {
	if (currentMenu == null) {
		showMenu();
		//alert("showing menu: " + currentMenu);
	}
}

function stopTimerCatCountry() {
	stopTimer(timerRunningCatCountry, timerIDCatCountry);
}

function startTimerCatCountry(thisTitle) {
	timerRunningCatCountry = true;
	timerIDCatCountry = self.setTimeout("triggerTimerCatCountry()", hoverDelayCat);
}

function resetTimerCatCountry() {
	stopTimerCatCountry();
	startTimerCatCountry();
}

function listIndexOf(haystack, needle) {
	var index = 0;
	for(var i = 0; i < haystack.length; ++i) {
		if(haystack[i] == needle)
			return i;
	}
	return -1;
}

function activateActiveArea() {
	nav = document.getElementById('topNav');
	if(nav == null) return;
	navChildren = nav.getElementsByTagName("A");
	for (var i = 0; i < navChildren.length; ++i) {
		if(navChildren.item(i).firstChild.data == activeArea) {
			currentPageNode = navChildren.item(i);
		}
	}
	if(currentPageNode == null) {
		for (var i = 0; i < navChildren.length; ++i) {
			if(navChildren.item(i).firstChild.data == "Home") {
				currentPageNode = navChildren.item(i);
			}

		}
	}
}

function getActiveArea() {
	nav = document.getElementById('topNav');
	nav2 = document.getElementById('secondaryNav');
	if(nav == null) return;
	navChildren = nav.getElementsByTagName("A");
	navChildren2 = (nav2 == null) ? null : nav2.getElementsByTagName("DIV");
	
	// Set it to "Home" as a failsafe default
	for (var i = 0; i < navChildren.length; ++i) {
		if(navChildren.item(i).name == "Home") {
			currentPageNode = navChildren.item(i);
			break;
		}
	}
	
	for (var i = 0; i < navChildren.length; ++i) {
		if((navChildren.item(i).className != null) && (navChildren.item(i).className.length > 0)) {
			currentPageNode = navChildren.item(i);
			break;
		}
	}
}

/* Start the country selection menus now */

if (!document.getElementById)
    document.getElementById = function() { return null; }
	
function showMenu() {
	menu.style.left = (parseInt(DL_GetElementLeft(actuator)) - parseInt(document.getElementById('container').offsetLeft)) + "px";
	menu.style.top = DL_GetElementTop(actuator) + actuator.offsetHeight + 2 + "px";
	menu.style.visibility = "visible";
	currentMenu = menu;
	//alert ("menu is at: " + menu.style.left + " " + menu.style.top);
}

function initializeMenu(menuId, actuatorId) {
    menu = document.getElementById(menuId);
    actuator = document.getElementById(actuatorId);

    if (menu == null || actuator == null) return;

    //if (window.opera) return; // I'm too tired
	
	actuator.onmouseover = function() {
		// do the delayed reset
		stopTimerSubCountry();
		resetTimerCatCountry();
	}
	
	actuator.onmouseout = function() {
		resetTimerSubCountry();
		stopTimerCatCountry();
	}
	
	menu.onmouseover = function() {
		stopTimerSubCountry();
	}
	
	menu.onmouseout = function() {
		resetTimerSubCountry();
	}
}


function init() {
	if (!W3CDOM) return;
	
	// call external function to format old articles better to match the new style
	if (typeof(formatArticles) == 'function') formatArticles();
	
	// initialize the country-selection menu
	initializeMenu("countryMenu", "countryActuator");

	getActiveArea();
	
	if(currentPageNode == null) return;
	
	// un-highlight all but the active category
	for (var i = 0; i < navChildren.length; ++i) {
		var oNode = navChildren.item(i);
		if((oNode.firstChild != null) && (oNode.firstChild.data == currentPageNode.title)) {
			oNode.className = "active";
		} else {
			oNode.className = null;
		}
	}
	
	currentPageNode.className = "active";
	
	if(navChildren2 != null) {
		// first hide all but the active submenu
		for (var i = 0; i < navChildren2.length; ++i) {
			var oNode = navChildren2.item(i);
			if(oNode.title == currentPageNode.firstChild.data) {
				oNode.style.display = "block";
				oNode.style.visibility = "visible";
			} else {
				oNode.style.display = "none";
				oNode.style.visibility = "hidden";
			}
		}
	}
	
	// set the hover properties/events for the categories and submenus
	for (var i = 0; i < navChildren.length; ++i) {
		var oNode = navChildren.item(i);
		oNode.onmouseover = function() {
			currentMenuNode = this;
			
			// do the delayed reset
			if (typeof(stopTimerSub) == 'function') {
				stopTimerSub();
				if(this.firstChild != null)
					resetTimerCat(this.firstChild.data);
			}
		}
		
		oNode.onmouseout = function() {
			if (typeof(triggerTimerSub) == 'function') {
				resetTimerSub();
				stopTimerCat();
			}
		}
	}
	
	
	// set the hover properties/events for the submenus
	var secondaryNav = document.getElementById('secondaryNav');
	if (secondaryNav) {
		secondaryNav.onmouseover = function() {
			stopTimerSub();
		}
		secondaryNav.onmouseout = function() {
			resetTimerSub();
		}
	}
}

function OpenEmail() {
	emailWind=window.open("/home/email.asp?URLemail="+escape(window.location)+"&Title="+escape(document.title),"Email","status=no,toolbar=no,resizable=yes,location=no,width=320,height=350");
	emailWind.focus();
}

function OpenPrint() {
	var url = window.location.href;
	//if (url.indexOf('?') > -1) url += '&';
	//if (url.indexOf('?') == -1) url += '?';
	url += '&vPrint=1';
	//alert(url);
	printWind=window.open(url,"PrintFriendly","status=no,toolbar=no,resizable=yes,location=no,scrollbars=yes,width=800,height=600");
	printWind.focus();
}
