var hoverDelayShow	= 90;				// in milliseconds, the top menu disappear delay
var hoverDelayHide	= 900;				// in milliseconds, the submenu disappear delay

var LoadObjects = new Array();
var hasExecuted = false;
//var hasExecutedTimeout = setTimeout(function() {
//	if (!hasExecuted) aigSetup();
//}, 10000);

Setup = function() {
	hasExecuted = true;
	for (var i = 0; i < LoadObjects.length; ++i) {
		LoadObjects[i]();
	}
}

//$(document).ready(function() { if (!hasExecuted) Setup(); });
//document.onLoad = function() { if (!hasExecuted) Setup(); };

function init() {
   // quit if this function has already been called
   if (arguments.callee.done) return;

   // flag this function so we don't do the same thing twice
   arguments.callee.done = true;

   // create the "page loaded" message
   Setup();
};

/* for Mozilla */
if (document.addEventListener) {
   document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
   document.write("<script defer src=ie_onload.js><"+"/script>");
/*@end @*/

/* for other browsers */
window.onload = init;


setupSubNav = function(theTimeout, targetA, delay, showing) {
	clearTimeout(theTimeout);

	return theTimeout = setTimeout(function() {
		//var thisName = $(targetA).get(0).data.toString().replace(/(\u00a0|&nbsp;|\s)+/ig, '_').replace(/\s/g, '*').replace(/\u00a0/, '*').replace(/[^a-z]/g, '&');
		var thisName = $(targetA).get(0).firstChild.data.toString();
		var subNav = $("#secnav").find("[@title='" + thisName + "']");
		var isDropDown = ($('#secnav').css('position') == 'absolute') ? 1 : 0;

		$("#topnav li a.active").removeClass("active");
		$(targetA).addClass("active");
		
		// make drop-down list types work
		if (isDropDown) {
			$("#secnav").css("left", findPos($(targetA).get(0))[0] - findPos($("#topnav").get(0))[0] );
		}
		
		$("#secnav div").hide().find('ul').hide();
		if (isDropDown) {
			if (showing) subNav.hide().show().find("ul").show();
		} else {
			subNav.hide().show().find("ul").fadeIn('fast');
		}
		
		//setTimeout(function() { subNav.show().find("ul").show().fadeIn('fast'); }, 30);
	}, delay);
}

//$(document).ready(function() {
LoadObjects.push(function() {
	var viewingA = null;		// which nav item we are currently viewing (the loaded page)
	var theTimeout = null;
	var subNavDiv = $("#secnav");
	
	viewingA = $("#topnav li a.active").get(0);
	if (viewingA == null) viewingA = $("#topnav li").get(0);

	$("#topnav li").each(function(i) {
		var thisName = $("a", this).get(0).firstChild.data.toString();	// setup classes for drop-downs
		var childCount = $("#secnav").find("[@title='" + thisName + "']").length;
		if (childCount > 0 && thisName.toUpperCase() != 'HOME') $(this).addClass('hasChildren');
		
		$(this)
			.hover(function() {
				theTimeout = setupSubNav(theTimeout, $("a", this).get(0), hoverDelayShow, true);
			}, function() {
				theTimeout = setupSubNav(theTimeout, viewingA, hoverDelayHide, false);
			})
		;
	});
	$("#secnav")
		.hover(function() {
			clearTimeout(theTimeout);
		}, function() {
			theTimeout = setupSubNav(theTimeout, viewingA, hoverDelayHide, false);
		})
	;
});