/*
	E-commerce site version 4.0.0
	-----------------------------
	This context menu file requires a few prereq functions, after that you may simply drop it in place and add the 
	following code in your document:
	
		<script language="JavaScript">
		//assign context menu to id(s)
		try{
			AssignMenu('^(d)*node_', 'menu1'); //assigns all ids like dnode_1, dnode_2, node_5 to menu1
		}catch(e){ /* alert if you desire * / }
		</script>
	
	TODO:
	better understanding of load and onload so no conflicts with other pages
	onclick events need integrated as well

*/
var _CONTEXTMENUS_VERSION_=4.00;
var _CONTEXTMENUS_INSTANCE=1;

var menuType='regexp'; //must be 'normal', or 'regexp', default is regexp
var menuIDName='';
var hm_cxl=0;
var hm_cxlseq=0;
var option_hm_cxl=0;
var menuLvl=new Array();
var menuMap=new Array();
var rMenuMap=new Array();	//private, make sure to use public AssignMenu function.
var menuAlign=new Array();
//holds the status message during mouseovers, initially set to blank
var statusBuffer='';
//last source object:
var rb_cm_lastObject=false;
//last selected text:
var rb_strLastSelection="";

AddOnloadCommand("document.oncontextmenu = showmenuie5;");
AddOnloadCommand("document.body.onclick = hidemenuie5;");

function AssignMenu(strRegExpId, strMenuName){
	rMenuMap[strRegExpId] = strMenuName;
	menuAlign[strRegExpId] = "mouse";
}

