//=================================================================================================
//	THE MAIN MENU
//=================================================================================================
TMenu = function( horizontal )
{
	this.Class        = "TMenu";
	this.disabled     = false;
	this.div          = null;
	this.horizontal   = horizontal;
	this.menuItems    = [];
	this.timer        = null;
	this.initialized  = false;
	this.initializing = false;
}
//-------------------------------------------------------------------------------------------------
TMenu.prototype.disable = function()
{
	if ( !this.disabled )
	{
		this.disabled = true;
		
		if ( !this.initialized )
			this.initialize();
		
		if ( this.initializing )
		{
			setTimeout("menu.disable()", 50 );
			return;
		}
		
		for ( var i = 0; i < this.menuItems.length; i++ )
			this.menuItems[i].disable();
	}
}
//-------------------------------------------------------------------------------------------------
TMenu.prototype.enable = function()
{
	if ( this.disabled )
	{
		this.disabled = false;
		
		if ( !this.initialized )
			this.initialize();
		
		if ( this.initializing )
		{
			setTimeout("menu.enable()", 50 );
			return;
		}
		
		for ( var i = 0; i < this.menuItems.length; i++ )
			this.menuItems[i].enable();
	}
}
//-------------------------------------------------------------------------------------------------
TMenu.prototype.initialize = function()
{
	if ( !this.initialized && !this.initializing )
	{
		this.initializing = true;
		
		this.div = document.getElementById("mainMenu");
		
		if ( this.div )
			for ( var i = 0; i < this.div.childNodes.length; i++ )
				if ( this.div.childNodes[i].nodeType == 1 )
					this.menuItems[this.menuItems.length] = new TMenuItem( this, this, this.div.childNodes[i] );
		
		this.initialized  = true;
		this.initializing = false;
	}
}
//=================================================================================================
//	A MENU ITEM
//=================================================================================================
TMenuItem = function( menu, parent, a )
{
	this.Class      = "TMenuItem";
	this.disabled   = false;
	this.parent     = parent;
	this.menu       = menu;
	this.menuItems  = [];
	this.timer      = null;
	
	this.a          = a;
	this.a.menuItem = this;
	this.id         = parseInt( a.id.substr( 1 ) );
	this.div        = document.getElementById("div" + this.id );
	
	if ( this.parent == this.menu )
		this.visible = true;
	else
		this.visible = false;
	
	if ( this.div )
	{
		this.div.menuItem      = this;
		this.div.style.display = "none";
		document.body.appendChild( this.div );
		
		for ( var i = 0; i < this.div.childNodes.length; i++ )
			if ( this.div.childNodes[i].nodeType == 1 )
				this.menuItems[this.menuItems.length] = new TMenuItem( this.menu, this, this.div.childNodes[i] );
		
		var
			border  = measure( this.div, "border-width"), 
			padding = measure( this.div, "padding-top");
		
		if ( border > 0 || padding > 0 )
		{
			addEvent( this.div, "mouseover", "stopHideMenu(\"" + this.div.id + "\");");
			addEvent( this.div, "mouseout",  "hideMenu( document.getElementById(\"" + this.a.id + "\") );");
		}
	}
	//------------------------------------------------------------------
}
//-------------------------------------------------------------------------------------------------
TMenuItem.prototype.disable = function()
{
	if ( !this.disabled )
	{
		this.a.disabled       = true;
		
		this.a.oldHRef        = this.a.href;
		this.a.href           = "javascript:void(0);";
		
		this.a.oldMouseOver   = this.a.onmouseover;
		
		if ( this.div )
			this.a.onmouseover = new Function("event", "stopHideMenu(\"" + this.div.id + "\");");
		else
			this.a.onmouseover = null;
		
		this.a.oldClass       = this.a.className;
		this.a.className     += " disabled";
		
		this.disabled         = true;
		
		for ( var i = 0; i < this.menuItems.length; i++ )
			this.menuItems[i].disable();
	}
}
//-------------------------------------------------------------------------------------------------
TMenuItem.prototype.enable = function()
{
	if ( this.disabled && !this.parent.disabled )
	{
		this.a.disabled     = false;
		
		this.a.href         = this.a.oldHRef;
		this.a.oldHRef      = undefined;
		
		this.a.onmouseover  = this.a.oldMouseOver;
		this.a.oldMouseOver = undefined;
		
		this.a.className    = this.a.oldClass;
		this.a.oldClass     = undefined;
		
		this.disabled       = false;
		
		for ( var i = 0; i < this.menuItems.length; i++ )
			this.menuItems[i].enable();
	}
}
//-------------------------------------------------------------------------------------------------
TMenuItem.prototype.hide = function( goUp, goDown )
{
/*/
	if ( goUp && goDown )
		alert( this.a.id + ".hide( " + valueString( goUp ) + ", " + valueString( goDown ) + " )");/**/
	
	if ( this.timer )
	{
		clearTimeout( this.timer );
		this.timer = null;
	}
	
	if ( goUp && this.parent != this.menu )
		this.parent.hide( true, false );
	
	if ( this.div && this.div.style.display != "none")
	{
		this.div.style.display = "none";
		
		if ( goDown )
			for ( var i = 0; i < this.menuItems.length; i++ )
				this.menuItems[i].hide( false, true );
	}
}
//-------------------------------------------------------------------------------------------------
TMenuItem.prototype.show = function()
{
//	alert( this.a.id + ".show()");
	
	this.stopHide();
	
	for ( var i = 0; i < this.parent.menuItems.length; i++ )
		if ( this.parent.menuItems[i] != this )
			this.parent.menuItems[i].hide( false, true );
	
	if ( this.div && this.div.style.display == "none")
	{
		var
			left = getLeft( this.a ),
			top  = getTop( this.a );
		
		if ( this.parent == menu && menu.horizontal )
		{
			top += this.a.offsetHeight;
			top += measure( menu.div, "border-bottom-width");
			
			if ( isIE )
				top += measure( menu.div, "border-top-width");
		}
		else
		{
			if ( isIE && this.a.firstChild.nodeName == "IMG")
			{
				var
					padding        = measure( this.a.parentNode, "padding-left"), 
					correctionLeft = this.a.firstChild.offsetLeft, 
					correctionTop  = 
							measure( this.a,            "padding-top") + 
							measure( this.a.firstChild, "margin-top");
				
				if ( this.a.parentNode.currentStyle.position == "absolute")
					correctionLeft -= padding;
				
				left -= correctionLeft;
				top  -= correctionTop;
			}
			
			top  += 3;
			left += this.a.offsetWidth - 3;
		}
		
		this.div.style.left    = left + "px";
		this.div.style.top     = top  + "px";
		this.div.style.display = "block";
		
		if ( (this.parent != menu || !menu.horizontal) && left + this.div.offsetWidth > document.body.offsetWidth - 20 )
		{
			left -= ( this.a.offsetWidth * 2 ) - 6;
			this.div.style.left = left + "px";
		}
	}
}
//-------------------------------------------------------------------------------------------------
TMenuItem.prototype.stopHide = function()
{
//	alert( this.a.id + ".stopHide()");
	
	if ( this.timer )
	{
		clearTimeout( this.timer );
		this.timer = null;
	}
	
	if ( hiding != null && hiding.timer )
	{
		clearTimeout( hiding.timer );
		
		if ( hiding.parent == this )
		{
			hiding = null;
			
			for ( var i = 0; i < this.menuItems.length; i++ )
			{
				var
					child = this.menuItems[i];
				
				if ( child.div )
					child.div.style.display = "none";
			}
		}
		else
			hiding = null;
	}
	
	if ( this.parent && this.parent != menu )
		this.parent.stopHide();
}
//=================================================================================================
//	HELPER FUNCTIONS
//=================================================================================================
function hideMenu( sender )
{
	if ( sender && menu && menu.initialized )
	{
		if ( sender.menuItem )
		{
			sender.menuItem.timer = setTimeout("hideMenu(\"" + sender.id + "\");", 500 );
			hiding = sender.menuItem;
		}
		else if ( typeof( sender ) == "string")
		{
			var
				menuItem = document.getElementById( sender ).menuItem;
			
			if ( typeof( menuItem ) != "undefined")
			{
//				alert("hideMenu( " + valueString( document.getElementById( sender ) ) + " )");
				menuItem.hide( true, true );
			}
		}
	}
}
//=================================================================================================
function showMenu( sender )
{
	if ( sender && typeof( sender.menuItem ) != "undefined")
	{
//		alert("showMenu( " + valueString( sender ) + " )");
		sender.menuItem.show();
	}
}
//=================================================================================================
function stopHideMenu( sender )
{
	sender = $(sender);
	
	if ( sender && typeof( sender.menuItem ) != "undefined")
	{
//		alert("stopHideMenu( " + valueString( sender ) + " )");
		sender.menuItem.stopHide();
	}
}
//=================================================================================================
var
	hiding = null, 
	menu   = new TMenu( horizontal );
