﻿// The code below is a slightly ammended version of the code at
// http://luke.breuer.com/tutorial/javascript-drag-and-drop-tutorial.aspx
// Changes have been made to scroll the page not drag an object.





var _startX = 0;			// mouse starting positions
var _startY = 0;
var _offsetX = 0;			// current element offset
var _offsetY = 0;
var _dragElement;			// needs to be passed from OnMouseDown to OnMouseMove
var _oldZIndex = 0;			// we temporarily increase the z-index during drag


InitDragDrop();

function InitDragDrop()
{
	document.onmousedown = OnMouseDown;
	document.onmouseup = OnMouseUp;
}

function OnMouseDown(e)
{
	// IE is retarded and doesn't pass the event object
	if (e == null) 
		e = window.event; 
	
	// IE uses srcElement, others use target
	var target = e.target != null ? e.target : e.srcElement;
	
	// for IE, left click == 1
	// for Firefox, left click == 0
	if (e.button == 1 && window.event != null || 
		e.button == 0)
	{
		// grab the mouse position
		_startX = e.clientX;
		_startY = e.clientY;
		x = document.body.scrollLeft + e.clientX;
	    y = document.body.scrollTop + e.clientY;
		
		// first check that the scrollbars hasn't been clicked
		// otherwise internet explorer gets serverly confused
		var cw = document.body.clientWidth;
		var ch = document.body.clientHeight;
		if(_startX>cw-2||_startY>ch-2) // -2 is required to prevent firefox getting confused when righ on edge of scrollbar
		// if scrollbar has been clicked then do nothing
		{
		}
        else{
        // otherwise run code
		
		
		// bring the clicked element to the front while it is being dragged
		_oldZIndex = target.style.zIndex;
		target.style.zIndex = 10000;
		
		// we need to access the element in OnMouseMove
		_dragElement = target;

		// tell our code to start moving the element with the mouse
		document.onmousemove = OnMouseMove;
		
		// cancel out any text selections
		document.body.focus();
		
		// prevent text selection in IE
		document.onselectstart = function () { return false; };
		// prevent IE from trying to drag an image
		target.ondragstart = function() { return false; };
		
		// prevent text selection (except IE)
		return false;
	}
}
}
function ExtractNumber(value)
{
	var n = parseInt(value);
	
	return n == null || isNaN(n) ? 0 : n;
}

function OnMouseMove(e)
{
document.body.style.cursor='move'

	if (e == null) 
		var e = window.event; 

	// this is the actual "scroll code"

	scrollTo(x-e.clientX,y-e.clientY);
}

function OnMouseUp(e)
{
document.body.style.cursor='auto'

	if (_dragElement != null)
	{
		_dragElement.style.zIndex = _oldZIndex;

		// we're done with these events until the next OnMouseDown
		document.onmousemove = null;
		document.onselectstart = null;
		_dragElement.ondragstart = null;

		// this is how we know we're not dragging
		_dragElement = null;
		
	}
}
//-->
