Drag = Class.create();

Drag.prototype = {
	
	initialize: function (target,lockToCenter,minX,maxX,minY,maxY,onMoveFunction) {
		this.target = target;
		this.parentTop = Position.cumulativeOffset(target)[1] - target.offsetTop;
		this.parentLeft = Position.cumulativeOffset(target)[0] - target.offsetLeft;
		this.lockToCenter = lockToCenter;
		this.minX = minX;
		this.maxX = maxX;
		this.minY = minY;
		this.maxY = maxY;
		this.onMoveFunction = onMoveFunction;
		this.eventMouseUp = this.stopDrag.bind(this);
		this.eventMouseMove = this.move.bindAsEventListener(this);
		Event.observe(target, "mousedown", this.startDrag.bind(this));
	},
	
	startDrag: function () {
		Event.observe(document, "mouseup", this.eventMouseUp);
		Event.observe(document, "mousemove", this.eventMouseMove);
	},
	
	stopDrag: function () {
		Event.stopObserving(document, "mouseup", this.eventMouseUp);
		Event.stopObserving(document, "mousemove", this.eventMouseMove);
	},
	
	move: function (e) {
		Event.SelectionClear();
		var xMouse = Event.pointerX(e); var yMouse = Event.pointerY(e);
		if (this.lockToCenter) { x = this.getCheckX((xMouse-this.parentLeft)-this.target.offsetWidth/2); y = this.getCheckY((yMouse-this.parentTop)-this.target.offsetHeight/2); }
		else { x = this.getCheckX(xMouse-this.parentLeft); y = this.getCheckY(yMouse-this.parentTop); }
		this.target.style.left = x + "px"; this.target.style.top = y + "px";
		if (this.onMoveFunction) this.onMoveFunction();
	},
	
	getCheckX: function (x) {
		return (x>=this.minX&&x<=this.maxX) ?x :this.target.offsetLeft;
	},
	
	getCheckY: function (y) {
		return (y>=this.minY&&y<=this.maxY) ?y :this.target.offsetTop;
	}

}