/* --- BoxOver - Dragbox Monkey Patch ---
/* --- v 0.1 (for boxover v 2.1) - 17th August 2006
By Luca Fabbri (http://keul.it)
This script works with "boxover" library, by Oliver Bryant (http://boxover.swazz.org)

HOW TO INSTALL:
Just add this script in the head of your HTML page (AFTER the "boxover.js" inclusion)

HOW IT WORKS:
Every time the user is able to positioning on a box header (e.g: singleclickstop activated), he can
drag and move the box all over the page.

TESTED BROWSER SUPPORT:
Firefox 1.5 - Win32,GNU/Linux
IE 6.0 SP1 - Win32
Opera 9 - Win32

FINAL NOTES:
- If you find browser where this isn't working, mail me at luca AT keul DOT it (of course patch is
  appreciated!).
- If you have suggestion to make this script better than now, I will listen you...
*/

// true if mouse is pressed, false when released
var boxHeaderPressed = false;
// true if mouse pointer is inside a box header
var boxHeaderIn = false;
// start dragging position X
var startDragMouseX = null;
// start dragging position Y
var startDragMouseY = null;

// Just register the new event handler as boxover.js do...
if (typeof document.attachEvent!='undefined') {
	document.attachEvent('onmousemove',prepareDraggableBox);
} else {
	document.addEventListener('mousemove',prepareDraggableBox,false);
}

/*
	Cross browser script for get a position of an object.
	Return an array with left and top distance.
*/
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

/*
	Handler for dragging the box header
*/
function dragBox(e) {
	// If button is not old on, or we are outside the box header, do nothing
	if (!boxHeaderPressed || !boxHeaderIn) return;
	// Get mouse position
	var posx = 0;
	var posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	}
	
	// Save X and Y position when begin the drag operation
	if (startDragMouseX==null) {
		curPos = findPos(oDv);
		startDragMouseX = posx-curPos[0];
		startDragMouseY = posy-curPos[1];
	}

	// *** check for position outside window ***
	// verticalMax is different is IE or NS... Mozilla engine has "short body" on little pages...
	verticalMax = window.innerHeight?window.innerHeight:document.body.offsetHeight;
	// ... But if body height > window height, I must use body height!
	if (document.body.offsetHeight>verticalMax) verticalMax=document.body.offsetHeight;
	if (posx-startDragMouseX+oDv.offsetWidth > document.body.offsetWidth) {
		oDv.style.left = oDv.style.left = (document.body.offsetWidth-oDv.offsetWidth)+"px";
	}
	else if (posx-startDragMouseX< 0) {
		oDv.style.left = "0px";
	}
	else if (posy-startDragMouseY+oDv.offsetHeight > verticalMax) {
		oDv.style.top = oDv.style.top = (verticalMax-oDv.offsetHeight)+"px";
	}
	else if (posy-startDragMouseY< 0) {
		oDv.style.top = "0px";
	}
	// If we are inside body, can move the box
	else {
		oDv.style.left = (posx-startDragMouseX)+"px";
		oDv.style.top = (posy-startDragMouseY)+"px";
	}
}

/*
	Handler for pressing mouse button down (only inside box header)
*/
function mouseDownBox() {
	if (boxHeaderIn) boxHeaderPressed = true;
}
/*
	Handler for release mouse button
*/
function mouseUpBox() {
	boxHeaderPressed = false;
	startDragMouseX = null;
	startDragMouseY = null;
}

/*
	Handler for move inside the box header
*/
function mouseInBox() {
	boxHeaderIn = true;
}

/*
	Handler for exiting mouse header.
	We made it only if we release the mouse button, to prevent too fast mouse move.
*/
function mouseOutBox() {
	if (!boxHeaderPressed) boxHeaderIn = false;
}

/*
	Additional handler for mouse movement.
*/
function prepareDraggableBox() {
	if (dvHdr.isDraggable!=null) return;
	if (typeof document.attachEvent!='undefined') {
	   dvHdr.attachEvent('onmouseover', mouseInBox);
	   dvHdr.attachEvent('onmouseout', mouseOutBox);
	   dvHdr.attachEvent('onmousedown', mouseDownBox);
	   document.attachEvent('onmouseup', mouseUpBox);
	   document.attachEvent('onmousemove',dragBox);
	} else {
	   dvHdr.addEventListener('mouseover', mouseInBox, false);
	   dvHdr.addEventListener('mouseout', mouseOutBox, false);
	   dvHdr.addEventListener('mousedown', mouseDownBox, false);
	   document.addEventListener('mouseup', mouseUpBox, false);
	   document.addEventListener('mousemove',dragBox,false);
	}
	dvHdr.isDraggable = true;
}