function drag(){
	this.Browser = Browser;
	this.dragStart = dragStart;
	this.dragGo = dragGo;
	this.dragStop = dragStop;

	this.dragObj = new Object();
	this.oldDrag = new Object();
	this.curDrag = new Object();
	this.event = new Object();
	this.isIE    = false;
	this.isNS    = false;
	this.version = null;
	this.changeBorder = true;

	this.onBgColor = "#EEEEEE";
	this.onBorder = "1px solid #888888";

	this.offBgColor = "transparent";
	this.offBorder = "0px solid #888888";

	this.dragGoFn = new Array();
	this.dragStopFn = new Array();

	this.dragStartFlag = false;
	this.browser = this.Browser();
	this.id    = null;

}
function Browser() {
	var ua, s, i;
	ua = navigator.userAgent;

	s = "MSIE";

	if ((i = ua.indexOf(s)) >= 0) {
		this.isIE = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	s = "Netscape6/";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = parseFloat(ua.substr(i + s.length));
		return;
	}

	s = "Gecko";
	if ((i = ua.indexOf(s)) >= 0) {
		this.isNS = true;
		this.version = 6.1;
		return;
	}
}
function dragStart(event) {
	if(this.dragStartFlag == false) {
		this.dragStartFlag = true;
		var el;
		var x, y;

		if (this.id) {
			var currentNode = document.getElementById(this.id);

//			var cloneDiv = currentNode.cloneNode(true);
//			cloneDiv.id = 'clone_' + this.id;
//			currentNode.parentNode.appendChild(cloneDiv);
//			this.id = cloneDiv.id;

			this.dragObj.elNode = currentNode;
			this.oldDrag = this.curDrag;
			this.curDrag = this.dragObj.elNode;

			if(this.changeBorder) {
				if(this.oldDrag.style) {
					this.oldDrag.style.backgroundColor = this.offBgColor;
					this.oldDrag.style.border = this.offBorder;
				}
				this.curDrag.style.backgroundColor = this.onBgColor;
				this.curDrag.style.border = this.onBorder;
			}
		}else {
			if (this.isIE)
				this.dragObj.elNode = window.event.srcElement;
			if (this.isNS)
				this.dragObj.elNode = event.target;
			if (this.dragObj.elNode.nodeType == 3)
				this.dragObj.elNode = this.dragObj.elNode.parentNode;
		}

		if (this.isIE) {
			x = window.event.clientX + document.documentElement.scrollLeft
			+ document.body.scrollLeft;
			y = window.event.clientY + document.documentElement.scrollTop
			+ document.body.scrollTop;
		}

		if (this.isNS) {
			x = event.clientX + window.scrollX;
			y = event.clientY + window.scrollY;
		}

		this.dragObj.cursorStartX = x;
		this.dragObj.cursorStartY = y;

//		this.dragObj.elStartLeft  = parseInt(this.dragObj.elNode.style.left, 10);
//		this.dragObj.elStartTop   = parseInt(this.dragObj.elNode.style.top,  10);

		this.dragObj.elStartLeft  = parseInt(findPosX(this.dragObj.elNode), 10);
		this.dragObj.elStartTop   = parseInt(findPosY(this.dragObj.elNode),  10);

		if (isNaN(this.dragObj.elStartLeft)) this.dragObj.elStartLeft = 0;
		if (isNaN(this.dragObj.elStartTop))  this.dragObj.elStartTop  = 0;

		this.dragObj.elNode.style.zIndex = ++this.dragObj.zIndex;

		document.dragObject  = this;
		document.onmouseup = function (evt) {
			this.dragObject.dragStop(evt);
		}
		document.onmousemove = function (evt) {
			this.dragObject.dragGo(evt);
		}
	}
}

function dragGo(event) {
	var x, y;
	if (this.isIE) {
		x = window.event.clientX + document.documentElement.scrollLeft
		+ document.body.scrollLeft;
		y = window.event.clientY + document.documentElement.scrollTop
		+ document.body.scrollTop;
	}
	if (this.isNS) {
		x = event.clientX + window.scrollX;
		y = event.clientY + window.scrollY;
	}
	this.dragObj.elNode.style.left = (this.dragObj.elStartLeft + x - this.dragObj.cursorStartX) + "px";
	this.dragObj.elNode.style.top  = (this.dragObj.elStartTop  + y - this.dragObj.cursorStartY) + "px";
	for(var i=0; i<this.dragGoFn.length; i++) {
		eval(this.dragGoFn[i] + '(this)');
	}

	if (this.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	}
	if (this.isNS)
		event.preventDefault();
}
function dragStop(event) {
	document.onmouseup = function (evt) {
		return false;
	}
	document.onmousemove = function (evt) {
		return false;
	}
	for(var i=0; i<this.dragStopFn.length; i++) {
		eval(this.dragStopFn[i] + '(this)');
	}

	this.dragStartFlag = false;
}

function stopfuction(obj) {
	var valDiv = document.getElementById('val');
	valDiv.innerHTML = obj.id;

}
function gofuction(obj) {
	var valDiv = document.getElementById('val');
	var objectDiv = document.getElementById(obj.id);
	var posX = objectDiv.offsetLeft;
	var posY = objectDiv.offsetTop;
	if(valDiv) {
		valDiv.innerHTML = "X:" + posX + " | Y:" + posY;
	}
	
}
function findPosX(obj) {
	var curleft = 0;
	if(obj) {
	if (obj.offsetParent) 	{
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curleft += obj.x;
	}
	return curleft;
	}
}

function findPosY(obj) {
	var curtop = 0;
	if(obj) {
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.x) {
		curtop += obj.x;
	}
	return curtop;
	}
}


function moveDiv(event, divDrag, id){
	var dragDiv = document.getElementById(id);
	divDrag.id = id;
	divDrag.dragObj.zIndex = 0;
	divDrag.dragStopFn[0] = 'gofuction';
	divDrag.dragStopFn[1] = 'stopfuction';
	divDrag.dragGoFn[0] = 'gofuction';
	divDrag.dragStart(event);
}
