targetDivId = "contextMenu";
contextMenuWidth = 110;

var contextPath = "/umacAuthor";

function getStyleObject(obj)
	{
	var theObj;
	if (document.layers)
		{
		if (typeof obj == "string")
			{ return document.layers[obj]; }
		else
			{ return obj; }
		}

	if (document.all)
		{
		if (typeof obj == "string")
			{ return document.all(obj).style; }
		else
			{ return obj.style; }
		}

	if (document.getElementById)
		{
		if (typeof obj == "string")
			{ return document.getElementById(obj).style; }
		else
			{ return obj.style; }
		}
	return null;
	}

function getHtmlObject(obj)
	{
	var theObj;
	if (document.layers)
		{
		if (typeof obj == "string")
			{ return document.layers[obj]; }
		else
			{ return obj; }
		}

	if (document.all)
		{
		if (typeof obj == "string")
			{ return document.all(obj); }
		else
			{ return obj; }
		}

	if (document.getElementById)
		{
		if (typeof obj == "string")
			{ return document.getElementById(obj); }
		else
			{ return obj; }
		}
	return null;
	}

function getActivationMenuHTML(menuHolderName, pageId, contextMenuWidth)
	{
	var tempHTML;
	tempHTML = "<div id=\"" + menuHolderName +"Xctx\" style=\"position:absolute; border: solid 1px #999999; width:" + contextMenuWidth + "px; padding: 12px; background-color: #FFFFFF; z-index:101\">"
		+	"<table width=\"100%\" cellspacing=\"0\" cellpadding=\"2\" bgcolor=\"#FFFFFF\" style=\"font-family: Verdana, sans-serif; font-size: 11px;\">"
		+		"<tr onclick=\"openActivationWindow('" + menuHolderName + "', '" + pageId + "', 'activate');\" style=\"cursor: pointer;\"><td onmouseover=\"this.style.backgroundColor='#BBB7C7';\" onmouseout=\"this.style.backgroundColor='';\">Activate this page</td></tr>"
		+		"<tr onclick=\"openActivationWindow('" + menuHolderName + "', '" + pageId + "', 'activatesub');\" style=\"cursor: pointer;\"><td onmouseover=\"this.style.backgroundColor='#BBB7C7';\" onmouseout=\"this.style.backgroundColor='';\">Activate incl. sub pages</td></tr>"
		+		"<tr><td style=\"padding:1px;\"><img src=\"" + contextPath + "/admindocroot/controls/activation/hr.gif\" width=\"100%\" height=\"6\" /></td></tr>"
		+		"<tr onclick=\"openActivationWindow('" + menuHolderName + "', '" + pageId + "', 'deactivate');\" style=\"cursor: pointer;\"><td onmouseover=\"this.style.backgroundColor='#BBB7C7';\" onmouseout=\"this.style.backgroundColor='';\">De-activate</td></tr>"
		+	"</table>"
		+ "</div>";
	return tempHTML;
	}

function printActivationMenu(menuHolderName, pageId, contextMenuWidth)
	{
	if(contextMenuWidth > 0) { /* Do nothing */ }
	else { contextMenuWidth = 150; }

	var contextMenuDiv = getHtmlObject(menuHolderName);

	if(contextMenuDiv.innerHTML == "")
		{
		contextMenuDiv.innerHTML = getActivationMenuHTML(menuHolderName, pageId, contextMenuWidth);

		var topPos = 20;
		var leftPos = 100;
//		var topPos = document.body.clientTop + window.event.clientY + 4;
//		var leftPos = document.body.clientLeft + window.event.clientX - 20;
//		var topPos = buttonObj.offsetTop + buttonObj.offsetHeight;
//		var leftPos = buttonObj.offsetLeft;
		if(window.event)
			{
			topPos = window.event.clientY + 4;
			leftPos = window.event.clientX - 20;
			}
		else
			{
			//TODO: Fix this more generic.. With event model
			topPos = 20;
			leftPos = window.innerWidth - 235;
			}

		if((leftPos + contextMenuWidth) > document.body.clientWidth)
			{
			leftPos -= (leftPos + contextMenuWidth) - document.body.clientWidth;
			leftPos = (leftPos < 0) ? 0 : leftPos;
			}

		if((topPos + getHtmlObject( menuHolderName + "Xctx" ).clientHeight) > document.body.clientHeight)
			{
			topPos -= (topPos + getHtmlObject( menuHolderName + "Xctx" ).clientHeight) - document.body.clientHeight;
			topPos = (topPos < 0) ? 0 : topPos;
			}

		with(document.getElementById(menuHolderName + "Xctx").style)
			{
			top = topPos + "px";
			left = leftPos + "px";
			}
		}
	else
		{ clearActivationMenu(menuHolderName); }

	}

function clearActivationMenu(menuHolderName)
	{ getHtmlObject(menuHolderName).innerHTML = ""; }

function openActivationWindow(menuHolderName, pageId, actionType)
	{
	var activationWindow = window.open("", "actPage", "width=400, height=400");
	activationWindow.document.write("<html><head><title>Activation in progress</title><link rel=\"stylesheet\" type=\"text/css\" href=\"" + contextPath + "/docroot/umac_ruby/css/main.css\"></head>");
	activationWindow.document.write("<body><strong>Activationprocess: " + actionType + "</strong><br/> Working with:<br/> <code>" + pageId + "</code><br><br> <img src=\"" + contextPath + "/admindocroot/controls/activation/loading.gif\" border=0 /> please wait...</body></html>");
	activationWindow.focus();
	activationWindow.location = contextPath + "/admintemplates/utilities/activatePage.jsp?path=" + pageId + "&action=" + actionType;
	//alert(contextPath + "/.magnolia/activatePage.html?path=" + pageId + "&action=" + actionType);
	clearActivationMenu(menuHolderName);
	}
