/* functions for drag&drop functionality */

var _startX = 0; 	// mouse positions 
var _startY = 0; 
var _offsetX = 0; 	// element positions 
var _offsetY = 0;
var _ZIndex = 0;
var _dragBoxes = new Array();	// box(es) that should be dragable
var _dragBox;
var _overlap = 100;

function InitDragDrop(arrElementName)
{
	// string is given instead of array
	if(typeof(arrElementName) == 'string'){
		arrElementName = new Array(arrElementName);
	}
	
	// init dragable elements
	var len = arrElementName.length;
	for(var i=0; i<len; i++) {
		_dragBoxes.push(document.getElementById(arrElementName[i]));
	}
	
	document.onmousedown 	= MouseFocus;
	document.onmouseup 		= MouseBlur;
}

function MouseFocus(e)
{
	// IE workaround
	if (e == null) 
		e = window.event; 
	
	var target = e.target != null ? e.target : e.srcElement;
	
	// IE: left click == 1
	// FF: left click == 0
	if ((e.button == 1 && window.event != null ||
	e.button == 0) && isChildOf(target, _dragBoxes)) {
		
		// mouse coordinates
		_startX = e.clientX;
		_startY = e.clientY;
		
		// element coordinates
		_offsetX = getNumber(_dragBox.style.left);
		_offsetY = getNumber(_dragBox.style.top);
		
		
		// bring the clicked element to the front while it is being dragged
		_ZIndex = _dragBox.style.zIndex;
		_dragBox.style.zIndex = 10000;
		
		// tell our code to start moving the element with the mouse
		document.onmousemove = MouseMove;
		
		// 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 MouseBlur(e)
{
	if (_dragBox != null)
	{
		_dragBox.style.zIndex = _ZIndex;

		// we're done with these events until the next OnMouseDown
		document.onmousemove = null;
		document.onselectstart = null;
		_dragBox.ondragstart = null;
	}
}



function MouseMove(e)
{
	if (e == null) 
		var e = window.event; 

	newX = _offsetX + e.clientX - _startX;
	if(newX < -_overlap) newX = -_overlap;
	if(newX > screen.availWidth - _overlap) newX = screen.availWidth - _overlap;
	
	
	newY = _offsetY + e.clientY - _startY;
	if(newY < -_overlap) newY = -_overlap;
	if(newY > screen.availHeight - _overlap) newY = screen.availHeight - _overlap;

	// move the element
	_dragBox.style.left 	= newX + 'px';
	_dragBox.style.top 		= newY + 'px';	
}

function getNumber(value)
{
    var num = parseInt(value);
	
    return num == null || isNaN(num) ? 0 : num;
}


function isChildOf(ChildObject, arrContainerObjects) 
{ 
	var len = arrContainerObjects.length;
	for (var i = 0; i < len; i++) {
		var objCurrent;
		if (typeof(arrContainerObjects[i]) == "string") {
			arrContainerObjects[i] = document.getElementById(arrContainerObjects[i]);
		}
		if (typeof(ChildObject) == "string") {
			ChildObject = document.getElementById(ChildObject);
		}
		objCurrent = ChildObject.parentNode;
		
		while (objCurrent != undefined) {
			if (objCurrent == document.body) {
				break;
			}
			if (objCurrent.id == arrContainerObjects[i].id) {
				// set found box to current dragable box
				_dragBox = arrContainerObjects[i];
				return true;
			}
			objCurrent = objCurrent.parentNode; // go up 
		}
	}
	
	return false
 } 
 

