global_contextMnuWidth = 155;
JSBasketMenuList = new Object();


function unblockEvents(oid){
	blockNextEvent = false;
	var theSpan = document.getElementById("o"+oid);
	if (theSpan != null){
		theSpan.style.border = "none";	
		theSpan.style.backgroundColor = 'transparent';	
	}
}

function closeContextMenu(){
	var theMenu = document.getElementById("oContextmenu");
	theMenu.style.display = "none";
	closeContextSubMenu();
}

function closeContextSubMenu(){
	var theMenu = document.getElementById("oContextSubmenu");
	theMenu.style.display = "none";
}




//////////////////////////////////////////////////////////////
///////////// Main menu //////////////////////////////////////
//////////////////////////////////////////////////////////////
 
function T1ContextMenu(oid,e){
	var mnuhref = "";
	var caption;
	var isInBasket = false;
	var isBasketItem = false;	
	var mnuBody = "";
	var thisTitle = "";
	
	if (!blockNextEvent){
	 blockNextEvent = true;

	var tg = (e.target) ? e.target : e.srcElement;

	//get context for click
	testNode = tg;
	var thisContext = "default";
	var thisContextOID = 0;	
	var thisCalDay = null;	
	while ((testNode != null) && (thisContext == "default")){
		
		if (testNode.context != null){	
			thisContext = testNode.context;
			if (thisContext == 'calendar'){
			
				if (testNode.calday != null)	
					thisCalDay = testNode.calday;
				if (testNode.oid != null)	
					thisContextOID = testNode.oid;	
			}
		}	
		
		if ((testNode.id != null) && (thisContext == "default") && (thisContextOID == 0)){	

			if (!isNaN(parseInt(testNode.id.substring(1,12)))){
			 thisContextOID = parseInt(testNode.id.substring(1,12));

			 if (testNode.id.substring(0,1) == "b"){
				isBasketItem = true;
				}
			 }
		}	
		testNode = testNode.parentNode;	
	}

	

	//check ancestor: basketItem, basket_clientarea
	if (thisContext == "basket"){
		// alert(thisContextOID + " " + isBasketItem);		
		isInBasket = true;
		basketItemOID = thisContextOID;
		if (basketItemOID > 0)
			isBasketItem = true;
	}
	/*
	testNode = tg;
	while (testNode != null){
		if (testNode.id != null){	
			if ((testNode.id == "basketItem")||(testNode.id == "basket_clientarea"))
				isInBasket = true;
			if (testNode.id.indexOf("basketItem") == 0){
				isBasketItem = true;
				basketItemOID = testNode.id.substring(11,33);
			}	
		}	
		testNode = testNode.parentNode;	
	}
	*/
	

	 if ((typeof(jsobjMenus) == 'undefined')||(jsobjMenus.length ==0)){
		jsobjMenus = new Array();
		jsobjMenus[0] = new Object();
		jsobjMenus[0]["d"] = "no";
		jsobjMenus[0]["oid"] = "0";
		jsobjMenus[0]["e"] = "no";
		jsobjMenus[0]["p"] = "no";
		jsobjMenus[0]["title"] = "System";
	 }
	// if ((typeof(basketItemOID) != 'undefined') && isBasketItem)
	  	oid = thisContextOID;//oid=basketItemOID;


		
 	for (var ix = 0; ix < jsobjMenus.length; ix++){
		if (jsobjMenus[ix].oid == oid){
			 var  thisObject = jsobjMenus[ix];
			 break;
	 	}
 	}
	 if (typeof(thisObject) == 'undefined'){
	  	if (typeof(jsBasketMenus) != 'undefined'){
			for (var ix = 0; ix < jsBasketMenus.length; ix++){
				if (jsBasketMenus[ix].oid == oid){
				 var  thisObject = jsBasketMenus[ix];
				 break;
		 		}
		 	}		
		}
	 }
	 
	
	 
 	 if (typeof(thisObject) == 'undefined'){
		 var  thisObject = jsobjMenus[0];
	 }
	 
	 

		 if (thisObject.title.length > 21){
		 thisObject.title = thisObject.title.substring(0,21) + '...';
		 }
	
		var  menuItems = new Array();
		var theMenu = document.getElementById("oContextmenu");
		var theSpan = document.getElementById('o'+oid);	
		if (theSpan != null){
			theSpan.style.backgroundColor = 'yellow';	
			theSpan.style.borderTop = 'solid 1px red';	
			theSpan.style.borderBottom = 'solid 1px red';			
			}
	
	
	
	 thisTitle = thisObject.title;
	 //if no object is selected, create title based on context
	 if (thisContextOID < 1){
		switch (thisContext){
		case "basket": 	thisTitle = "Korg";
		break;
		case "default": thisTitle = "Systemmeny";
		break;
		case "sat": thisTitle = "Systemmeny";
		break;		
		default : thisTitle = "Systemmeny";
		}
	 	
	  }  
	
		theMenu.innerHTML = "";
		menuItems.push('<div class="contextMenuTitle">'+ thisTitle + '</div><ul>');

		//add underscores to reduce false hits
		thisContext = '_' + thisContext + '_';
		
		
		//Add events: for context: calendar
		ContextForThisItem = "_calendar_";
		
		if ((thisCalDay != null) && (!global_sat)){
		

			if (thisObject.e == "Yes"){
 
				mnuhref= global_applicationRoot + "/desk/sysmethods.cfm?sysmethod=createObject&poid=" + thisObject.oid +"&calday=" + thisCalDay + "&objname=cc_event&" + global_urlAppend;
				caption="L&auml;gg till kalendernotering";				
				menuItems.push('<li><a href="'+ mnuhref +'"  onmouseover="closeSubMenu()">' + caption + '</a></li>');	
				}
		}
		//Edit: for context: main,default,basket,calendar
		ContextForThisItem = "_main_,_view_,_basket_,_calendar_";
		if ((ContextForThisItem.indexOf(thisContext) >= 0) && (thisContextOID > 0) && (!global_sat)){
				if (thisObject.e == "Yes"){
				mnuhref= global_applicationRoot + "/desk/objpreview.cfm?oid=" + thisContextOID + "&" + global_urlAppend;
				caption=globaltext_edit;				
				menuItems.push('<li><a href="'+ mnuhref +'"  onmouseover="closeSubMenu()">' + caption + '</a></li>');	
				}
		}
		
		if (thisObject.p == "Yes"){
			//Move: for context: main,default,basket,calendar
		ContextForThisItem = "_main_,_view_,_basket_,";
		if ((ContextForThisItem.indexOf(thisContext) >= 0) && (thisContextOID > 0) && (!global_sat)){
				mnuhref= global_applicationRoot + "/joinproperties.cfm?oid=" + thisObject.oid + "&" + global_urlAppend;
				caption=globaltext_move;				
				menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()">' + caption + '</a></li>');	
			}
			
			//Create: for context: main,default,calendar
			ContextForThisItem = "_main_,_view_";
			if ((ContextForThisItem.indexOf(thisContext) >= 0) && (thisContextOID > 0)){
				// Create a submenu for createObject
				// get allowed parents with the serverlink
				var jRules = getServerData(global_applicationRoot + '/serverlink.cfm?replyfunction=jresults&question=getjoinrules&oid='+oid);
				
			
				
				//mnuhref= global_applicationRoot + "/desk/sysmethods.cfm?sysmethod=createobject&poid=" + thisObject.oid + "&" + global_urlAppend;
				mnuhref="#";
				caption=globaltext_createchild;				
				menuItems.push('<li class="contextSubmenuTrigger"  onMouseOver="showT1ContextSubMenu(event)"><a href="'+ mnuhref +'">' + caption + '</a></li>');	
				//menuItems.push('<div class="contextSubmenuTrigger" onmouseover="showT1ContextSubMenu(event)">' + caption + '</div>');	
				}			
			}
		
		//Kategorier: for context: main,default,basket,calendar
		ContextForThisItem = "_main_,_view_,_default_,_basket_,_calendar_";
		if ((ContextForThisItem.indexOf(thisContext) >= 0) && (thisContextOID > 999)){
			if (thisObject.e == "Yes"){		
				if (menuItems.length > 1)
					menuItems.push("<hr>");	
				mnuhref= global_applicationRoot + "/desk/objpreview.cfm?oid=" + thisContextOID + "&" + global_urlAppend;
				caption=globaltext_editOCS;				
				menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()">' + caption + '</a></li>');	
				
				mnuhref= global_applicationRoot + "/desk/objpreview.cfm?oid=" + thisContextOID + "&" + global_urlAppend;
				caption=globaltext_editGIS;				
				menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()">' + caption + '</a></li>');	
				}
		}
	
	 	 
		//Korgar: for context: main,default,basket,calendar
		ContextForThisItem = "_main_,_view_,_default_,_basket_";
		if ((ContextForThisItem.indexOf(thisContext) >= 0) && (!global_sat)){
			if (menuItems.length > 1)
					menuItems.push("<hr>");						
		    if (!isInBasket){
				if (thisContextOID > 999){ 
				 // mnuhref= "javascript:popupWindow('" + global_applicationRoot + "/basket.cfm?method=add&items=" + thisObject.oid + "&sat=1" +  "','baskets',500,500)";
				 mnuhref= "javascript:getServerData('"+global_applicationRoot+"/serverlink.cfm?replyFunction=rebuildBasket&question=addtobasket&oid=" + thisObject.oid +"')";
			     caption=globaltext_addToBasket;				
				 menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()">' + caption + '</a></li>');	
				}
			}
			else{	
		
				if ((thisContextOID > 999) & (isBasketItem)){ 
				 mnuhref= "javascript:getServerData('"+global_applicationRoot+"/serverlink.cfm?replyFunction=rebuildBasket&question=removeFromBasket&oid=" + thisObject.oid +"')";
			     caption=globaltext_removeFromBasket;				
				 menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()">' + caption + '</a></li>');	
				}
				else{
				//Tomma korgen
				 mnuhref= "javascript:getServerData('"+global_applicationRoot+"/serverlink.cfm?replyFunction=rebuildBasket&question=emptyBasket')";
			     caption=globaltext_emptyBasket;				
				 menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()">' + caption + '</a></li>');	
				}
			}
		}
		
		
		// for all but  sat

		if (!global_sat){
			if ((menuItems.length > 1) && (global_userlevel > 0))
					menuItems.push("<hr>");		

			if (global_userlevel > 9){
			// Ditt kontor
			 caption=globaltext_office;				
			 mnuhref= global_applicationRoot + "/desk/index.cfm?" + global_urlAppend;
			 menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()">' + caption + '</a></li>');	
			}
			if (global_userlevel > 0){
			  //Egna instellningar
				 caption=globaltext_personal;			
				 mnuhref= global_applicationRoot + "/personalpage.cfm?" + global_urlAppend;
				 menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()">' + caption + '</a></li>');	
			}	
		}

		if (menuItems.length > 1)
				menuItems.push("<hr>");		

	//always this		
	// Startsida
		if (global_sat)
		  varMnuItemTarget=' target="_parent" ';
		else
		  varMnuItemTarget=''; 
		   
		 caption=globaltext_startpage;	
		 mnuhref= global_applicationRoot + "/";
		 menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()" ' + varMnuItemTarget +'>' + caption + '</a></li>');	
		 
	 //Korgar
	/*	if (global_showBaskets){
		 caption=globaltext_baskets;
		 mnuhref=global_applicationRoot + "/basket.cfm?" + global_urlAppend;
		 menuItems.push('<a href="'+ mnuhref +'" onmouseover="closeSubMenu()">' + caption + '</a>');	
		 }
	*/	 
		
	 // Search 
		 caption=globaltext_search;
		 mnuhref=global_applicationRoot + "/search.cfm";
		 menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()" ' + varMnuItemTarget +'>' + caption + '</a></li>');		
		
	 //Help
			caption=globaltext_help;
			mnuhref= global_applicationRoot + "/webhelp/";
			menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()" ' + varMnuItemTarget +'>' + caption + '</a></li>');		
	
		menuItems.push("<hr>");			
		
		if (global_username != 'guest'){
			caption= globaltext_logout;
			mnuhref= global_applicationRoot + "/logout.cfm?sessiontimeout=0&" + global_urlAppend;
			menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()" ' + varMnuItemTarget +'>' + caption + '</a></li>');		
			}
		else
		 {	
			caption= globaltext_login;
			mnuhref= global_applicationRoot + "/login.cfm?" + global_urlAppend;
			menuItems.push('<li><a href="'+ mnuhref +'" onmouseover="closeSubMenu()" ' + varMnuItemTarget +'>' + caption + '</a></li>');				
	     }
		menuItems.push("</ul>");		
	    for (ix = 0; ix < menuItems.length; ix++){
			mnuBody = mnuBody + menuItems[ix];
		}
		theMenu.innerHTML = mnuBody;
		//position it and show it
		showContextMenu(theMenu,e);
	
		window.setTimeout("unblockEvents("+thisObject.oid+ ")", 1000); 
		
	}
		

  return false;
}

