/* Copyright Schools on the Net 2006. */

var DragDropBase = {
	// Distance that the mouse must travel at the start of a drag before the drag begins (squared).
	dragTolerance: 25,
	
	_beginDrag : function(e, element, drag, endDrag, initBlock) {
		if(!e) var e = window.event;
		
		var mousePosition = getMousePosition(e);
		var elemPosition = getElementsOverallPosition(element);
		
		var offset = { x : mousePosition.x - elemPosition.x, y : mousePosition.y - elemPosition.y };
		
		DragDropBase._elementToDrag = element;
		DragDropBase._ghostElement = null;
		DragDropBase._dragStart = mousePosition;
		DragDropBase._offsetVector = offset;
		
		document.onmousemove = drag;
		document.onmouseup = endDrag;
		
		if(initBlock != null) {
			initBlock(DragDropBase._elementToDrag);
		}
		
		return false;
	},
	
	_updateDrag : function(e, useGhost, updateBlock) {
		if(!e) var e = window.event;
		
		if(DragDropBase._elementToDrag == null) {
			return true;
		}
		
		var mousePosition = getMousePosition(e);
		var draggedElement = DragDropBase._elementToDrag;
		var draggedElementPosition = getElementsOverallPosition(draggedElement);
		
		if(DragDropBase._inDrag == false) {
			// Has the mouse travelled far enough to begin the drag?
			var dx = mousePosition.x - DragDropBase._dragStart.x;
			var dy = mousePosition.y - DragDropBase._dragStart.y;
			if((dx*dx+dy*dy) < DragDropBase.dragTolerance) {
				return true;
			}
			
			// Begin dragging.
			DragDropBase._inDrag = true;
			
			if(useGhost == true) {
				// Create the ghost.
				var ghost = draggedElement.cloneNode(true);
				
				ghost.style.position = "absolute";
				ghost.style.left = draggedElementPosition.x.toString() + "px";
				ghost.style.top = draggedElementPosition.y.toString() + "px";
				ghost.style.cssFloat = ghost.style.styleFloat = "none";
				
				ghost.style.opacity = "0.5";
				ghost.style.filter = "alpha(opacity=50)";
				
				document.body.appendChild(ghost);			
				DragDropBase._ghostElement = ghost;
			}
		}

		if(useGhost == true) {		
			// Move the ghost to the cursor position.
			var ghost = DragDropBase._ghostElement;
			
			ghost.style.left = (mousePosition.x - DragDropBase._offsetVector.x).toString() + "px";
			ghost.style.top = (mousePosition.y - DragDropBase._offsetVector.y).toString() + "px";
		}
		
		updateBlock(draggedElement, mousePosition);
		
		return false;
	},
	
	_finishDrag : function(e, saveBlock) {
		if(DragDropBase._ghostElement != null) {
			DragDropBase._ghostElement.parentNode.removeChild(
				DragDropBase._ghostElement);
			DragDropBase._ghostElement = null;
		
			if(DragDropBase._elementToDrag != null) {
				saveBlock(DragDropBase._elementToDrag);
			}
		}
		
		DragDropBase._elementToDrag = null;
		DragDropBase._offsetVector = null;
		DragDropBase._inDrag = false;
		DragDropBase._elementIndex = -1;
		
		document.onmouseup = null;
		document.onmousemove = null;
		
		return true;
	},
	
	_elementToDrag : null,
	_ghostElement : null,
	_dragStart: null,
	_offsetVector : null,
	_inDrag : false
};
