// JavaScript Document

jQuery(document).ready(function(){
	var mis = jQuery("#mainmenu .menuitem a");

	mis.each (function() {
		this.className = "mouseOut";
		this.onmouseover=onMenuOver;
		this.onmouseout=onMenuOut;
	});
	
	return true;
});

function onMenuOver()
{
	if (this.menuOutTimeoutId != null) {
		clearInterval(this.menuOutTimeoutId);
		this.menuOutTimeoutId = null;
	}
	
	var eltId = this.id;
	
	jQuery("#mainmenu .menuitem").not("#"+eltId).children("a").each (function(){
        if (jQuery(this).hasClass("mouseOver") && this.menuOutTimeoutId == null)
			this.menuOutTimeoutId = timedMenuOut(this.id);
	});
	
	jQuery("#mainmenu .menuitem").has("#"+this.id).children("a").each (function(){
		if (this.menuOutTimeoutId != null) {
			clearInterval(this.menuOutTimeoutId);
			this.menuOutTimeoutId = null;
		}
	});
	jQuery("#mainmenu .menuitem").has("#"+this.id).children("a").addClass("mouseOver");
	jQuery("#mainmenu .menuitem").has("#"+this.id).children("a").removeClass("mouseOut");

	jQuery(this).addClass("mouseOver");
	jQuery(this).removeClass("mouseOut");
	
	jQuery(this).siblings("ul").show();
}

function onMenuOut()
{
	jQuery("#mainmenu .menuitem").has("#"+this.id).children("a").each (function(){
        if (jQuery(this).hasClass("mouseOver") && this.menuOutTimeoutId == null)
			this.menuOutTimeoutId = timedMenuOut(this.id);
	});
}

function timedMenuOut(eltId)
{
	return setTimeout(function()
	{
//		jQuery("#debugger").append("close "+eltId+"<br>");
		if (jQuery(".menuitem > #"+eltId).has("ul .mouseOver").length == 0)
		{
			jQuery("#"+eltId).addClass("mouseOut");
			jQuery("#"+eltId).removeClass("mouseOver");
			jQuery("#"+eltId).siblings("ul").hide();
		}
	}, 300);
}

function showMenu (menuId, sender)
{
	jQuery(sender).addClass('menuActive');
	var menu = jQuery("#"+ menuId);
	menu.each (function()
	{
		this.closing = false;
	});
	menu.show ();
}

function hideMenu (menuId, sender)
{
	var menu = jQuery("#"+ menuId);
	menu.each (function (){this.closing = true;});
	sender.menuOutTimeoutId = setTimeout(function()
	{
		if (menu.length == 0)
			jQuery(sender).removeClass('menuActive');
		else
			menu.each (function () {
				if (this.closing) {
					var lis = jQuery("#"+menuId+" li");
					lis.each (function() {this.className = "mouseOut"});
					// menu.hide ();
					jQuery(sender).removeClass('menuActive');
				}
			});
	}, 200);
}
