﻿function cl_Menu()
{
	this.CurrentSubMenu = null;
	this.ShowTimeout = null;
	this.HideTimeout = null;
	
	this.ShowSubMenu = function(id)
	{
		if (menu.CurrentSubMenu && id == menu.CurrentSubMenu.id)
			window.clearTimeout(menu.HideTimeout);
		
		window.clearTimeout(this.ShowTimeout);
		this.ShowTimeout = window.setTimeout('__showSubMenuAsync("' + id + '");', 250);
	}
	this.HideSubMenu = function(id, ms)
	{
	    if (ms == null) ms = 1000;
	    window.clearTimeout(this.ShowTimeout);
	    if (this.CurrentSubMenu && id == this.CurrentSubMenu.id)
	        this.HideTimeout = window.setTimeout('__hideSubMenuAsync("' + id + '");', ms);
	}
	this.SubMenuOver = function(subMenu)
	{
		window.clearTimeout(this.HideTimeout);
	}
	this.SubMenuOut = function(subMenu)
	{
		this.HideSubMenu(subMenu.id);
	}
}

var menu = new cl_Menu();

function __showSubMenuAsync(id)
{
	var subMenu = document.getElementById(id);
	if (!subMenu) return;
	
	if (menu.CurrentSubMenu && id != menu.CurrentSubMenu.id)
	{
		window.clearTimeout(menu.HideTimeout);
		__hideSubMenuAsync(menu.CurrentSubMenu.id);
	}
	menu.CurrentSubMenu = subMenu;
	
	subMenu.style.display = 'block';
}

function __hideSubMenuAsync(id)
{
	var subMenu = document.getElementById(id);
	if (!subMenu) return;
	subMenu.style.display = 'none';
}

var __bc_override = false;
function __bodyClickOverride()
{
    __bc_override = true;
}
function __bodyClickHide()
{
    if (__bc_override) { __bc_override = false; return; }
    if (menu.CurrentSubMenu == null) return;
    menu.HideSubMenu(menu.CurrentSubMenu.id, 0);
}
document.body.onclick = __bodyClickHide;
