var ContextMenu = function() { }

ContextMenu._menu = null;
ContextMenu._menuElement = null;
ContextMenu._menuStyle = { 'x' : '', 'y' : '' };
ContextMenu._max = { 'x' : 0, 'y' : 0};

ContextMenu.setup = function(menuName)
{
	if (document.all != undefined && document.getElementById != undefined && window.opera == undefined)
	{
		ContextMenu.IE = true;
	}
	else if (document.all == undefined && document.getElementById != undefined && window.opera == undefined)
	{
		ContextMenu.FF = true;
	}
	else if (document.all != undefined && document.getElementById != undefined && window.opera != undefined)
	{
		ContextMenu.OP = true;
	}

	if (ContextMenu.IE || ContextMenu.FF)
	{
		document.oncontextmenu = ContextMenu._show;
		document.onclick = ContextMenu._hide;
	}

	ContextMenu._max.x = (document.width + window.scrollMaxX);
	ContextMenu._max.y = (document.height + window.scrollMaxY);

	ContextMenu._menu = menuName;
}

ContextMenu.KeepShown = function()
{
	ContextMenu._menuElement = document.getElementById(this._menu);
	setTimeout("ContextMenu._menuElement.style.display = 'block';", 10);

	return false;
}

ContextMenu._show = function(e)
{
	if (typeof(SubMenu) != "undefined")
	{
		if (SubMenu._menuElement != null)
		{
			if (SubMenu._menuElement.style.display == 'block')
				return false;
		}
	}

	var m = ContextMenu._getMousePosition(e);
	var s = ContextMenu._getScrollPosition();

	ContextMenu._hide();

	ContextMenu._menuElement = document.getElementById(ContextMenu._menu);
	/* Like to add this, but it's FireFox specific, need someone to sudject code to make this work on all browsers...
	Currently other browsers get the un-impressive Menu-is-anywhere behavior.. :| */
	if (ContextMenu.FF == true)
	{
		var Elem = ContextMenu._menuElement;

		Elem.style.display = 'block';

		// Set up the menu to never become hidden by scroll-bars when clicked near the edge....
		if ((m.x + s.x + Elem.clientWidth) >= ContextMenu._max.x)
			Elem.style.left = ContextMenu._max.x - Elem.clientWidth + 'px';
		else
			Elem.style.left = m.x + s.x + 'px';

		if ((m.y + s.y + Elem.clientHeight) >= ContextMenu._max.y)
			Elem.style.top = ContextMenu._max.y - Elem.clientHeight + 'px';
		else
			Elem.style.top = m.y + s.y + 'px';

		ContextMenu._menuStyle.x = Elem.style.left;
		ContextMenu._menuStyle.y = Elem.style.top;

		//alert("Menu position Y (un-transformed): " + (m.y + s.y) + "\nMax menu position Y: " + (document.height + window.scrollMaxY) +
		//	"\nMenu position X (un-transformed): " + (m.x + s.x) + "\nMax menu position X: " + (document.width + window.scrollMaxX));
	}
	else
	{
		ContextMenu._menuElement.style.left = m.x + s.x + 'px';
		ContextMenu._menuElement.style.top = m.y + s.y + 'px';
		ContextMenu._menuElement.style.display = 'block';
	}

	return false;
}

ContextMenu._hide = function()
{
	if (ContextMenu._menuElement)
	{
		ContextMenu._menuElement.style.display = 'none';
	}
}

ContextMenu._getMousePosition = function(e)
{
	e = e ? e : window.event;
	var position =
	{
		'x' : e.clientX,
		'y' : e.clientY
	}

	return position;
}

ContextMenu._getScrollPosition = function()
{
	var x = 0;
	var y = 0;

	if (typeof(window.pageYOffset) == 'number')
	{
		x = window.pageXOffset;
		y = window.pageYOffset;
	}
	else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
	{
		x = document.documentElement.scrollLeft;
		y = document.documentElement.scrollTop;
	}
	else if (document.body && (document.body.scrollLeft || document.body.scrollTop))
	{
		x = document.body.scrollLeft;
		y = document.body.scrollTop;
	}

	var position =
	{
		'x' : x,
		'y' : y
	}

	return position;
}