//--------------------------------------------------------------------------------
//--------------------------------------------------------------------------------
// drag element code (copied from internet)
// needs event.js and position.js

// drag class
function class_drag (
	element,       // main element to drag
	handle,        // handle element used to drag the main element
	lowerBound,    // one corner of the drag bound area
	upperBound,    // one corner of the drag bound area
	startCallback, // function called when dragging starts
	moveCallback,  // function called when dragging is occuring
	endCallback,   // function called when dragging stops
	attachLater)   // flag to delay dragging until StartListening is called
{

	// check for a valid, existing element
	if (typeof (element) == "string")  element = document.getElementById (element);
	if (element == null)  return;

	// find the absolute x,y position of the triggered event object
	function absoluteCursorPostion (eventObj) {
		eventObj = eventObj ? eventObj : window.event;
		if (isNaN (window.scrollX))
			return new class_position (
				eventObj.clientX + document.documentElement.scrollLeft + document.body.scrollLeft, 
				eventObj.clientY + document.documentElement.scrollTop + document.body.scrollTop);
		else
			return new class_position (
				eventObj.clientX + window.scrollX,
				eventObj.clientY + window.scrollY);
	} //function

	// swap the drag bound area corners if needed
	function swapBounds() {
		if (lowerBound != null && upperBound != null) {
			var temp = lowerBound.Min (upperBound);
			upperBound = lowerBound.Max (upperBound);
			lowerBound = temp;
		} //if
	} //function
	swapBounds();

	// set the lower/upper bounds
	this.setBounds = function (lower, upper) {
		lowerBound = lower;
		upperBound = upper;
		swapBounds();
	} //function - method

	// local variables
	var cursorStartPos = null;
	var elementStartPos = null;
	var dragging = false;
	var listening = false;
	var disposed = false;

	// initialize the drag handle - use the main element as the drag handle if needed
	if (typeof (handle) == "string")  handle = document.getElementById (handle);
	if (handle == null)  handle = element;
	
	function dragStart (eventObj) {
 		if (dragging || !listening || disposed)  return;
		dragging = true;
		if (startCallback != null)  startCallback (eventObj, element);
		cursorStartPos = absoluteCursorPostion (eventObj);
		elementStartPos = new class_position (parseInt (element.style.left), parseInt (element.style.top));
		elementStartPos = elementStartPos.Check();
		hookEvent (document, "mousemove", dragGo);
		hookEvent (document, "mouseup", dragStopHook);
		return cancelEvent (eventObj);
	} //function

	function dragGo (eventObj) {
		if (!dragging || disposed)  return;
		var newPos = absoluteCursorPostion (eventObj);
		newPos = newPos.Add (elementStartPos).Subtract (cursorStartPos);
		newPos = newPos.Bound (lowerBound, upperBound)
		newPos.Apply (element);
		if (moveCallback != null)  moveCallback (newPos, element);
		return cancelEvent (eventObj);
	} //function

	function dragStopHook (eventObj) {
		dragStop();
		return cancelEvent (eventObj);
	} //function

	function dragStop() {
		if (!dragging || disposed)  return;
		unhookEvent (document, "mousemove", dragGo);
		unhookEvent (document, "mouseup", dragStopHook);
		cursorStartPos = null;
		elementStartPos = null;
		if (endCallback != null)  endCallback (element);
		dragging = false;
	} //function

	this.Dispose = function() {
		if (disposed)  return;
		this.StopListening (true);
		element = null;
		handle = null
		lowerBound = null;
		upperBound = null;
		startCallback = null;
		moveCallback = null
		endCallback = null;
		disposed = true;
	} //function - method

	this.StartListening = function() {
		if (listening || disposed)  return;
		listening = true;
		hookEvent (handle, "mousedown", dragStart);
	} //function - method

	this.StopListening = function (stopCurrentDragging) {
		if (!listening || disposed)  return;
		unhookEvent (handle, "mousedown", dragStart);
		listening = false;
		if (stopCurrentDragging && dragging)  dragStop();
	} //function - method

	this.IsDragging = function() { return dragging; }
	this.IsListening = function() { return listening; }
	this.IsDisposed = function() { return disposed; }

	// delay dragging until later if needed
	if (!attachLater)  this.StartListening();

} //function - class


