var CM_Color_On="#C0C0C0";
var CM_Color_OnFg="#000000";
var CM_Color_Bkg="#FFFFFF";
var CM_Color_Fg="#000000";
var CM_Color_Off_Fg="#C0C0C0";
var CM_ie5;
var CM_ns6;
var CM_menuobj;
var CM_control4menu;

function InitCM(){
	CM_ie5=document.getElementById&&document.all;
	CM_ns6=document.getElementById&&!document.all;
	if(CM_ie5||CM_ns6) { 
		document.onclick=CloseCM;
	};
	var l = document.styleSheets.length;
	for (i=0;i<l;i++){
		var h = document.styleSheets[i].href;
		//var m = h.match(/contextmenu.css$/ig);
		//if (m!=null) {
		var rls;
			if (CM_ie5) {	
				rls = document.styleSheets[i].rules;
			}else{
				rls = document.styleSheets[i].cssRules;
			}
			var rl = rls.length;
			for (j=0;j<rl;j++){
				var rn = rls[j].selectorText;
				if (rn.match(/^.cm_menu$/ig)){
					CM_Color_Bkg =rls[j].style.backgroundColor;
					CM_Color_OnFg =rls[j].style.color;
					CM_Color_Fg =rls[j].style.color;
				}
				if (rn.match(/^.cm_item_sel$/ig)){
					CM_Color_On =rls[j].style.color;
					CM_Color_Off_Fg =rls[j].style.color;
				}			
			}
		//}
	}
}


function OpenCM(c,e){
	CM_control4menu=c;
	e.cancelBubble = true;
	e.returnValue=false;
	var s=0;
	try{
		var mymenu = c.getAttribute('ctxtmenu');
		try{CM_menuobj.style.visibility='hidden';}catch(ex){}
		CM_menuobj=document.getElementById(mymenu);s=3;
		CM_menuobj.style.display=''
		var rights = c.getAttribute('ctxtmenu_rights');s=4;
		if (rights==undefined) rights="";s=5;
		var remsz1=CM_ie5?document.body.clientWidth-event.clientX:window.innerWidth-e.clientX;
		var remsz2=CM_ie5?document.body.clientHeight-event.clientY:window.innerHeight-e.clientY;s=6;
			
		if (remsz1<CM_menuobj.offsetWidth){
			s=61;
			var l =CM_ie5? document.body.scrollLeft+event.clientX-CM_menuobj.offsetWidth:window.pageXOffset+e.clientX-CM_menuobj.offsetWidth;
			if (l<0) {l=0;}
			CM_menuobj.style.left = l;
		}else{
			s=62;
			CM_menuobj.style.left=CM_ie5? document.body.scrollLeft+event.clientX:window.pageXOffset+e.clientX;
		}

		var allmenuitems=CM_menuobj.getElementsByTagName("div");s=7;

		if (remsz2<CM_menuobj.offsetHeight){
			s=71;
			var h = CM_ie5? document.body.scrollTop+event.clientY-CM_menuobj.offsetHeight:window.pageYOffset+e.clientY-CM_menuobj.offsetHeight;
			if (h<0) {h=0;}
			CM_menuobj.style.top=h;
		}else{
			s=72;
			CM_menuobj.style.top=CM_ie5? document.body.scrollTop+event.clientY:window.pageYOffset+e.clientY;
		}s=8;
		for (i=0;i<allmenuitems.length;i++){
			var item;
			item=allmenuitems[i];
			var ir = item.getAttribute("right");
			var re = new RegExp(ir,'ig')
			if ((rights=="") || (rights.match(re))) {//
				item.className="cm_item_on";
				item.style.color=CM_Color_Fg;
			}else{
				item.className="cm_item_off";
				item.style.color=CM_Color_Off_Fg;
			}
		}s=9;
		CM_menuobj.style.visibility="visible";
	}catch(ex){
		alert(ex + s);
	}
	return false;

};


