

var showDelay = 250;
var showTime = 500;
var showTime2 = showTime;
if ($.browser.msie)
	showTime = 0;

var hideTime = 20000;
var maxHeight = 30;

var timerID = null;
var menuOpen = false;

function menuSetUp() {
	$("#menubar > a").mouseover(function(evt) {
		$("#subsubmenu > div > a").removeClass("has-over");
		if ($("#" + evt.target.id).hasClass("has-submenu")) {
			showSubMenu(evt.target.id);
		} else {
			closeSubMenu(evt.target.id);
		}
	});

	$("#submenu").mouseover(function(evt) {
		if (menuOpen && timerID)
			window.clearTimeout(timerID);
		if (restoreTimerID)
			window.clearTimeout(restoreTimerID);
	});

	$("#submenu > div > a").mouseover(function(evt) {
		$("#subsubmenu > div > a").removeClass("has-over");
		if ($("#" + evt.target.id).hasClass("has-subsubmenu")) {
			showSubSubMenu(evt.target.id);
		} else {
			closeSubSubMenu(evt.target.id);
		}
	});

	$("#subsubmenu > div > a").mouseover(function(evt) {
		$("#subsubmenu > div > a").removeClass("has-over");
		$("#" + evt.target.id).addClass("has-over");
	});

	$("#subsubmenu").mouseover(function(evt) {
		if (subMenuOpen && subTimerID)
			window.clearTimeout(subTimerID);
		if (restoreTimerID)
			window.clearTimeout(restoreTimerID);
	});

	$("#wholemenu").mouseout(function(evt) {
		restoreDefaultMenu(currMenu);
	});

	setUpCurrentMenu(currMenu);
}

function setUpCurrentMenu(id) {
	if(document.getElementById('menu' + id)){
		$("#menubar > a").removeClass("has-over");
		$("#submenu > div > a").removeClass("has-over");
		$("#subsubmenu > div > a").removeClass("has-over");
		$("#submenu > div").hide(showTime2);
		$("#subsubmenu > div").hide(showTime2);
	
		if (id == 0) {
			$("#submenu").stop(true, true);
			$("#submenu").animate({height: '0px'}, showTime);
			$("#subsubmenu").stop(true, true);
			$("#subsubmenu").animate({height: '0px'}, showTime);
			return;
		}

		var item = document.getElementById('menu' + id);
		var pdiv = item.parentNode;

		$("#menu" + id).addClass("has-over");
		if (pdiv.id == "menubar") {
			doCloseSubMenu('menu' + id);
			return;
		}

		var pmenu = pdiv.id.replace(/^sub/, '');
		$("#" + pmenu).addClass("has-over");
		$("#sub" + pmenu).show();
		$("#submenu").stop(true, true);
		$("#submenu").animate({height: maxHeight + 'px'}, showTime);
		menuOpen = true;

		var ppdiv = pdiv.parentNode;
		if (ppdiv.id == "submenu") {
			doCloseSubSubMenu('menu' + id);
			return;
		}
	
		var ppmenu = document.getElementById(pmenu).parentNode.id.replace(/^sub/, '');
		$("#" + ppmenu).addClass("has-over");

		$("#sub" + ppmenu).show();
		$("#subsubmenu").stop(true, true);
		$("#subsubmenu").animate({height: maxHeight + 'px'}, showTime);
		subMenuOpen = true;
	}
}

var restoreTimerID = null;
function restoreDefaultMenu(id) {
	if (restoreTimerID)
		window.clearTimeout(restoreTimerID);

	restoreTimerID = window.setTimeout('setUpCurrentMenu(' + id + ')', hideTime);
}

function showSubMenu(id) {
	if (restoreTimerID)
		window.clearTimeout(restoreTimerID);
	if (timerID)
		window.clearTimeout(timerID);

	timerID = window.setTimeout('doShowSubMenu("' + id + '")', showDelay);
}

function doShowSubMenu(id) {
	$("#menubar > a").removeClass("has-over");
	$("#submenu > div > a").removeClass("has-over");
	$("#" + id).addClass("has-over");
	
	$("#submenu > div").hide(showTime2);
	$("#sub" + id).show(showTime2);
	$("#submenu").stop(true, true);
	$("#submenu").animate({height: maxHeight + 'px'}, showTime);
	menuOpen = true;

	$("#subsubmenu").stop(true, true);
	$("#subsubmenu").animate({height: '0px'}, showTime);
	subMenuOpen = false;
}

function closeSubMenu(id) {
	if (restoreTimerID)
		window.clearTimeout(restoreTimerID);
	if (timerID)
		window.clearTimeout(timerID);

	timerID = window.setTimeout('doCloseSubMenu("' + id + '")', showDelay);
}

function doCloseSubMenu(id) {
	$("#menubar > a").removeClass("has-over");
	$("#submenu > div > a").removeClass("has-over");
	$("#" + id).addClass("has-over");

	$("#submenu").stop(true, true);
	$("#submenu").animate({height: '0px'}, showTime);
	menuOpen = false;

	if (subTimerID)
		window.clearTimeout(subTimerID);
	$("#subsubmenu").stop(true, true);
	$("#subsubmenu").animate({height: '0px'}, showTime);
	subMenuOpen = false;
}

var subTimerID = null;
var subMenuOpen = false;

function showSubSubMenu(id) {
	if (restoreTimerID)
		window.clearTimeout(restoreTimerID);
	if (subTimerID)
		window.clearTimeout(subTimerID);

	subTimerID = window.setTimeout('doShowSubSubMenu("' + id + '")', showDelay);
}

function doShowSubSubMenu(id) {
	$("#submenu > div > a").removeClass("has-over");
	$("#" + id).addClass("has-over");

	$("#subsubmenu > div").hide(showTime2);
	$("#sub" + id).show(showTime2);
	$("#subsubmenu").stop(true, true);
	$("#subsubmenu").animate({height: maxHeight + 'px'}, showTime);
	subMenuOpen = true;
}

function closeSubSubMenu(id) {
	if (restoreTimerID)
		window.clearTimeout(restoreTimerID);
	if (subTimerID)
		window.clearTimeout(subTimerID);

	subTimerID = window.setTimeout('doCloseSubSubMenu("' + id + '")', showDelay);
}

function doCloseSubSubMenu(id) {
	$("#submenu > div > a").removeClass("has-over");
	$("#" + id).addClass("has-over");

	$("#subsubmenu").stop(true, true);
	$("#subsubmenu").animate({height: '0px'}, showTime);
	subMenuOpen = false;
}


