
// Html del menu
document.write('<div id="context_menu" style="z-index:9999;width:200px;border:1px solid black;background-color:#EEEEEE;visibility:hidden;position:absolute;line-height:1.3em; padding:0;font-size:0.7em">');
document.write('<p id="ctx_Title" style="border:1px solid #ccc;background-color:#ddd;padding:3px 5px;margin:0;font-weight:bold">Menu</p>');
document.write('<div id="context_menu_container" style="padding:5px">');
document.write('	<a id="ctx_btnOpen" href="#" onClick="ctx_Click(' + "'open'" + ')">Visualizza</a>');
if (canEdit) {
//document.write('	<a id="ctx_btnNewChild" href="#" onClick="ctx_Click(' + "'new'" + ')">Nuovo</a>');
document.write('	<a id="ctx_btnNewChild" href="#" onClick="ctx_Click(' + "'newchild'" + ')">Nuovo figlio</a>');
document.write('	<a id="ctx_btnNewBrother" href="#" onClick="ctx_Click(' + "'newbrother'" + ')">Nuovo fratello</a>');
document.write('	<a id="ctx_btnEdit" href="#" onClick="ctx_Click(' + "'edit'" + ')">Modifica</a>');
document.write('	<a id="ctx_btnDelete" href="#" onClick="ctx_Click(' + "'delete'" + ')">Elimina</a>');

document.write('	<hr style="border:none;border-top:1px dotted grey;" />');

document.write('	<a class="itemCutted" id="ctx_btnCut" href="#" onClick="ctx_Click(' + "'cut'" + ')">Taglia</a>');
document.write('	<a class="itemDestination" id="ctx_btnDest" href="#" onClick="ctx_Click(' + "'dest'" + ')">Imposta come destinazione</a>');

document.write('    <div id="ctx_btnDest_subMenu" class="subMenu"><p class="itemDestination">Incolla sulla destinazione:</p>');
document.write('		<a class="itemDestination sub" id="ctx_btnDestLC" href="#" onClick="ctx_Click(' + "'lastChild'" + ')">- come ultimo figlio</a>');
document.write('		<a class="itemDestination sub" id="ctx_btnDestPB" href="#" onClick="ctx_Click(' + "'prevBrother'" + ')">- come fratello precedente</a>');
document.write('		<a class="itemDestination sub" id="ctx_btnDestNB" href="#" onClick="ctx_Click(' + "'nextBrother'" + ')">- come fratello successivo</a>');
document.write('    </div>');
/*
document.write('    <div id="ctx_btnDest_subMenu" class="subMenu"><p class="itemDestination">Loading...</p>');
//document.write('		<a class="itemDestination sub" id="ctx_btnDestBefore" href="#" onClick="ctx_Click(' + "'pasteBefore'" + ')">- incolla prima</a>');
//document.write('		<a class="itemDestination sub" id="ctx_btnDestAfter" href="#" onClick="ctx_Click(' + "'pasteAfter'" + ')">- incolla dopo</a>');
document.write('    </div>');
*/

document.write('	<a id="ctx_btnCancel" href="#" onClick="ctx_Click(' + "'cancel'" + ')">Annulla</a>');

//document.write('	<hr style="border:none;border-top:1px dotted grey;" />');
}

document.write('	<hr style="border:none;border-top:1px dotted grey;" />');
if (canEdit) {
document.write('	<a id="ctx_btnAdmin" href="#" onClick="ctx_Click(' + "'admin'" + ')">Gestisci nodo</a>');
//document.write('	<a id="ctx_btnAdmin" href="#" onClick="ctx_Click(' + "'cache'" + ')">Cache nodo</a>');
}
document.write('	<a id="ctx_btnExportTree" href="#" onClick="ctx_Click(' + "'exportTree'" + ')">Esporta ramo</a>');

document.write('</div>');
document.write('</div>');

var ctxMenu_mozilla=document.getElementById && !document.all;
var ctxMenu_ie=document.all;
var contextisvisible=0;

var exportID=null;
var ctxClickObject=null;

function iebody(){
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body;
}

function displaymenu(e){
	srcObj=Event.element(e); //(e.target)?e.target:e.srcElement;
	
	if (srcObj.className.indexOf("ctxMenu")==-1) {
		if (ctxMenu_mozilla){
			e.preventDefault();
		}		
		return false;
	}
	
	//srcObj=srcObj.parentNode.parentNode.nextSibling.firstChild;
	srcObj=srcObj.up().up().next().firstDescendant();
	ctxClickObject=srcObj;
	
	//evento generato prima della visualizzazione del menu
	beforeCtxDisplay(srcObj);
	
	el=document.getElementById("context_menu");
	contextisvisible=1;
	if (ctxMenu_mozilla){
		el.style.left=pageXOffset+e.clientX+"px";
		el.style.top=pageYOffset+e.clientY+"px";
		el.style.visibility="visible";
		e.preventDefault();
		return false;
	}
	else { if (ctxMenu_ie){
		el.style.left=iebody().scrollLeft+event.clientX;
		el.style.top=iebody().scrollTop+event.clientY;
		el.style.visibility="visible";
	    return false;
	}}
}

