//===========================================================================
// FMenu v2.0 
//   for JavaScript
//
// - Using document object model, W3C compliant
// - Uses iframes under SubMenu's to mask components like <select>
// - Sets focus to an invisible <input> in order to hide the flashing cursor
//
// Supported browsers
//  * IE5.0 : No <iframe> so component masking doesn't work
//  * IE5.5 : 
//  * IE6.0 : Perfect
//  * FF1.5 : Perfect
//
// 2006 © Copyright by Fastload-Media.be
// Programmed by Huysentruit Wouter
//===========================================================================

//===========================================================================
// FMenu object
//---------------------------------------------------------------------------
function FMenu(ObjectName, Parent)
{
	this.ObjectName = ObjectName;
	this.Parent = Parent;
	this.ParentTd = null;
	this.ItemCount = 0;
	this.CurrentSubMenu = null;
	this.SubMenuHideTimeout = null;
	this.CreateDom();
}
//---------------------------------------------------------------------------
FMenu.prototype.CreateDom = function()
{
	// Create elements
	this.div = document.createElement("div");
	this.table = document.createElement("table");
	this.tbody = document.createElement("tbody");
	// Set element parameters
	this.div.className = this.Parent?"FMenuFloating":"FMenu";
	this.div.Menu = this;
	this.div.setAttribute("id", this.ObjectName);
	this.table.className = "FMenu";
	this.table.setAttribute("cellPadding", 0);
	this.table.setAttribute("cellSpacing", 0);	
	// Append elements
	this.table.appendChild(this.tbody);
	this.div.appendChild(this.table);
	// Auto append submenu to document.body
	if (this.Parent) this.AppendDom();
	// Create input field for cursor hack
	if (!this.Parent)
	{
		var input = document.createElement("input");
		input.setAttribute("id", this.ObjectName + "_hack");
		input.style.position = "absolute";
		input.style.left = 0;
		input.style.top = 0;
		input.style.width = 5;
		input.style.height = 5;
		input.style.display = "none";
		document.getElementsByTagName("body").item(0).appendChild(input);
	}
}
//---------------------------------------------------------------------------
FMenu.prototype.AppendDom = function(Node)
{
	if (!Node) Node = document.getElementsByTagName("body").item(0);
	Node.appendChild(this.div);
}
//---------------------------------------------------------------------------
FMenu.prototype.RemoveDom = function()
{
	if (this.div.parentNode) this.div.parentNode.removeChild(this.div);
}
//---------------------------------------------------------------------------
FMenu.prototype.CleanupDom = function()
{
	if (!this.div) return;
	this.RemoveDom();
	// Free memory ... ?
	
}
//---------------------------------------------------------------------------
FMenu.prototype.AddHeader = function(Caption)
{
	// Create elements
	var tr = document.createElement("tr");
	var th = document.createElement("th");
	var txt = document.createTextNode(Caption);
	// Set element parameters
	th.className = "FMenu";
	th.setAttribute("noWrap", "true");
	// Append elements
	th.appendChild(txt);
	tr.appendChild(th);
	this.tbody.appendChild(tr);
}
//---------------------------------------------------------------------------
FMenu.prototype.AddItem = function(Caption, Url)
{
	// Create elements
	var tr = document.createElement("tr");
	var td = document.createElement("td");
	var txt = document.createTextNode(Caption);
	// Set element parameters
	td.className = "FMenuItem";
	td.setAttribute("noWrap", "true");
	td.Menu = this;
	td.Url = Url;
	td.SubMenu = new FMenu(this.ObjectName + "_" + this.ItemCount++, this);
	td.SubMenu.ParentTd = td;
	// Set element events
	td.onmouseover = function() { this.Menu.OnItemOver(this); }
	td.onmouseout = function() { this.Menu.OnItemOut(this); }
	if (Url) td.onclick = function() { document.location.href = this.Url; }
	// Append elements
	td.appendChild(txt);
	tr.appendChild(td);
	this.tbody.appendChild(tr);
	// Set DefaultIcon and pass parameter
	if (this.DefaultIcon)
	{
		td.SubMenu.SetIcon(this.DefaultIcon);
		td.SubMenu.DefaultIcon = this.DefaultIcon;
	}
	return td.SubMenu;
}
//---------------------------------------------------------------------------
FMenu.prototype.OnItemOver = function(td)
{
	if (td.Menu.Parent)
		td.Menu.Parent.CancelAutoHideSubMenu();
	td.className = "FMenuItemOver";
	if (td.SubMenu.ItemCount) td.Menu.ShowSubMenu(td.SubMenu);
}
//---------------------------------------------------------------------------
FMenu.prototype.OnItemOut = function(td)
{
	if (td.Menu.Parent)
		td.Menu.Parent.AutoHideSubMenu();
	td.className = "FMenuItem";
	if (td.SubMenu.ItemCount) td.Menu.AutoHideSubMenu(td.SubMenu);
}
//---------------------------------------------------------------------------
FMenu.prototype.SetIcon = function(IconUrl)
{
	if (!this.Parent) return;
	var img = this.ParentTd.firstChild;
	if (img.nodeName.toLowerCase != "img")
	{
		img = document.createElement("img");
		// Append element
		var txt = this.ParentTd.firstChild;
		this.ParentTd.replaceChild(img, txt);
		this.ParentTd.appendChild(txt);
		txt.nodeValue = " " + txt.nodeValue;
	}
	// Set element parameters
	img.setAttribute("src", IconUrl);
	img.setAttribute("align", "absmiddle");
}
//---------------------------------------------------------------------------
FMenu.prototype.GetPosition = function(el)
{
	if (el.offsetParent)
	{
		var PosX = 0, PosY = 0;
		while (el.offsetParent)
		{
			PosX += el.offsetLeft;
			PosY += el.offsetTop;
			el = el.offsetParent;
		}
		return [PosX, PosY];
	} else {
		return [el.x, el.y ];
	}
}
//---------------------------------------------------------------------------
FMenu.prototype.SetVisible = function(Visible)
{
	this.div.style.display = Visible?"block":"none";
}
//---------------------------------------------------------------------------
FMenu.prototype.ShowSubMenu = function(SubMenu)
{
	if (SubMenu == this.CurrentSubMenu)
	{	// Submenu already visible
		if (this.SubMenuHideTimeout)
		{
			clearTimeout(this.SubMenuHideTimeout);
			this.SubMenuHideTimeout = null;
		}
	} else {
		this.HideSubMenu();
		var Pos = this.GetPosition(SubMenu.ParentTd.parentNode);
		// Cover components like <select>
		SubMenu.div.style.left = Pos[0] + SubMenu.ParentTd.clientWidth - 3;
		SubMenu.div.style.top = Pos[1] + 3;
		this.CurrentSubMenu = SubMenu;
		SubMenu.SetVisible(true);
		this.ShowIFrame(SubMenu.div);
	}
}
//---------------------------------------------------------------------------
FMenu.prototype.HideSubMenu = function()
{
	if (this.CurrentSubMenu)
	{
		if (this.SubMenuHideTimeout)
		{
			clearTimeout(this.SubMenuHideTimeout);
			this.SubMenuHideTimeout = null;
		}
		this.CurrentSubMenu.HideSubMenu();
		this.HideIFrame();
		this.CurrentSubMenu.SetVisible(false);
		this.CurrentSubMenu = null;
	}
}
//---------------------------------------------------------------------------
FMenu.prototype.AutoHideSubMenu = function()
{
	this.CancelAutoHideSubMenu();
	if (this.CurrentSubMenu)
		this.SubMenuHideTimeout = setTimeout(this.ObjectName + ".Menu.HideSubMenu();", 500);
	if (this.Parent) this.Parent.AutoHideSubMenu();
}
//---------------------------------------------------------------------------
FMenu.prototype.CancelAutoHideSubMenu = function()
{
	if (this.SubMenuHideTimeout)
	{
		clearTimeout(this.SubMenuHideTimeout);
		this.SubMenuHideTimeout = null;
	}
	if (this.Parent) this.Parent.CancelAutoHideSubMenu();
}
//---------------------------------------------------------------------------
FMenu.prototype.ShowIFrame = function(el)
{
	if (!this.iframe)
	{
		this.iframe = document.createElement("iframe");
		this.iframe.style.position = "absolute";
		this.iframe.setAttribute("scrolling", "no");
		this.iframe.setAttribute("frameBorder", "no");
		this.div.parentNode.appendChild(this.iframe);
	}
	
	// Search path depth and set zIndex
	var a = this.ObjectName.split("_");	
	this.iframe.style.zIndex = 1000 + a.length * 2;
	el.style.zIndex = 1001 + a.length * 2;
	// Cursor hack
	var input = eval(a[0] + "_hack");
	input.style.display = "block";
	input.focus();
	input.style.display = "none";
	delete a;

	// Position iframe
	try
	{
		this.iframe.style.left = el.style.left;
		this.iframe.style.top = el.style.top;
		this.iframe.style.width = el.clientWidth;
		this.iframe.style.height = el.clientHeight;
		this.iframe.style.display = "block";
	}
	catch (e)
	{
	}
}
//---------------------------------------------------------------------------
FMenu.prototype.HideIFrame = function()
{
	this.iframe.style.display = "none";
}
//---------------------------------------------------------------------------

