/*
 * DON'T REMOVE THE FOLLOWING LICENSE
 *           INFORMATION!
 * ----------------------------------
 * Copyright by
 * Dennis Ritz
 * Author: Dennis Ritz
 * dennis.ritz@gmx.net
 * 2007-2008
 * ----------------------------------
 */
var dragObject = new Object();
var dropObject = new Object();  
var dropDummy = Browser.createElement("div","","","treeNodeDummy");

function DragAndDrop() {
	this.startDrag = function startDrag(ev,p_dragNode,p_dragType,p_dragID){
		
		ev = ev || window.event;
		ContextMenu.hide(ev);
		var mousepos = mouseCoords(ev);
			
		dragObject.dragNode 		= p_dragNode.parentNode;
		dragObject.dragBar			= p_dragNode;
		dragObject.dropNode			= p_dragNode;
		dragObject.dragAction 		= "move";
		dragObject.dragActionProp 	= "xy";
		dragObject.dragType			= p_dragType;
		dragObject.dragID			= p_dragID;
		dragObject.drag 		= true;
		// Save starting positions of cursor and elements.
		dragObject.cursorStartX 	= mousepos.x;
		dragObject.cursorStartY 	= mousepos.y;
		dragObject.elStartLeft  	= parseInt(dragObject.dragNode.style.left, 10);
		dragObject.elStartTop   	= parseInt(dragObject.dragNode.style.top,  10);
		dragObject.elStartWidth  	= parseInt(dragObject.dragNode.style.width, 10);
		dragObject.elStartHeight   	= parseInt(dragObject.dragNode.style.height,  10);
		if (isNaN(dragObject.elStartLeft)) dragObject.elStartLeft = 0;
		if (isNaN(dragObject.elStartTop))  dragObject.elStartTop  = 0;
	
		document.onmouseup = DragAndDrop.stopDrag;
		document.onmousemove = DragAndDrop.drag;
		return false;
	}
	
	this.drag = function drag(ev){
	    ev           = ev || window.event;
		var mousePos = mouseCoords(ev);
		//standard return false;
	    return false;
	}
	
	this.stopDrag = function stopDrag(){
	    /* Please never set return false on drag mode */
		if(dragObject.drag) dragObject.drag = false;
	
		try{
			dropDummy.style.background="url('img/GUI/Tree/divider.png')";
		}catch(e){}  
		
		document.onmouseup = null;
		document.onmousemove = null;	
		if(dropObject.className != null) {
			dropObject.dropNode.className = dropObject.className;
		}
	    if(dragObject.dragNode){
		    switch(dragObject.dragAction){
		    	case "move":		if(dropObject.dropable) {   
		    							dropObject.dropable = false;
		 	                        	eval(dropObject.dropComp).dragAndDrop(dragObject.dragType,dragObject.dragID,dropObject.dropType,dropObject.dropID,dropObject.order);
		                            }
	                                break;
		    }
	    }

	    return false;
	}
	
	this.setDrop = function setDrop(ev,item,type,p_dropTypes,p_comp,p_dropID){
	
		if(!item) return false;
		if(dragObject.drag) {
			dropObject.dropNode = item.parentNode;
			if(!dropObject.className) dropObject.className = item.parentNode.className;
	        dropObject.dropType = type;
	        dropObject.dropComp = p_comp;
	        dropObject.dropID   = p_dropID;
			
	        moffset = getMouseOffset(dropObject.dropNode,ev);
			//alert("y: "+Browser.getHeight(item)+"px "+moffset.y+"px x: "+Browser.getWidth(item)+"px "+moffset.x+"px");
	        if(dropObject.dropID != dragObject.dragID){
				if(dropObject.dropType == dragObject.dragType && moffset.y < (Browser.getHeight(dropObject.dropNode)*0.30)) {
					dropObject.dropNode.className = dropObject.className;
					dropObject.dropNode.parentNode.previousSibling.style.background="url('img/GUI/Tree/drop.png')";
					dropDummy = dropObject.dropNode.parentNode.previousSibling;
					//dropObject.dropNode.parentNode.parentNode.insertBefore(dropDummy,dropObject.dropNode.parentNode);
					dropObject.order = "before";
					dropObject.dropable = true;
				} else if(dropObject.dropType == dragObject.dragType && moffset.y > (Browser.getHeight(dropObject.dropNode)*0.70)) {
		    		dropObject.dropNode.className = dropObject.className;

					dropObject.dropNode.parentNode.nextSibling.style.background="url('img/GUI/Tree/drop.png')";
					dropDummy = dropObject.dropNode.parentNode.nextSibling;
		    		//dropObject.dropNode.parentNode.parentNode.insertBefore(dropDummy,dropObject.dropNode.parentNode.nextSibling);

					dropObject.order = "after";
					dropObject.dropable = true;
				} else if(p_dropTypes.indexOf(dragObject.dragType)>=0) {
					//dropObject.dropNode.nextSibling.appendChild(dropDummy);
					try{
			    	   	dropDummy.style.background="url('img/GUI/Tree/divider.png')";
			        }catch(e){}
					//Browser.setDisplay(dropObject.dropNode.nextSibling,true);
					if(dropObject.className != null) {
						dropObject.dropNode.className = dropObject.className+"_active";
					}
					
					dropObject.order = "under";
					dropObject.dropable = true;
				} else {
					dropObject.dropable = false;
			        try{
			        	dropDummy.style.background="url('img/GUI/Tree/divider.png')";
			        }catch(e){}
				}
			}else{
				dropObject.dropable = false;
		        try{
		        	dropDummy.style.background="url('img/GUI/Tree/divider.png')";
		        }catch(e){}
			}
		}else{
		        dropObject.dropable = false;
		        try{
		        	dropDummy.style.background="url('img/GUI/Tree/divider.png')";
		        }catch(e){}
	    }
	    return false;
	}
	this.clearDrop = function clearDrop(){
		if(dragObject.drag){
			if(dropObject.dropable)
	    		dropObject.dropNode.className = dropObject.className;
		    dropObject.dropable = false;
	    	dropObject.dropType = null;
	    	
	    	dropObject.className = null;
		}
	}
}
var DragAndDrop = new DragAndDrop();