var isIE = navigator.userAgent.match(/MSIE/);

var menus =
{
	current : 0,
	timer : 0,
	show : function(link, menu)
	{
		if(menus.current && menus.current != menu)
		{
			if(menus.timer) menus.clearTimer();
			menus.hide(menus.current);
		}
		menus.current = menu;
		var coords = menus.findPos(link);
		var ul = $('menu-'+menu);
		ul.style.position = 'absolute';
		ul.style.top = (coords[1]+79) + 'px';
		ul.style.left = coords[0] + 'px';
		ul.style.display = 'block';
		ul.style.zIndex = 10;
		menus.setBehaviour(ul);
		menus.startListener(menu);
		ul.onmouseover = function()
		{
			menus.clearTimer(menu);
		}
	},
	setBehaviour : function(ul)
	{
		for(var i = 0; i < ul.childNodes.length; i++)
		{
			if(ul.childNodes[i].nodeName.toLowerCase() == 'li')
			{
				var li = ul.childNodes[i];
				li.onmouseover = function()
				{
					for(var i = 0; i < this.childNodes.length; i++)
					{
						if(this.childNodes[i].nodeName.toLowerCase() == 'ul')
						{
							if(this.childNodes[i].style.display == 'block') return;
						}
					}
					this.style.backgroundColor = '#8E8B89';
					for(var i = 0; i < this.parentNode.childNodes.length; i++)
					{
						if(this.parentNode.childNodes[i].nodeName.toLowerCase() == 'li')
						{
							menus.closeSubMenu(this.parentNode.childNodes[i]);
						}
					}
					for(var i = 0; i < this.childNodes.length; i++)
					{
						if(this.childNodes[i].nodeName.toLowerCase() == 'ul')
						{
							this.childNodes[i].style.display = 'block';
							var maxWidth = document.body.offsetWidth;
							var pos = menus.findPos(this.childNodes[i]);
							var test = (this.parentNode.parentNode.nodeName.toLowerCase() == 'body' && pos[0]+4*150 > maxWidth) || this.parentNode.style.left == '-150px';
							if(test)
							{
								this.childNodes[i].style.left = '-150px';
							}
						}
					}
				}
				li.onmouseout = function()
				{
					this.style.backgroundColor = '#C4BFBB';
				}
				for(var j = 0; j < li.childNodes.length; j++)
				{
					if(li.childNodes[j].nodeName.toLowerCase() == 'ul')
					{
						menus.setBehaviour(li.childNodes[j]);
					}
				}
			}
		}
	},
	closeSubMenu : function(li)
	{
		for(var i = 0; i < li.childNodes.length; i++)
		{
			if(li.childNodes[i].nodeName.toLowerCase() == 'ul')
			{
				var ul = li.childNodes[i];
				for(var j = 0; j < ul.childNodes.length; j++)
				{
					if(ul.childNodes[j].nodeName.toLowerCase() == 'li')
					{
						menus.closeSubMenu(ul.childNodes[j]);
					}
				}
				ul.style.display = 'none';
			}
		}
		return true;
	},
	findPos : function(obj)
	{
		var curleft = curtop = 0;
		if(obj.offsetParent)
		{
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while (obj = obj.offsetParent)
			{
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
		return [curleft,curtop];
	},
	startListener : function(menu)
	{
		document.body.onmouseover = function(e)
		{
			if(isIE)
			{
				var e = window.event;
				var o = e.srcElement;
			}
			else
			{
				var o = e.target;
			}
			if(!menus.timer)
			{
				var h = 1;
				while(o.nodeName.toLowerCase() != 'body')
				{
					if(o.id == 'menu-'+menu) h = 0;
					o = o.parentNode;
				}
				if(h)
				{
					menus.timer = setTimeout('menus.hide(\'' + menu + '\')', 1000);
				}
			}
		}
	},
	clearTimer : function()
	{
		if(menus.timer)
		{
			clearTimeout(menus.timer);
			menus.timer = 0;
		}
	},
	hide : function(menu)
	{
		var ul = $('menu-'+menu);
		for(var i = 0; i < ul.childNodes.length; i++)
		{
			if(ul.childNodes[i].nodeName.toLowerCase() == 'li')
			{
				if(menus.closeSubMenu(ul.childNodes[i])) continue;
			}
		}
		ul.style.display = 'none';
	}
}

function $(id)
{
	return document.getElementById(id);
}