// Global object to hold drag information.
var dragObj = new Object();
dragObj.zIndex = 0;
dragObj.x = 0;
dragObj.y = 0;
dragObj.sandbox = { top:-1, left:-1, bottom:-1, right:-1 };
dragObj.restrictMovement = function ( rect ) {
	// make sure rect is in the right order
	this.sandbox = {
		top : Math.min( rect.top, rect.bottom ),
		left : Math.min( rect.left, rect.right ),
		bottom : Math.max( rect.top, rect.bottom ),
		right : Math.max( rect.left, rect.right )
	};
};

function onDragMove() { return; }

function moveDrag( event ) {
	var mouse = getMouseCoordsByEvent( event );
	var newX = dragObj.elStartLeft + mouse.x - dragObj.cursorStartX;
	var newY = dragObj.elStartTop  + mouse.y - dragObj.cursorStartY;
	if( dragObj.sandbox.left > -1 ) {
		// if the sandbox has been set, restrict the movement of the thing
		if( newX < dragObj.sandbox.left ) newX = dragObj.sandbox.left;
		if( newX > dragObj.sandbox.right ) newX = dragObj.sandbox.right;
		if( newY < dragObj.sandbox.top ) newY = dragObj.sandbox.top;
		if( newY > dragObj.sandbox.bottom ) newY = dragObj.sandbox.bottom;
	}
	dragObj.elNode.style.left = newX + "px";
	dragObj.elNode.style.top = newY + "px";
	if( isIE ) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		event.preventDefault();
	}
	dragObj.x = newX;
	dragObj.y = newY;
	onDragMove();
}

function stopDrag( event ) {
	// Clear the drag element global.
	dragObj.elNode = null;
	// Stop capturing mousemove and mouseup events.
	if( isIE ) {
		document.detachEvent( "onmousemove", moveDrag );
		document.detachEvent( "onmouseup", stopDrag );
	} else {
		document.removeEventListener( "mousemove", moveDrag, true );
		document.removeEventListener( "mouseup", stopDrag, true );
	}
}

function startDrag( event, id ) {
	var el;
	// If an element id was given, find it. Otherwise use the element being
	// clicked on.
	if( id ) {
		dragObj.elNode = document.getElementById( id );
	} else {
		if( isIE )
			dragObj.elNode = window.event.srcElement;
		else
			dragObj.elNode = event.target;
		// If this is a text node, use its parent element.
		if( dragObj.elNode.nodeType == 3 )
			dragObj.elNode = dragObj.elNode.parentNode;
	}
	// get mouse location
	var mouse = getMouseCoordsByEvent( event );
	// Save starting positions of cursor and element.
	dragObj.cursorStartX = mouse.x;
	dragObj.cursorStartY = mouse.y;
	dragObj.elStartLeft = parseInt( dragObj.elNode.style.left, 10 );
	dragObj.elStartTop = parseInt( dragObj.elNode.style.top,  10 );
	if( isNaN( dragObj.elStartLeft ) ) dragObj.elStartLeft = 0;
	if( isNaN( dragObj.elStartTop ) ) dragObj.elStartTop = 0;
	// Update element's z-index.
	dragObj.elNode.style.zIndex = ++dragObj.zIndex;
	// Capture mousemove and mouseup events on the page.
	if( isIE ) {
		document.attachEvent( "onmousemove", moveDrag );
		document.attachEvent( "onmouseup", stopDrag );
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		document.addEventListener( "mousemove", moveDrag, true );
		document.addEventListener( "mouseup", stopDrag, true );
		event.preventDefault();
	}
}