// * * * * * *  evento click per il context menu   * * * * * *
function contextClick(btnName, htmlObj){
    var btnName, htmlObj;
	var divNode=htmlObj.parentNode.parentNode;
	var treeObj=myTree; //eval(divNode.id.split("_")[0]);
	var nodeObj=treeObj.findChild(divNode.id.split("_")[1]);
    
	switch (btnName){
	    case "open":
	        showDocument(nodeObj.id);
		    break;

		case "new":
		    var puri=(nodeObj!=null ? nodeObj.parent.id : null);
		    selectDocumentType(nodeObj.livelloArchivistico, nodeObj.id, puri);
		    break;
	        
		case "newchild":
		    var puri=(nodeObj!=null ? nodeObj.parent.id : null);
		    selectDocumentType(nodeObj.livelloArchivistico, nodeObj.id);
		    break;

		case "newbrother":
		    var puri=(nodeObj!=null ? nodeObj.parent.id : null);
		    selectDocumentType(nodeObj.livelloArchivistico, puri, nodeObj.id);
		    break;

	    case "edit":
	        editDocument(nodeObj.id);
		    break;
		    
		case "delete":
		    deleteDocument(nodeObj.id, nodeObj);
		    break;
	        
		case "cut":
			htmlObj.className+=" itemCutted";
			nodeObj.cutted=true;
			treeObj.cuttedItems.push(nodeObj);
		    break;
		
		case "dest":
			htmlObj.className+=" itemDestination";
			nodeObj.isDestination=true;
			treeObj.destinationItem=nodeObj;
		    break;

		case "lastChild":
		case "prevBrother":		    
		case "nextBrother":
	        var idArray= new Array();
	        var descrizione, destId, destParentId, sourceDescr=null,mode;
	        
	        if (treeObj.destinationItem!=null) {
	            descrizione=treeObj.destinationItem.descrizione;
	            destId=treeObj.destinationItem.id;
	            destParentId=treeObj.destinationItem.parent.id;
	            sourceDescr=nodeObj.descrizione;
	            idArray.push(sourceDescr);
	        } else {
	            descrizione=nodeObj.descrizione;
	            destId=nodeObj.id;
	            destParentId=nodeObj.parent.id;
	            for(x=0;x<treeObj.cuttedItems.length;x++){
		            idArray.push(treeObj.cuttedItems[x].descrizione);
	            }
	        }
	        
	        mode=(btnName=="lastChild"?(idArray.length>1?"ultimi figli":"ultimo figlio"):(btnName=="prevBrother"?(idArray.length>1?"fratelli precedenti":"fratello precedente"):(idArray.length>1?"fratelli successivi":"fratello successivo")));
	        
	        if (confirm("Sei sicuro di voler spostare " + (idArray.length>1?"i nodi":"il nodo") + ":\n" + idArray.join("\n") + "\n\ncome " + mode + " di\n" + descrizione)) {
	            var data;
	            data='<paste mode="' + btnName + '">\n';
	            if (sourceDescr==null) {
                    for(x=0;x<treeObj.cuttedItems.length;x++){
	                    data+='   <source uri="' + treeObj.cuttedItems[x].id + '" parentUri="' + treeObj.cuttedItems[x].parent.id + '" />\n';
                    }
                } else {
	                    data+='   <source uri="' + nodeObj.id + '" parentUri="' + nodeObj.parent.id + '" />\n';
                }
                
                data+='   <destination uri="' + destId + '" parentUri="' + destParentId + '" />\n';
                data+="</paste>";

		        pasteNode(data);
	        }
		    break;
/*
		case "paste":
			var idArray= new Array()
			for(x=0;x<treeObj.cuttedItems.length;x++){
				idArray.push(treeObj.cuttedItems[x].descrizione);
			}

			//if (window.confirm("Sei sicuro di voler incollare il documento\n" + cutID + "\ncome fratello precedente di\n" + document.getElementById("currentIDTitle").value + "?"))

			alert("Nodi incollati PRIMA:\n" + idArray.join("\n") + "\n su:" + treeObj.destinationItem.descrizione);
		    break;

		case "pasteAfter":
			var idArray= new Array()
			for(x=0;x<treeObj.cuttedItems.length;x++){
				idArray.push(treeObj.cuttedItems[x].descrizione);
			}

			alert("Nodi incollati DOPO:\n" + idArray.join("\n") + "\n su:" + treeObj.destinationItem.descrizione);
		    break;
*/

		case "cancel":
			resetSelection(treeObj);
		    break;
		
		case "exportTree":
		    exportID = nodeObj.id;
		    Lightview.show({ href: "selectExport.aspx?id="+nodeObj.id+"&title="+nodeObj.descrizione, title: "Esportazione ramo selezionato", rel: 'iframe'}); //, options: { width: w, height: h }
		    break;

		case "admin":
		    var win=window.open('admin/adminNode.aspx?uri=' + nodeObj.id + '&parentUri=' + nodeObj.parent.id, 'adminNode');
		    win.focus();
		    break;

		case "cache":
		    var data;
            data='<cache uri="' + nodeObj.id + '" parentUri="' + nodeObj.parent.id + '" />';
		    
		    new Ajax.Request('AJAXHierarchy.ashx', {
                method: "post",
                postBody: data,
                onSuccess: function(transport) { 
                    eval(transport.responseText);
                }
            }); 
		    break;
		
		default:
			alert("Bottone [" + btnName + "] non gestito!");
		    break;
	}
}

function ctx_Click(btnName){
	//stabilisco quali item visualizzare
	//menuHandler
	contextClick(btnName,ctxClickObject);
}

function hidemenu(){
	if (typeof el!="undefined" && contextisvisible){
		el.style.visibility="hidden";
		contextisvisible=0;
	}
}

if (ctxMenu_mozilla){
	document.addEventListener("contextmenu", displaymenu, true);
	document.addEventListener("click", hidemenu, true);
}
else if (ctxMenu_ie){
	document.attachEvent("oncontextmenu", displaymenu);
	document.attachEvent("onclick", hidemenu);
}
