/********************************/
/* Multi-level flyout menu		*/
/********************************/
MultiMenu = function (ElementName, Visible)
{
	// Set properties
	this.Element = $(ElementName);
	this.Counter = 0;
	this.Submenus = new Array();
	
	// Add top level
	this.RootMenu = new MultiMenuSubmenu(this.Element, null, this, Visible);
	
	// Return object
	return this;
}
MultiMenu.prototype.Show = function () { this.RootMenu.Show(true); }
MultiMenu.prototype.Hide = function () { this.RootMenu.Hide(true); }

// Submenu
MultiMenuSubmenu = function (Element, Parent, Menu, Visible)
{
	// Set properties
	this.Menu = Menu;
	this.Element = Element;
	this.MenuItem = Parent ? Element.parentNode.firstChild : null;
	this.ParentMenu = Parent;
	this.Delay = null;
	this.IsVisible = Visible;
	
	// Attach events
	var Submenu = this;
	attachEventListener(this.Element, "mouseover", function () { Submenu.Show(); }, true);
	attachEventListener(this.Element, "mouseout", function () { Submenu.Hide(); }, true);
	
	// Build submenus
	this.BuildSubmenus();	
	
	// Reference to self
	this.Self = "MultiMenuSubmenu" + (this.Menu.Counter++);
	eval(this.Self + "=this");
	
	// Return object
	return this;
}
MultiMenuSubmenu.prototype.Show = function (Now)
{
	// Clear hiding interval
	clearTimeout(this.Delay);
	
	// Wait
	if (!Now) 
	{		
		// Show this
		try { eval(this.Self + ".Show(true)"); }
		catch (err) { } 
	}
	else
	{
		// Highlight menu item
		if (this.MenuItem) this.MenuItem.className += " Highlight";
		
		// Show element
		show(this.Element);
	}
}
MultiMenuSubmenu.prototype.Hide = function (Now)
{
    // Ignore
    if (this.IsVisible) return;
    
	// Clear exisitng hiding interval
	clearTimeout(this.Delay);
	
	// Wait
	if (!Now) 
	{		
	    try { eval("if (" + this.Self + ".Hide) { /*nada*/ } ;"); }
		catch (err) { return; }
	
		// Hide this
		this.Delay = setTimeout(this.Self + ".Hide(true)", 50);
		
		// Hide parent
		if (this.ParentMenu) this.ParentMenu.Hide();
	}
	else
	{
		// Lowlight menu item
		if (this.MenuItem) this.MenuItem.className = this.MenuItem.className.replace(/Highlight/gi, "");
		
		// Hide element
		hide(this.Element);
	}
}
MultiMenuSubmenu.prototype.BuildSubmenus = function ()
{
	// Loop through children
	for (var i = 0; i < this.Element.childNodes.length; i++)
	{
		// Get node
		var Node = this.Element.childNodes[i];
		if (Node.nodeName.toLowerCase() == "li")
		{
			// Find submenu
			var SubmenuElements = Node.getElementsByTagName("ul");
			if (SubmenuElements.length > 0)
			{
				// Create submenu object
				Node.Submenu = new MultiMenuSubmenu(SubmenuElements[0], this, this.Menu);
				
				// Attach events
				Node.onmouseover = function () { this.Submenu.Show(); }
				Node.onmouseout = function () { this.Submenu.Hide(); }
				
				// Attach style
				Node.firstChild.className += " Arrow";
			}
		}
	}
}