
// # Add a Global context menu
// taken from http://localhost/AddObject/nlstree_pro_2.3/demo/contextmenu.htm
    
//context menu
//global context menu, apply to all nodes

var ctx = new NlsCtxMenu("Ctx");

ctx.absWidth=150;
//ctx.add("1", "Add New Section", "", "nlstree/img/newnode.gif", true);
ctx.add("20", "Add New Section", "", "nlstree/img/leaf.gif");
ctx.add("21", "Edit", "", "");
//ctx.add("22", "Edit Security", "", "");
ctx.add("2", "Delete", "", "nlstree/img/deletenode.gif");
//ctx.add("3", "Rename", "", "");
ctx.addSeparator();

// If you add or remove from this list, you must also change the same item in the contextMenuShow function (around line 36)

// ctx.add("4", "Cut", "", "nlstree/img/cut.gif");
// ctx.add("5", "Copy", "", "nlstree/img/copy.gif");
// ctx.add("6", "Insert as Child", "", "");
// ctx.add("7", "Insert Before", "", "");
// ctx.add("8", "Insert After", "", "");
// ctx.addSeparator();

ctx.add("9", "Expand", "", "nlstree/img/arrowdown.gif");
ctx.add("10", "Collapse", "", "nlstree/img/arrowright.gif");
ctx.addSeparator();
ctx.add("11", "Move Up", "");
ctx.add("12", "Move Down", "");
ctx.addSeparator();
ctx.add("13", "Copy Link", "");

/* Some of these are hidden by a call below - see line */
ctx.menuOnClick=globalCtxMenu;
ctx.menuOnShow=contextMenuShow;
   
function contextMenuShow(selNode) {
  var nSel=tree.getSelNodes().length;
  var clCnt=nlsClipboard.clAction;
  // ctx.enableItem(1, nSel<=1);
  // ctx.enableItem(3, nSel<=1);
  // ctx.enableItem(6, (clCnt!=null) && nSel<=1);
  // ctx.enableItem(7, (clCnt!=null) && nSel<=1);
  // ctx.enableItem(8, (clCnt!=null) && nSel<=1);
  ctx.enableItem(9, (selNode.fc!=null && !selNode.exp && nSel<=1));
  ctx.enableItem(10, (selNode.fc!=null && selNode.exp && nSel<=1));
  ctx.enableItem(11, nSel<=1);
  ctx.enableItem(12, nSel<=1);
}

function getRoot(currentNode)
{
	var node = currentNode;
	while (node.pr) 
	{
		node=node.pr;
	}
	
	//alert(node.orgId);
	return node.orgId.substring(1,node.orgId.length);
}

function globalCtxMenu(selNode, menuId, itemId) {

	//alert('selNode = ' + selNode + ' menuId= ' + menuId + ' itemId = ' + itemId);

	var firstChar=selNode.orgId.substring(0,1);
	var theid=selNode.orgId;
	var parentID=(selNode.pr)?selNode.pr.orgId:0;
	var firstCharParent=parentID?parentID.substring(0,1):"";
	var DocumentTitle=selNode.capt;
		
  switch (itemId) {
    case "1":
		tree.ctx_liveAdd(selNode.orgId);
      break;
    case "2":
		if (theid==0 || firstChar == 'G' || firstChar == 'C' || firstChar == 'I' || firstChar == 'T')
		{
			alert('This option is not available for this item.');
		}
		else
   	    {
	      if(confirm('Are you sure you want to delete this section?')) {DeleteNode(theid);}
		}
      break;
    case "3":
      tree.liveNodeEditStart(selNode.id);
      break;
    /*case "4":
      cut(tree.getSelNodes());
      break;
    case "5":
      copy(tree.getSelNodes());
      break;
    case "6": //as child
      insertAsChild(t, tree.getSelNode());
      break;
    case "7": //insert before
      insertBefore(t, tree.getSelNode());
      break;
    case "8": //insert after
      insertAfter(t, tree.getSelNode());
      break;
	  */
    case "9": //Expand
      tree.expandNode(selNode.orgId);
      break;
    case "10": //Collapse
      tree.collapseNode(selNode.orgId);
      break;
    case "11": //move up
      moveUp(tree.getSelNodes());
      break;

    case "12":  //move down
      moveDown(tree.getSelNodes());
      break;
	case "13":  //
	  if( window.clipboardData && clipboardData.setData )
	  {
		clipboardData.setData("Text", tree.getSelNode().url);
		alert("Text copied");
	  }
	  else
	  {
		alert("Internet Explorer required");
	  } 
      
	  break;

	 case "20":   // Add New Section
		if (firstChar == 'G' || firstChar == 'C' || firstChar == 'I' || firstChar == 'T')
		{
			alert('This option is not available for this item.');
		}
		else
	    {
			if (firstChar == 'P')
			{
				var URL = 'EditSections.aspx?PublicationID=' + theid.substring(1,theid.length) + '&ParentSectionID=' + parentID;
			}
			else
			{
				if (firstCharParent == 'P')
				{
					//parentID.substring(1,parentID.length)
					var URL = 'EditSections.aspx?PublicationID=' + parentID.substring(1,parentID.length) + '&ParentSectionID=' + theid;
				}
				else
				{
					var URL = 'EditSections.aspx?PublicationID=' + getRoot(selNode) + '&ParentSectionID=' + theid;
				}
			}

			window.open(URL);
		}
      break;

	 case "21":   // Edit Title and Description

		if (firstChar == 'G' || firstChar == 'C' || firstChar == 'I' || firstChar == 'T')
		{
			alert('This option is not available for this item.');
		}
		else
	    {
			if (firstChar == 'P')
			{
				var URL = 'EditPublications.aspx?PublicationID=' + theid.substring(1,theid.length);
			}
			else
			{
				if (firstCharParent == 'P')
				{
					var URL = 'EditSections.aspx?PublicationID=' + parentID.substring(1,parentID.length) + '&SectionID=' + theid + '&ParentSectionID=' + parentID.substring(1,parentID.length);
				}
				else
				{
					var URL = 'EditSections.aspx?PublicationID=' + getRoot(selNode) + '&SectionID=' + theid + '&ParentSectionID=' + parentID;
				}
				
			}
			window.open(URL);
		}

      break;

	  case "22":  // Security
		alert('editing security');
        break;
  }
}