////////////////////////////////////////////////////////////////////////////
/////////////// Sub menu //////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
function showT1ContextSubMenu(e){
	var menuobj = document.getElementById("oContextSubmenu");
	var ie5=document.all&&document.getElementById
	var ns6=document.getElementById&&!document.all
	//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

	var eOffsetX=ie5? event.offsetX : e.layerX 
	var eOffsetY=ie5? event.offsetY : e.layerY
		
	menuobj.style.display = "block";	
	menuobj.style.position = "absolute";
	menuobj.style.zIndex = 1001;	
	menuobj.style.width = global_contextMnuWidth;		

	/* if the horizontal distance isnt enough to accomodate the width of the context menu */
	if ((rightedge)<(menuobj.offsetWidth+(global_contextMnuWidth)+4)){
	/* move the horizontal position of the menu to the left by it's width */
	/* alert(rightedge + " " + menuobj.offsetWidth) */
	menuobj.style.left=ie5? document.body.scrollLeft+(event.clientX+(global_contextMnuWidth-4-eOffsetX)-(global_contextMnuWidth*2)) : window.pageXOffset+e.clientX+(global_contextMnuWidth-4-eOffsetX)-(global_contextMnuWidth*2)
	}
	else
	//position the horizontal position of the menu where the mouse was clicked
	menuobj.style.left=ie5? document.body.scrollLeft+(event.clientX+(global_contextMnuWidth-10-eOffsetX)) : window.pageXOffset+e.clientX+(global_contextMnuWidth-10-eOffsetX)
	
	//vertical position
	if (bottomedge<menuobj.offsetHeight){
	menuobj.style.top=ie5? document.body.scrollTop+event.clientY-menuobj.offsetHeight + eOffsetY: window.pageYOffset+e.clientY-menuobj.offsetHeight + eOffsetY
	}
	else{
	menuobj.style.top=ie5? document.body.scrollTop+event.clientY -eOffsetY : window.pageYOffset+e.clientY - eOffsetY
	}
		if (menuobj.style.top.indexOf('-') == 0){

		menuobj.style.top = "4px";
		}
	
}

