//set this variable to 1 if you wish the URLs of the highlighted menu to be displayed in the status bar
var display_url=0;

var ie5=document.all&&document.getElementById;
var ns6=document.getElementById&&!document.all;

var objSourceElement = null;
var aryMenus = Array();

function attachMenu(strMenuId, fnInContextFunc) {
	
	aryMenus[aryMenus.length] = {id:strMenuId, fnInContext:fnInContextFunc};

}

function attachToNode(strMenuId, strNodeId) {

	aryMenus[aryMenus.length] = {id:strMenuId, strNode:strNodeId};

}

function isChild(varChild, strParentId) {


	if ( varChild == document.getElementById(strParentId) ) return true;
	
	if ( varChild.parentNode ) {
		return isChild(varChild.parentNode, strParentId);
	}

	return false;
	
}

function showmenuie5(e){

	objSourceElement = ns6 ? e.target : event.srcElement;

	oMenu = null;

	for(i=0;i<aryMenus.length;i++) {

//alert("here1 el.id: "+objSourceElement.id+", strNode: "+aryMenus[i].strNode+", "+isChild(objSourceElement,aryMenus[i].strNode));
		
//		if ( aryMenus[i].fnInContext(objSourceElement) ) {
		if ( isChild(objSourceElement,aryMenus[i].strNode) ) {
			oMenu = document.getElementById(aryMenus[i].id);
		
			break;	
		}
	}

	if ( !oMenu ) return true;

	//Find out how close the mouse is to the corner of the window
	var rightedge = ie5 ? document.body.clientWidth-event.clientX : 
							window.innerWidth-e.clientX;
	var bottomedge = ie5 ? document.body.clientHeight-event.clientY : 
							window.innerHeight-e.clientY;
	
	//if the horizontal distance isn't enough to accomodate the width of the context menu
	if (rightedge<oMenu.offsetWidth)
		//move the horizontal position of the menu to the left by it's width
		oMenu.style.left= ie5 ? (document.body.scrollLeft+event.clientX-oMenu.offsetWidth) +"px" : 
									(window.pageXOffset+e.clientX-oMenu.offsetWidth) +"px";
	else
		//position the horizontal position of the menu where the mouse was clicked
		oMenu.style.left = ie5 ? (document.body.scrollLeft+event.clientX) +"px" : 
									(window.pageXOffset+e.clientX) +"px";
	
	//same concept with the vertical position
	if (bottomedge<oMenu.offsetHeight)
		oMenu.style.top = ie5 ? (document.body.scrollTop+event.clientY-oMenu.offsetHeight) +"px" : 
									(window.pageYOffset+e.clientY-oMenu.offsetHeight) +"px";
	else
		oMenu.style.top = ie5 ? (document.body.scrollTop+event.clientY) +"px" : 
									(window.pageYOffset+e.clientY) +"px";
	
	oMenu.style.display="";

	return false;
}

function hidemenuie5(e){
	
	for(i=0;i<aryMenus.length;i++) {
		oMenu = document.getElementById(aryMenus[i].id);
		oMenu.style.display="none";
	}
}

function highlightie5(e){

	var firingobj = ie5 ? event.srcElement : e.target;
	if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems"){
		if (ns6&&firingobj.parentNode.className=="menuitems") firingobj=firingobj.parentNode //up one node
		firingobj.style.backgroundColor="highlight"
		firingobj.style.color="white"
		if (display_url==1)
			window.status=firingobj.getAttribute("url");
	}
}

function lowlightie5(e){

	var firingobj=ie5? event.srcElement : e.target;
	if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems") {
		if (ns6&&firingobj.parentNode.className=="menuitems") 
			firingobj=firingobj.parentNode; //up one node
		firingobj.style.backgroundColor="";
		firingobj.style.color="black";
		window.status='';
	}
}

function jumptoie5(e){

	var firingobj=ie5? event.srcElement : e.target;

	if (firingobj.className=="menuitems"||ns6&&firingobj.parentNode.className=="menuitems") {
		if (ns6&&firingobj.parentNode.className=="menuitems") 
			firingobj=firingobj.parentNode;

		if (firingobj.getAttribute("target"))
			window.open(firingobj.getAttribute("url"),firingobj.getAttribute("target"));
		else {
			strURL = firingobj.getAttribute("url");

			if ( !strURL ) return;

			if ( strURL.match("javascript:") ) {
				eval(strURL);
			} else {
				window.location = strURL;
			}
		}
	}
}

if (ie5||ns6) {

	document.oncontextmenu=showmenuie5;
	document.onclick=hidemenuie5;

}