

function _contextMenu_setMenuStyle(e)
{
	with (e.style) {
		cursor = "default";
		position = "absolute";
		backgroundColor = "menu";
		display = "none";
		border = "2 outset buttonhighlight";
		paddingBottom =  "3px";
		zIndex = "99";
	}
}

function _contextMenu_setMenuItemStyle(e)
{
	with (e.style) {
		cursor = "default";
//		font = "menu";
		fontFamily = "Tahoma,Arial";
		fontSize = "8pt";
		paddingLeft = "10px";
		paddingRight =  "8px";
		paddingTop =  "2px";
		paddingBottom =  "2px";
		color = "black";
		backgroundColor = "menu";	
		zIndex = "99";
	}
	
}


function contextMenu_show(o, x, y) 
{
   menu = this.m_menu;
	
	//Find out how close the mouse is to the corner of the window
	var rightedge=document.body.clientWidth-x
	var bottomedge=document.body.clientHeight-y
	
	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge<menu.offsetWidth)
	//move the horizontal position of the menu to the left by it's width
	menu.style.left=document.body.scrollLeft+x-menu.offsetWidth
	else
	//position the horizontal position of the menu where the mouse was clicked
	menu.style.left=document.body.scrollLeft+x
	
	//same concept with the vertical position
	if (bottomedge<menu.offsetHeight)
	menu.style.top=document.body.scrollTop+y-menu.offsetHeight
	else
	menu.style.top=document.body.scrollTop+y

   
   menu.style.display="block";
   menu.setCapture(); //so a click anywhere will come here

   menu._contextMenuSubject = o;

   return false;
}


function _contextMenu_switchMenu() 
{   
	if (event.srcElement.id==null) return;
	if (event.srcElement.id=="") return;
	
   	with (event.srcElement.style) {
	   if (backgroundColor == "highlight") {
			backgroundColor = "menu";
			color = "black";
	   } 
	   else if (backgroundColor == "menu") {
			backgroundColor = "highlight";
			color = "white";
	   }
	}
}

function _contextMenu_clickMenu() 
{ 
	var menu = this;
	menu.style.display="none";
	document.releaseCapture();

	var itm = event.srcElement
	if (itm.parentElement!=menu) return;

	var action = itm._contextMenuAction
	if (action) action(menu._contextMenuSubject)		
}

function contextMenu_addItem(label, action) 
{ 
	var itm = document.createElement("div");
	itm.id = "CXM"+document.uniqueID;
	_contextMenu_setMenuItemStyle(itm);
	
	itm.innerText = label;
	this.m_menu.appendChild(itm);
	itm._contextMenuAction = action;
}


function contextMenu_addSeparator() 
{ 
	var itm = document.createElement("<div style='margin-top:3px;margin-bottom:3px;border-bottom:1px buttonhighlight solid;border-top:1px buttonshadow solid'>");
//	_contextMenu_setMenuItemStyle(itm);
	this.m_menu.appendChild(itm);
}


function CContextMenu()
{
	this.m_menu = document.createElement('div');
	_contextMenu_setMenuStyle(this.m_menu);
	
	document.body.appendChild(this.m_menu);
	this.m_actions = new Array();	
	
	this.addItem = contextMenu_addItem;
	this.addSeparator =  contextMenu_addSeparator
	this.show = contextMenu_show;
	this.clickMenu = _contextMenu_clickMenu
	
	this.m_menu.onclick = _contextMenu_clickMenu;
	this.m_menu.onmouseover = _contextMenu_switchMenu;
	this.m_menu.onmouseout = _contextMenu_switchMenu;

	
}