function closeSubMenu(){
	var theMenu = document.getElementById("oContextSubmenu");
	theMenu.style.display = "none";	
}

function initT1ContextSubMenu(sItems){
	var theMenu = document.getElementById("oContextSubmenu");
	var mnuBody = "<ul>";
	theMenu.innerHTML = ""
	var  menuItems = new Array();

	for (var ix = 0; ix < sItems.length; ix++){
		menuItems.push('<li><a href="'+ sItems[ix].url +'">' + sItems[ix].caption + '</a></li>');		
	}
	menuItems.push('</ul>');
	for (ix = 0; ix < menuItems.length; ix++){
		
		mnuBody = mnuBody + menuItems[ix];
	}
	theMenu.innerHTML = mnuBody;
	
}	

function setBasketObjList(reply){

  JSBasketMenuList = reply;

}



function jresults(reply){
		var submenu = new Array;
		var preUrl = global_applicationRoot + "/desk/sysmethods.cfm?sysmethod=createObject&poid="+reply.oid+"&objname=";
		for (var ii = 0; ii < reply.allowedchildrens.length; ii++){		
		itm = new Object;
		itm.url = preUrl + reply.allowedchildrens[ii].objname; + global_urlAppend;
		itm.caption = reply.allowedchildrens[ii].title;
		submenu.push(itm);
		}
	initT1ContextSubMenu(submenu);

}

