// JavaScript Document

//findPos function is from http://www.quirksmode.org/js/findpos.html;
//where its workings are explained in more detail.
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

//Display a named menu, at the position of another object
function display_menu(parent, t, col, id, N, E, U, D, A, X)
{
var collumn = col; 
var itemID = id;
var type = t;
//var editorTemp = et;
var newItem = N;
var editItem = E;
var upItem = U;
var downItem = D;
var publishItem = A;
var deleItem = X;
var editpopUpURL = "";
var newpopUpURL  = "";
if(type=="i"){
editpopUpURL = "'templates/editor/editor.php?id="+itemID+"'";
newpopUpURL = "'templates/editor/new_item.php?id="+itemID+"'";
deleteURL = "'templates/editor/delete_item.php?id="+itemID+"'";
moveUpURL = "'templates/editor/move.php?t=i&m=u&id="+itemID+"'";
moveDownURL = "'templates/editor/move.php?t=i&m=d&id="+itemID+"'";
publishURL = "'templates/editor/publish_archive.php?t=i&a=p&id="+itemID+"'";
archiveURL = "'templates/editor/publish_archive.php?t=i&a=a&id="+itemID+"'";
}
if(type=="p"){
editpopUpURL = "'templates/editor/page_editor.php?id="+itemID+"'";
newpopUpURL = "'templates/editor/new_page.php?id="+itemID+"'";
deleteURL = "'templates/editor/delete_page.php?id="+itemID+"'";
moveUpURL = "'templates/editor/move.php?t=p&m=u&id="+itemID+"'";
moveDownURL = "'templates/editor/move.php?t=p&m=d&id="+itemID+"'";
publishURL = "'templates/editor/publish_archive.php?t=p&a=p&id="+itemID+"'";
archiveURL = "'templates/editor/publish_archive.php?t=p&a=a&id="+itemID+"'";
}
if(type=="ni"){
editpopUpURL = "'templates/editor/editor.php?id="+itemID+"'";
newpopUpURL = "'templates/editor/new_item.php?id="+itemID+"&col="+collumn+"&type="+type+"'";
deleteURL = "'templates/editor/delete_item.php?id="+itemID+"'";
moveUpURL = "'templates/editor/move.php?t=i&m=u&id="+itemID+"'";
moveDownURL = "'templates/editor/move.php?t=i&m=d&id="+itemID+"'";
publishURL = "'templates/editor/publish_archive.php?t=i&a=p&id="+itemID+"'";
archiveURL = "'templates/editor/publish_archive.php?t=i&a=a&id="+itemID+"'";
}
//var popUpURL = "'templates/editor/"+editorTemp+"?id="+itemID+"'";

	//get the named menu
	//var menu_element = document.getElementById(named);
	try {
    	menu_element = document.createElement('<div id="myDiv" style="z-index:100020; position:absolute;"/></div>');
		} catch (e) {
    	menu_element = document.createElement("div");
		menu_element.setAttribute("id", "myDiv");
		menu_element.style.position="absolute";
		menu_element.style.zIndex = 100020;
	}
	//override the 'display:none;' style attribute
	menu_element.style.display = "";
	//get the placement of the element that invoked the menu...
	var placement = findPos(parent);
	//...and put the menu there
	var pleft = placement[0]+0;
	var ptop = placement[1]+0;
	menu_element.style.left = pleft + "px";
	menu_element.style.top = ptop + "px";
	var contextHTML = '<table width="100" cellpadding="0" cellspacing="0" border="0" class="contextmenu">';
	contextHTML +='<tr><td class="tab"><img src="images/context_close.gif" onclick="hide_menu(this.parentNode.parentNode.parentNode)"></td><td class="tabspace" ></td></tr>';
	if(newItem==1){contextHTML +='<tr><td class="row" colspan="2" ><a href="Javascript:popUp('+newpopUpURL+', 750, 700)" style="border:none; width:20px; height:20px; float:left;"><img src="images/context_new.gif" style="border:none;"></a><a href="Javascript:popUp('+newpopUpURL+', 750, 700)">New</a></td></tr>';}
	if(editItem==1){contextHTML +='<tr><td class="row" colspan="2" ><a href="Javascript:popUp('+editpopUpURL+', 750, 700)" style="border:none; width:20px; height:20px;"><img src="images/context_edit.gif" style="border:none;"></a><a href="Javascript:popUp('+editpopUpURL+', 750, 700)">Edit</a></td></tr>';}
	if(upItem==1){contextHTML +='<tr><td class="row" colspan="2" ><a href="Javascript:popUp('+moveUpURL+', 50, 50)" style="border:none; width:20px; height:20px;"><img src="images/context_up.gif" style="border:none;"></a><a href="Javascript:popUp('+moveUpURL+', 50, 50)">Move Up</a></td></tr>';}
	if(downItem==1){contextHTML +='<tr><td class="row" colspan="2" ><a href="Javascript:popUp('+moveDownURL+', 50, 50)" style="border:none; width:20px; height:20px;"><img src="images/context_down.gif" style="border:none;"></a><a href="Javascript:popUp('+moveDownURL+', 50, 50)">Move Down</a></td></tr>';}
		if(publishItem==1){contextHTML +='<tr><td class="row" colspan="2" ><a href="Javascript:popUp('+archiveURL+', 200, 200)" style="border:none; width:20px; height:20px;"><img src="images/context_archive.gif" style="border:none;"></a><a href="Javascript:popUp('+archiveURL+', 200, 200)">Archive</a></td></tr>';}
			if(publishItem==2){contextHTML +='<tr><td class="row" colspan="2" ><a href="Javascript:popUp('+publishURL+', 200, 200)" style="border:none; width:20px; height:20px;"><img src="images/context_live.gif" style="border:none;"></a><a href="Javascript:popUp('+publishURL+', 200, 200)">Publish</a></td></tr>';}
	if(deleItem==1){contextHTML +='<tr><td class="row" colspan="2" ><a href="Javascript:popUp('+deleteURL+', 400, 300)" style="border:none; width:20px; height:20px;"><img src="images/context_delete.gif" style="border:none;"></a><a href="Javascript:popUp('+deleteURL+', 400, 300)">Delete</a></td></tr>';}
	contextHTML +='</table>';
	
	menu_element.innerHTML = contextHTML;
	
	document.body.appendChild(menu_element);
	
}

//Hide a named menu
function hide_menu(named)
{
				var par = named.parentNode;
				par.removeChild(named);
}
