var dragObject  = null;
var mouseOffset = null;
var originalX = null;
var originalY = null;

function GetObject(id)
{
	return document.getElementById(id);
}

function MouseMove(ev)
{
	ev = ev || window.event;
	var mousePos = MouseCoords(ev);
	if(dragObject)
	{	
		var newX = parseInt(originalX + (mousePos.x - mouseOffset.x));
		var newY = parseInt(originalY + (mousePos.y - mouseOffset.y));
		GetObject(dragObject.id).style.left = newX;
		GetObject(dragObject.id).style.top = newY;
		return false;
	}

	return true;
}

function MouseUp(ev)
{
	dragObject = null;
}

function MouseCoords(ev)
{
	if(ev.pageX || ev.pageY)
	{
		return {x:ev.pageX, y:ev.pageY};
	}
	
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

function MakeDraggable(obj, targetObj)
{
	if(!targetObj) return;

	targetObj.onmousedown = function(ev)
	{
		if(!dragObject)
		{
			ev = ev || window.event;
			mouseOffset = MouseCoords(ev);
			dragObject  = obj;
			originalX = parseInt(dragObject.style.left);
			originalY = parseInt(dragObject.style.top);
		}

		return false;
	}
}

document.onmousemove = MouseMove;
document.onmouseup = MouseUp;