/*
* nlstreeext_dd.js v2.2
* NlsTree Extension : Drap-drop operations
* This file is property of AddObject.com
* You are not allowed to use this script or 
* part of this script indepently without any interaction 
* with NLSTree Professional.
* Copyright 2005-2006, addObject.com. All Rights Reserved
* Author Jack Hermanto, www.addobject.com
*/
var nlsddSession=null;
function NlsDDAction(){}NlsDDAction.DD_INSERT="I";
NlsDDAction.DD_APPEND="A";
function NlsDDSession(A,B){this.srcObj=A;
this.srcData=B;
this.destObj=null;
this.destData=null;
this.action=null;
this.consume=function(){this.srcObj=null;
this.srcData=null;
this.destObj=null;
this.destData=null;
this.action=null
}
}function I18N(){this.notAllowed="Not allowed!"
}function NlsTreeDD(A){this.tId=A;
this.tree=nlsTree[A];
this.tree.opt.evMouseUp=true;
this.tree.opt.evMouseDown=true;
this.tree.opt.evMouseOver=true;
this.tree.opt.evMouseOut=true;
this.tree.treeOnMouseUp=ddMouseUp;
this.tree.treeOnMouseDown=ddMouseDown;
this.tree.treeOnMouseOver=ddMouseOver;
this.tree.treeOnMouseOut=ddMouseOut;
this.tree.ddHandler=this;
this.startDrag=startDrag;
this.endDrag=endDrag;
this.ic=[new Image()];
this.ic[0].src=this.tree.defImgPath+"nodrop.gif";
this.i18n=new I18N();
this.onNodeDrag=onNodeDrag;
this.onNodeDrop=onNodeDrop;
this.canDrag=canDragNode;
this.canDrop=canDropNode;
return this
}function startDrag(D){var C=NlsGetElementById("ddGesture");
var H=window.scrollX?window.scrollX:document.body.scrollLeft;
var G=window.scrollY?window.scrollY:document.body.scrollTop;
C.style.left=D.clientX+H+5+"px";
C.style.top=D.clientY+G+5+"px";
C.style.zIndex=1;
if(C.style.display=="none"){var F=nlsddSession.srcData;
var I="";
for(var B=0;
B<F.length;
B++){var E=NlsGetElementById(F[B].id).childNodes[0].childNodes[0].childNodes[0].childNodes[1];
var A=null;
if(nlsddSession.srcObj.opt.icon){A=E.childNodes[(E.childNodes.length>1?1:0)]
}I+="<table cellpadding=0 cellspacing=0><td>"+(A?'<img src="'+A.src+'">':"&nbsp;")+'</td><td><a class="'+this.tree.opt.stlprf+'node">'+F[B].capt+"</a></td></table>"
}C.innerHTML=I;
C.style.display=""
}this.onNodeDrag(D)
}function endDrag(B){var A=NlsGetElementById("ddGesture");
A.innerHTML="";
A.style.display="none";
document.onmousemove=null;
document.onmouseup=null;
document.onmousedown=function(){return true
};
document.onselectstart=function(){return true
};
document.onselectstart=function(){return true
}
}function getTargetId(A,B,D){if(nls_isIE){var C=A.nLst[B.srcElement.parentElement.parentElement.parentElement.parentElement.parentElement.id];
if(C){return C.orgId
}else{return D
}}else{return D
}}function ddMouseUp(A,C){var B=this.getNodeById(getTargetId(this,A,C));
if(!nlsddSession){return false
}if(!this.ddHandler.canDrop(B.orgId)){return false
}nlsddSession.destObj=this;
nlsddSession.destData=B;
this.ddHandler.endDrag(A);
this.ddHandler.onNodeDrop(A);
nlsddSession=null
}function ddMouseDown(C,E){if(this.$editing){return 
}var B=this.ddHandler;
var A=this.genIntId(E);
NlsTree._blockEdit=false;
if(this.opt.multiSel){if(!this.isSelected(E)&&!(C.ctrlKey||C.shiftKey)){NlsTree._blockEdit=true;
this.selectNode(A)
}}else{if(this.selNd==null||this.selNd.id!=A){this.selectNode(A);
NlsTree._blockEdit=true
}}var D=this.getSelNodes();
if(B.canDrag(E)){nlsddSession=new NlsDDSession(this,D);
document.onmousemove=function(F){B.startDrag((F?F:event))
};
document.onmouseup=function(F){nlsddSession.action=null;
B.endDrag((F?F:event))
};
document.onselectstart=function(){return false
};
document.onmousedown=function(){return false
};
document.ondragstart=function(){return false
}
}}function ddMouseOver(C,E){if(nlsddSession!=null&&document.onmousemove!=null){}else{return 
}var A=this.ddHandler;
var D=getTargetId(this,C,E);
if(!A.canDrop(D)){var B=NlsGetElementById("ddGesture");
B.style.display="";
B.innerHTML='<table cellpadding=0 cellspacing=0><td><img src="'+A.ic[0].src+'"></td><td><a class="'+this.opt.stlprf+'node">'+A.i18n.notAllowed+"</a></td></table>"
}}function ddMouseOut(B,C){if(nlsddSession!=null&&document.onmousemove!=null){}else{return 
}var A=NlsGetElementById("ddGesture");
A.style.display="none"
}function onNodeDrag(A){if(A.shiftKey){nlsddSession.action=NlsDDAction.DD_INSERT
}else{nlsddSession.action=NlsDDAction.DD_APPEND
}}function onNodeDrop(e){if(!nlsddSession){return 
}var sData,dData,sObj,dObj;
with(nlsddSession){if(!action){return 
}sData=srcData;
sObj=srcObj;
dData=destData;
dObj=destObj
}if(sObj.tId==dObj.tId){switch(nlsddSession.action){case NlsDDAction.DD_INSERT:sObj.moveChild(sData,dData,2);
break;
case NlsDDAction.DD_APPEND:sObj.moveChild(sData,dData,1);
break
}}else{switch(nlsddSession.action){case NlsDDAction.DD_INSERT:for(i=0;
i<sData.length;
i++){with(sData[i]){var nNd=dObj.addBefore(null,dData.orgId,capt,url,(ic?ic.join(","):ic),exp,chk,xtra);
if(fc){duplicateNode(fc,nNd)
}}}dObj.reloadNode(dData.pr.orgId);
break;
case NlsDDAction.DD_APPEND:for(i=0;
i<sData.length;
i++){with(sData[i]){var nNd=dObj.append(null,dData.orgId,capt,url,(ic?ic.join(","):ic),exp,chk,xtra);
if(fc){duplicateNode(fc,nNd)
}}}dObj.reloadNode(nNd.orgId);
dObj.expandNode(dData.orgId);
break
}}}function duplicateNode(C,A){do{var B=nlsddSession.destObj.add(null,A.orgId,C.capt,C.url,(C.ic?C.ic.join(","):C.ic),C.exp,C.chk,C.xtra);
if(C.fc){duplicateNode(C.fc,B)
}C=C.nx
}while(C)
}function canDragNode(C){if(this.tree.opt.multiSel){var B=this.tree.getSelNodes();
for(var A=0;
A<B.length;
A++){if(B[A].allowDrag==false){return false
}}}else{if(this.tree.getNodeById(C).allowDrag==false){return false
}}return true
}function canDropNode(F){var A=this.tree.getNodeById(F);
var E=nlsddSession.srcData;
var D=(nlsddSession.srcObj.tId==this.tree.tId);
if(A.allowDrop==false){return false
}if(!nlsddSession){return false
}if(!nlsddSession.action){return false
}if(!E||!A||E.length==0){return false
}if(D){if(this.tree.isSelected(A.orgId)){return false
}var C=A;
while(C.pr){if(this.tree.isSelected(C.orgId)){return false
}C=C.pr
}switch(nlsddSession.action){case NlsDDAction.DD_INSERT:if(A.equals(this.tree.rt)){return false
}for(var B=0;
B<E.length;
B++){if(E[B].nx&&A.equals(E[B].nx)){return false
}}break;
case NlsDDAction.DD_APPEND:if(this.tree.isSelected(this.tree.rt.orgId)){return false
}for(var B=0;
B<E.length;
B++){if(A.equals(E[B].pr)){return false
}}break
}}else{switch(nlsddSession.action){case NlsDDAction.DD_INSERT:if(A.equals(this.tree.rt)){return false
}case NlsDDAction.DD_APPEND:break
}}return true
}NlsTree._allowEdit=false;
NLSTREE.ddHandler=null;
NLSTREE.unloadChild=function(A){var B=A.pr;
if(B.lc.equals(A)){B.lc=A.pv
}if(B.fc.equals(A)){B.fc=A.nx
}if(A.pv!=null){A.pv.nx=A.nx
}if(A.nx!=null){A.nx.pv=A.pv
}A.nx=null;
A.pv=null;
A.pr=null;
if(this.selNd){this.selNd=null;
this.selElm=null
}if(this.opt.multiSel){this.msRemove(A.orgId)
}};
NLSTREE.moveChild=function(B,G,F){if(!B||!G||B.length==0){return 
}if(this.isSelected(G.orgId)){return 
}var D=G;
while(D.pr){if(this.isSelected(D.orgId)){return 
}D=D.pr
}switch(F){case 1:if(this.isSelected(this.rt.orgId)){return 
}for(var E=0;
E<B.length;
E++){if(G.equals(B[E].pr)){false
}}for(var E=0;
E<B.length;
E++){var J=B[E].pr;
this.unloadChild(B[E]);
this.reloadNode(J.orgId);
B[E].pr=G;
if(G.lc==null){G.fc=B[E];
G.lc=B[E]
}else{var I=G.fc;
if(this.opt.sort!="no"){do{if(this.opt.sort=="asc"?this.compareNode(I,B[E]):this.compareNode(B[E],I)){break
}I=I.nx
}while(I!=null);
if(I!=null){if(I.pv==null){I.pv=B[E];
G.fc=B[E]
}else{B[E].pv=I.pv;
I.pv.nx=B[E];
I.pv=B[E]
}B[E].nx=I
}}if(this.opt.sort=="no"||I==null){B[E].pv=G.lc;
G.lc.nx=B[E];
G.lc=B[E]
}}}this.reloadNode(G.orgId);
this.expandNode(G.orgId);
break;
case 2:case 3:var C="pv";
var H="nx";
var A="lc";
if(F==2){C="nx";
H="pv";
A="fc"
}if(G.equals(this.rt)){return 
}for(var E=0;
E<B.length;
E++){if(B[E][C]&&G.equals(B[E][C])){return 
}}for(var E=0;
E<B.length;
E++){var J=B[E].pr;
this.unloadChild(B[E]);
this.reloadNode(J.orgId);
B[E].pr=G.pr;
if(G[H]==null){G[H]=B[E];
G.pr[A]=B[E]
}else{B[E][H]=G[H];
G[H][C]=B[E];
G[H]=B[E]
}B[E][C]=G
}this.reloadNode(G.pr.orgId);
break
}};
function nls_setNodeDnD(B,E,D,A,C){if(C==true){B.loopTree(B.getNodeById(E),function(F){F[D]=A
})
}else{B.getNodeById(E)[D]=A
}}NLSTREE.setDrag=function(C,A,B){nls_setNodeDnD(this,C,"allowDrag",A,B)
};
NLSTREE.setDrop=function(C,A,B){nls_setNodeDnD(this,C,"allowDrop",A,B)
};
NlsNode.prototype.allowDrag=true;
NlsNode.prototype.allowDrop=true;
