var PopupMenu;
PopupMenu.Menus = [];
PopupMenu.hideDelay = 300;
function getElementPosition(element)
{
	var offsetTrail = element;
	var offsetLeft = 0;
	var offsetTop = 0;
	while (offsetTrail)
	{
		offsetLeft += offsetTrail.offsetLeft;
		offsetTop += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined")
	{
		offsetLeft += document.body.leftMargin+"px";
		offsetTop += document.body.topMargin+"px";
	}
	return {left:offsetLeft, top:offsetTop};
}

function ShowContextMenu(item, id)
{
	var menu = document.getElementById(id);
	if (menu)
	{	
		var position = getElementPosition(item);
		menu.style.top = position.top + 15+"px";
		menu.style.left = position.left+"px";
		new PopupMenu(id, true);
		PopupMenu.showMenu(id);
	}
	return false;
}

function PopupMenu(id, autoHide)
{
	this.id = id;
	this.hideTimer = 0;
	this.autoHide = autoHide;
	this.menu = null;
	PopupMenu.Menus[id] = this;

	this.init = function()
	{
		this.menu = document.getElementById(this.id);
		if (!this.menu)
		{
			window.setTimeout('PopupMenu.Menus["' + this.id + '"].init()', 100);
			return;
		}
		if (this.autoHide)
		{
			this.menu.onmouseover = new Function('PopupMenu.showMenu("' + this.id + '")');
			this.menu.onmouseout = new Function('PopupMenu.hideMenu("' + this.id + '")');
		}
	}

	this.show = function()
	{
		if (this.menu) this.menu.style.display = 'block';
	}

	this.hide = function()
	{
		if (this.menu)
		{
			if (this.hideTimer)
			{
				window.clearTimeout(this.hideTimer);
				this.hideTimer = 0;
			}
			this.menu.style.display = 'none';
		}
	}

	this.init();
}

PopupMenu.showMenu = function(id)
{
	var activeMenu = PopupMenu.Menus[id];
	if (activeMenu)
	{
		for (var menu in PopupMenu.Menus)
		{
			if (menu != id) PopupMenu.Menus[menu].hide();
		}

		if (activeMenu.hideTimer)
		{
			window.clearTimeout(activeMenu.hideTimer);
			activeMenu.hideTimer = 0;
		}

		activeMenu.show();
	}
}

PopupMenu.hideMenu = function(id)
{
	var activeMenu = PopupMenu.Menus[id];
	if (activeMenu)
	{
		if (activeMenu.hideTimer)
		{
			window.clearTimeout(activeMenu.hideTimer);
		}
		activeMenu.hideTimer = window.setTimeout('PopupMenu.Menus["' + id + '"].hide()', PopupMenu.hideDelay);
	}
}

PopupMenu.hideAll = function()
{
	for (var menu in PopupMenu.Menus)
	{
		PopupMenu.Menus[menu].hide();
	}
}

function MenuItemFocus(item)
{
	item.className = 'popup-menu-item-hover';
}

function MenuItemBlurr(item)
{
	item.className = 'popup-menu-item';
}
