// Copyright © 2002 Microsoft Corporation.  All rights reserved.

var omenu = null;
var omenuitem = null;

function initContextMenu(menuid, menuitemid)
{
	if (null == menuid  || null == menuitemid)
		return false;

	omenu = menuid;
	omenuitem = menuitemid;
	
	// attach event handlers to context menu
	omenu.onclick = hidecontextmenu;
	omenu.onblur = hidecontextmenu;

	// attach event handlers to each context menu item;
	// if length is undefined we have only one menu item,
	// otherwise we have an array of items
	if (!omenuitem.length)
	{
		omenuitem.onmouseover=highlightmenuitem;
		omenuitem.onmouseout=unhighlightmenuitem;
	}
	else
	{
		for (var i = 0; i < omenuitem.length; i++)
		{
			omenuitem[i].onmouseover=highlightmenuitem;
			omenuitem[i].onmouseout=unhighlightmenuitem;
		}
	}

	return true;
}

function showcontextmenu() 
{
	var right = document.body.clientWidth - event.clientX;
	var bottom = document.body.clientHeight - event.clientY;

	//if (right < idcontextmenu.offsetWidth)
	//	omenu.style.left = document.body.scrollLeft + event.clientX - omenu.offsetWidth;
	//else
		omenu.style.left = document.body.scrollLeft + event.clientX;

	//if (bottom < idcontextmenu.offsetHeight)
	//	omenu.style.top = document.body.scrollTop + event.clientY - omenu.offsetHeight;
	//else
		omenu.style.top = document.body.scrollTop + event.clientY;

	omenu.style.visibility = "visible";

	return false;
}

function hidecontextmenu() 
{
	omenu.style.visibility = "hidden";
	omenu.srcElement = "";
}

function highlightmenuitem() 
{
	if (event.srcElement.className == "contextmenuitem") 
	{
		event.srcElement.style.backgroundColor = "highlight";
		event.srcElement.style.color = "white";
	}
}

function unhighlightmenuitem() 
{
	if (event.srcElement.className == "contextmenuitem") 
	{
		event.srcElement.style.backgroundColor = "";
		event.srcElement.style.color = "black";
   	}
}