//=================================================================================================
/*/
function alert( text )
{
	if ( typeof( this.timer ) != "undefined")
		clearTimeout( this.timer );
	
	this.timer = setTimeout("colorLast()", 2000 );
	
	var 
		debug = document.getElementById("divDebug");
	
	if ( !debug )
	{
		debug = document.createElement("DIV");
		
		debug.id            = "divDebug";
		debug.style.display = "none";
		
		document.body.appendChild( debug );
		
		debug.style.position   = "absolute";
		debug.style.background = "white";
		debug.style.border     = "1px solid black";
		debug.style.height     = "700px";
		debug.style.overflow   = "auto";
		debug.style.width      = "400px";
	}
	
	if ( debug )
	{
		debug.style.display = "block";
//		debug.style.left    = ( (getSize( szPage ).width - 400) / 2 ) + "px";
		debug.style.left    = "10px";
		positionElement( debug, 200 );
		
		debug.innerHTML += "<div>" + String( text ).htmlEncode() + "</div>";
		debug.scrollTop = debug.scrollHeight;
	}
}
//-------------------------------------------------------------------------------------------------
function colorLast()
{
	var 
		debug = document.getElementById("divDebug");
	
	if ( debug && debug.lastChild )
		debug.lastChild.style.backgroundColor = "#E0E0E0";
}
//=================================================================================================
//*/