function OpenCMHoriz(c,e){
	CM_control4menu=c;
	e.cancelBubble = true;
	e.returnValue=false;
	var s=0;
	try{
		var mymenu = c.getAttribute('ctxtmenu');
		try{CM_menuobj.style.visibility='hidden';}catch(ex){}
		CM_menuobj=document.getElementById(mymenu);s=3;
		CM_menuobj.style.display=''
		var rights = c.getAttribute('ctxtmenu_rights');s=4;
		if (rights==undefined) rights="";s=5;
		var remsz1=CM_ie5?document.body.clientWidth-event.clientX:window.innerWidth-e.clientX;
		var remsz2=CM_ie5?document.body.clientHeight-event.clientY:window.innerHeight-e.clientY;
			
		if (remsz1<CM_menuobj.offsetWidth){
			var l =CM_ie5? document.body.scrollLeft+event.clientX-CM_menuobj.offsetWidth:window.pageXOffset+e.clientX-CM_menuobj.offsetWidth;
			if (l<0) {l=0;}
			CM_menuobj.style.left = l;
		}else{
			CM_menuobj.style.left=CM_ie5? document.body.scrollLeft+event.clientX:window.pageXOffset+e.clientX;
		}

		var allmenuitems=CM_menuobj.getElementsByTagName("span");
		if (remsz2<CM_menuobj.offsetHeight){
			var h =CM_ie5? document.body.scrollTop+event.clientY-CM_menuobj.offsetHeight:window.pageYOffset+e.clientY-CM_menuobj.offsetHeight;
			if (h<0){h=0;}
			CM_menuobj.style.top=h;
		}else{
			CM_menuobj.style.top=CM_ie5? document.body.scrollTop+event.clientY:window.pageYOffset+e.clientY;
		}
		for (i=0;i<allmenuitems.length;i++){
			var item;
			item=allmenuitems[i];
			var ir = item.right;
			var re = new RegExp(ir,'ig')
			if ((rights=="") || (rights.match(re))) {//
				item.className="cm_item_on";
				item.style.color=CM_Color_Fg;
			}else{
				item.className="cm_item_off";
				item.style.color=CM_Color_Off_Fg;
			}
		}
		CM_menuobj.style.visibility="visible";
	}catch(ex){
		alert(ex) + s;
	}
	return false;

};

function CloseCM(e){
	try{
		CM_menuobj.style.visibility="hidden";
	}catch(ex){}
};

function CM_MsOver(e){
	e.cancelBubble = true;
	e.returnValue=false;
	var thismenuitem=CM_ie5? event.srcElement:e.target;
	if(thismenuitem.className=="cm_item_on"||CM_ns6&&thismenuitem.parentNode.className=="cm_item_on"){
		if(CM_ns6&&thismenuitem.parentNode.className=="cm_item_on"){thismenuitem=thismenuitem.parentNode;}
		var allmenuitems=CM_menuobj.getElementsByTagName("div");
		thismenuitem.style.backgroundColor=CM_Color_On;thismenuitem.style.color=CM_Color_OnFg;
		if (thismenuitem.getAttribute("description")){
			window.status=thismenuitem.getAttribute("description");
		}
	}
};

function CM_MsOut(e){
	e.cancelBubble = true;
	e.returnValue=false;
	var thismenuitem=CM_ie5?event.srcElement:e.target;
	if(thismenuitem.className=="cm_item_on"||CM_ns6&&thismenuitem.parentNode.className=="cm_item_on"){
		if(CM_ns6&&thismenuitem.parentNode.className=="cm_item_on"){
			thismenuitem=thismenuitem.parentNode;
		}
		var allmenuitems=CM_menuobj.getElementsByTagName("div");
		thismenuitem.style.backgroundColor=CM_Color_Bkg;
		thismenuitem.style.color=CM_Color_Fg;window.status='';
	}
};
function CM_MsClick(e){
	var thismenuitem=CM_ie5?event.srcElement:e.target;
	if (thismenuitem.className=="cm_item_on"||CM_ns6&&thismenuitem.parentNode.className=="cm_item_on"){
		if (CM_ns6&&thismenuitem.parentNode.className=="cm_item_on"){
			thismenuitem=thismenuitem.parentNode;
		}
		var allmenuitems=CM_menuobj.getElementsByTagName("div");
		var url=thismenuitem.getAttribute("url");
		if (thismenuitem.getAttribute("target")&&(thismenuitem.getAttribute("target")!="_self")){
			window.open(thismenuitem.getAttribute("url"),thismenuitem.getAttribute("target"));
		}else{
			window.location=thismenuitem.getAttribute("url");}
	}
};