// JavaScript Document

jQuery(document).ready(function(){
	var mis = jQuery(".menu .menuitem, .menu .topmenuitem");

	mis.each (function() {
		jQuery(this).addClass("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;
	var elt = jQuery(this);
	while (elt.attr('parentId') != null)
	{
		var parentId = elt.attr('parentId');
		elt = jQuery("#"+parentId);
		elt.each (function(){
			if (this.menuOutTimeoutId != null) {
				clearInterval(this.menuOutTimeoutId);
				this.menuOutTimeoutId = null;
			}
		});
	}

	jQuery(this).addClass("mouseOver");
	jQuery(this).removeClass("mouseOut");
	
	jQuery("#"+eltId+"-submenu").show();
}

function onMenuOut()
{
	this.menuOutTimeoutId = timedMenuOut(this.id);
	
	var elt = jQuery(this);
	while (elt.attr('parentId') != null)
	{
		var parentId = elt.attr('parentId');
		elt = jQuery("#"+parentId);
		elt.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();
		}
	}, 200);
}

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);
}