//redundant - this is declared in globals
function AssignMenu(strRegExpId, strMenuName){
	rMenuMap[strRegExpId] = strMenuName;
	menuAlign[strRegExpId] = "mouse";
}
function LastSourceObject(){ return rb_cm_lastObject; }
function GetLastSelection(){ return rb_strLastSelection; }
function showmenuie5(event, lvl) {
	if(typeof event == "undefined") event = window.event;
	//2007-03-06: allows user to hold shift key and bypass my context menu
	if(event.shiftKey){
		hidemenuie5(event, 0);
		return true;
	}
	if(typeof lvl == "undefined")lvl = 0;
	var mynode=0;
	if(GetSourceElement(event).getAttribute("id")) {
		idLvl = 1;
		mynode = GetSourceElement(event);
	} else if(GetSourceElement(event).parentNode.getAttribute("id")) {
		idLvl = 2;
		mynode = GetSourceElement(event).parentNode;
	} else if(GetSourceElement(event).parentNode.parentNode.getAttribute("id")) {
		idLvl = 3;
		mynode = GetSourceElement(event).parentNode.parentNode;
	} else {
		if (!event.altKey) hidemenuie5(event, 0);
		return true;
	}
	if(in_array(mynode.id, menuLvl)) {
		alert('Use the left click button to select an option');
		return false;
		//unset the array elements -- not developed
		//handle the click -- which may open another submenu for that matter -- not developed
		//return false to stop IE context menu -- not developed
		return false;
	}
	//search for id based on normal or regex method, see arrays
	//reset menuIDName: (please do not remove):
	var menuIDName = false;
	if(menuType=='normal'){
		menuIDName=menuMap[mynode.id];
		menuAlignment=menuAlign[mynode.id];
	}else if(menuType=='regexp'){
		for(x in rMenuMap){
			eval( 'reg=/'+x+'/;' );
			if(mynode.id.match(reg)){
				menuAlignment=menuAlign[x];
				menuIDName=rMenuMap[x];
				break;
			}
		}
	}
	if(!menuIDName){
		//hide all menus
		//prevent endless loop:
		if (!event.altKey) hidemenuie5(event, 0);
		return true;
	}
	/*
	eval( 'if(typeof '+menuIDName+'=="undefined"){alert("The RelateBase context menu '+menuIDName+' is not defined");r=1;}else{r=0;}' );
	if(r==1)return false;
	eval('x='+menuIDName+';');
	*/
	if (typeof menuIDName =="undefined") {
		alert("The RelateBase context menu is not defined");
		return false;
	}
	var objMenu=document.getElementById(menuIDName);
	if (!objMenu){
		alert("The RelateBase context menu " + menuIDName + " is not defined");
		return false;
	}
	//hide all other menus
	//prevent endless loop:
	if (!event.altKey) hidemenuie5(event, lvl);
	if(objMenu.getAttribute("precalculated")) eval(objMenu.getAttribute("precalculated"));
	//2004-10-15: Introduced by Yahav - store last source object and last selection for later use
	rb_cm_lastObject = GetSourceElement(event);
	rb_strLastSelection = (typeof document.selection != "undefined")?document.selection.createRange().text:"";
	//open the given menu
	positionmenueie5(event, objMenu, menuAlignment, mynode);
	//build the given menu into menuLvl array
	if(sizeof(menuLvl)==0) menuLvl[1]=objMenu.id;
	if(sizeof(menuLvl)>0){
		if(menuLvl[sizeof(menuLvl)]!==objMenu.id){
			var e=sizeof(menuLvl)+1;
			menuLvl[e]=objMenu.id;
		}
	}
	return false;
}
function close_menus(){
	if(sizeof(menuLvl)>0){
		//close the top level menu
		g(menuLvl[sizeof(menuLvl)]).style.visibility='hidden';
		//unset the array element
		delete_element(menuLvl,sizeof(menuLvl));
	}
}
function positionmenueie5(event, x, menAlign, mynode){
	if(typeof menuAligment=='undefined') var menAlign='mouse';
	var bw=document.body.clientWidth;
	var bh=document.body.clientHeight;
	var sl=document.body.scrollLeft;
	var st=document.body.scrollTop;
	switch(menuAlignment){
		case 'mouse':
			var rtedge = bw-event.clientX;
			var btmedge = bh-event.clientY;
			if (rtedge < x.offsetWidth){
				x.style.left = (sl + event.clientX - x.offsetWidth)+'px';
			}else{
				x.style.left = (sl + event.clientX) +'px';
			}
			if (btmedge < x.offsetHeight){
				x.style.top = (st + event.clientY - x.offsetHeight)+'px';
			}else{
				x.style.top = (st + event.clientY)+'px';
			}
		break;
		case 'topleftalign':
			//width and height of div
			//mynode is global from showmenuie5 -- good enough for now
			x.style.top = (mynode.offsetTop - x.offsetHeight)+'px';
			x.style.left= (mynode.offsetLeft)+'px';
		break;
	}
	x.style.visibility = "visible";
}
function hidemenuie5(event, lvl) {
	if (typeof event == "undefined")	event = window.event;
	//wait... maybe user pressed ALT and click? if so, activate menu:
	if (event.altKey){
		showmenuie5(event, lvl);
		return;
	}
	if(typeof lvl=='undefined') lvl=0;
	//added by Yahav, 28/08/2004 - reason: don't hide menu when clicking disabled menu options of menu line breaks
	if(GetSourceElement(event).disabled || GetSourceElement(event).tagName == "hr") return false;
	// this was a patch
	if(hm_cxl || hm_cxlseq==1){
		hm_cxl=0;
	}else{
		var gx='';
		for(var x in menuLvl){
			if(x>lvl){
				//old method - eval( menuLvl[x]+'.style.visibility="hidden";' );
				g(menuLvl[x]).style.visibility="hidden";
				gx=x+','+gx;
			}
		}
		gx= gx.substring(0,gx.length-1);
		var h=gx.split(',');
		for(var x in h){
			if(h[x]>lvl){
				delete_element(menuLvl,h[x]);
			}
		}
	}
	if(hm_cxlseq>0)hm_cxlseq--;
	if(lvl>0){
			hm_cxl=1;
	}
}
function highlightie5(event) {
	if (typeof event == "undefined")
		event = window.event;
	if(!GetSourceElement(event).getAttribute('disabled')){
		if (GetSourceElement(event).className == "menuitems") {
			GetSourceElement(event).style.backgroundColor = "highlight";
			GetSourceElement(event).style.color = "#FFFFFF";
		}
	}
	if(GetSourceElement(event).getAttribute('status')){
		statusBuffer=window.status;
		window.status=GetSourceElement(event).getAttribute('status');
	}
}
function lowlightie5(event) {
	if (typeof event == "undefined")
		event = window.event;
	if(!GetSourceElement(event).getAttribute("disabled")){
		if (GetSourceElement(event).className == "menuitems") {
			GetSourceElement(event).style.backgroundColor = "";
			GetSourceElement(event).style.color = "#000000";
		}
		window.status = statusBuffer;
	}
}
function executemenuie5(event) {
	if (typeof event == "undefined")
		event = window.event;
	if(!GetSourceElement(event).getAttribute('disabled')){
		if(GetSourceElement(event).getAttribute('command')){
			try{
				eval(GetSourceElement(event).getAttribute('command'));
			}catch(e){
				var strMsg=e.description||e.message;
				alert('Function to execute: '+GetSourceElement(event).getAttribute('command')+'\nError: '+strMsg);
			}
			if(option_hm_cxl==1){
				option_hm_cxl=0;
			}else{
				hidemenuie5(event);
			}
			return false;
		}
	}else{
		//we need to interrupt the closure of the menu
	}
}