// NOTE: requires prototype.js
/* Use something like the following to set up this object:
Event.observe(window,'load', function() {
  Event.observe(document,'mousemove', name_your_variable.dragMove);
});
This class is designed to be instatiated once per page and then used by multiple
draggable windows one at a time.
*/
function tbWindowDragger()
{
	// Private variables
	var containerobj = null;
	var curX;
	var curY;
	var mouX;
	var mouY;
	var scrollTracker;
	if(Prototype.Browser.Opera)
	{
		scrollTracker = {left:0,top:0};
	}
	else
	{
		scrollTracker = document.viewport.getScrollOffsets();
		Event.observe(window,'scroll', scrollChange);
	}

	// inob:
	// ev = the event that fired this call. In IE this will be 'undefined' or null, but picked up in the first line
	// containerobj = the element to be dragged
	this.init = function(inob)
	{
		containerobj = inob.containerobj;
		var ev = window.event ? window.event : inob.ev;
		var c = Element.positionedOffset(inob.containerobj);
		curX = c.left;
		curY = c.top;
		mouX = Event.pointerX(ev) - scrollTracker.left;
		mouY = Event.pointerY(ev) - scrollTracker.top;
	}

	// Called when dragging is no longer wanted
	this.dragEnd = function() {
		if(containerobj == null) return;
		containerobj.setStyle({cursor:'default'});
		containerobj = null;
	}
	// This method is registered with the system to monitor mouse moves at the appropriate time.
	// To avoid unecessary overhead, this method should be registered when needed and then unregistered
	// when no longer required. Example: register with the 'mousedown' event and unregister on the
	// 'mouseup' and 'mouseout' events.
	this.dragMove = function(ev){
		ev = window.event ? window.event : ev;
		if(containerobj == null) 
		{
			mouX = Event.pointerX(ev) - scrollTracker.left;
			mouY = Event.pointerY(ev) - scrollTracker.top;
			return;
		}
		var pX = Event.pointerX(ev) - scrollTracker.left;
		var pY = Event.pointerY(ev) - scrollTracker.top;
		curX += (pX - mouX);
		curY += (pY - mouY);
		containerobj.setStyle({left:curX+'px',top:curY+'px'});
		mouX = pX;
		mouY = pY;
	}

	// Registered to catch all window scrolling movements	
	function scrollChange(ev)
	{
		scrollTracker = document.viewport.getScrollOffsets();
	}

}