///////////////////////////////////////////////////////////////////////////
/////////////// Show menu /////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////


function showContextMenu(menuobj,e){
	var ie5=document.all&&document.getElementById
	var ns6=document.getElementById&&!document.all
	//if (ie5 & !e.clientX)
	//alert(e);	
	//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
	var browserHeight = ie5? document.body.clientHeight : window.innerHeight;
	var eOffsetX=ie5? event.offsetX : e.layerX 
	var eOffsetY=ie5? event.offsetY : e.layerY
		
	menuobj.style.position = "absolute";
	menuobj.style.display = "block";	
	menuobj.style.zIndex = 1000;	
	menuobj.style.width = global_contextMnuWidth;		

	
	// if the horizontal distance isnt enough to accomodate the width of the context menu
	if (rightedge<menuobj.offsetWidth)
	//move the horizontal position of the menu to the left by its width
	menuobj.style.left=ie5? document.body.scrollLeft+event.clientX-menuobj.offsetWidth : window.pageXOffset+e.clientX-menuobj.offsetWidth
	else
	//position the horizontal position of the menu where the mouse was clicked
	menuobj.style.left=ie5? document.body.scrollLeft+event.clientX : window.pageXOffset+e.clientX
	
	//same concept with the vertical position
	//if (bottomedge<menuobj.offsetHeight)
	//menuobj.style.top=ie5? document.body.scrollTop+event.clientY-menuobj.offsetHeight : window.pageYOffset+e.clientY-menuobj.offsetHeight
	//else
	//menuobj.style.top=ie5? document.body.scrollTop+event.clientY : window.pageYOffset+e.clientY

	if (bottomedge<menuobj.offsetHeight){
	
	menuobj.style.top=ie5? document.body.scrollTop+event.clientY-menuobj.offsetHeight + eOffsetY: window.pageYOffset+e.clientY-menuobj.offsetHeight + eOffsetY
	}
	else{
	
	menuobj.style.top=ie5? document.body.scrollTop+event.clientY -eOffsetY : window.pageYOffset+e.clientY - eOffsetY
	}
	
	if ((parseInt(menuobj.style.top) + menuobj.offsetHeight) > browserHeight)
		menuobj.style.top = browserHeight - menuobj.offsetHeight;

		
	//alert(document.body.clientHeight-(menuobj.scrollTop+menuobj.offsetHeight));
	if ((document.body.clientHeight < menuobj.offsetHeight) || (menuobj.style.top.indexOf('-') == 0)){
		menuobj.style.top = "4px";
	}	
	
	
	
return false
}



///////////////////////////////////////////////////////////////////////
////////////////// serverlink ////////////////////////////////////////
///////////////////////////////////////////////////////////////////////


function getServerData(url){

if (document.getElementById('serverLink') == null)
	createServerLink();
	// replyfonction can be set in url myPage.htm?replyFunction=myReply
	var theLink = document.getElementById('serverLink');
	theLink.src =  url; 
}

function createServerLink(){
var sTag = "IFRAME";
var oElement = document.createElement(sTag);
oElement.setAttribute("id", 'serverLink'); 
oElement.setAttribute("frameborder", '0');
oElement.style.width=0 + "px";
oElement.style.height=0 + "px";
document.body.appendChild(oElement);
}

function fillDropdown(id,data){
	var theSelect= document.getElementById(id);
	var ii = 0;
	if (theSelect != null){
	for (ii=0; ii = theSelect.options.length; ii++){
	theSelect.options[0] = null;
	}
	for (ii=0; ii < data.length; ii=ii+1){
	theSelect.options[ii] = new Option;
	theSelect.options[ii].value = data[ii].value; 
	theSelect.options[ii].text = data[ii].text;	
	}
	
	}
	
}

