
function Move(target) {

	this.target = target;

	this.v = 4;

	this.interval = 20;
	this.intervalId;

	this.sp = {x:0,y:0};
	this.ep = {x:0,y:0};
	this.pos = {x:0,y:0};
	this.angle = 0;
	this.delta = {x:0,y:0};

	this.onAfterMove = null;
	this.isMoving = false;
	this.isEventEnabled = true;
}

Move.prototype.to = function(x, y, v, isEventEnabled) {

	if (this.isMoving)
		return;

	this.isMoving = true;

	if (v != null)
		this.v = v;

	if (isEventEnabled != null) {
		this.isEventEnabled = isEventEnabled;
	}
	else {
		this.isEventEnabled = true;
	}

	this.sp.x = parseInt(this.target.style.left);
	this.sp.y = parseInt(this.target.style.top);
	this.ep.x = x;
	this.ep.y = y;
	this.pos.x = this.sp.x;
	this.pos.y = this.sp.y;

	this.angle = Math.atan2(this.ep.y - this.sp.y, this.ep.x - this.sp.x);

	if (this.sp.x == this.ep.x)
		this.delta.x = 0;
	else
		this.delta.x = this.v * Math.cos(this.angle);

	if (this.sp.y == this.ep.y)
		this.delta.y = 0;
	else
		this.delta.y = this.v * Math.sin(this.angle);

//alert("delta:" + this.delta.x + "," + this.delta.y + " pos:" + this.pos.x + "," + this.pos.y + " ep:" + this.ep.x + "," + this.ep.y);

	var self = this;
	this.intervalId = setInterval(function() { self.doMove(); }, this.interval);
}

Move.prototype.doMove = function() {

	this.pos.x += this.delta.x;
	this.pos.y += this.delta.y;

	if ((this.delta.x < 0 && this.pos.x < this.ep.x) 
		|| (this.delta.x > 0 && this.pos.x > this.ep.x) 
		|| (this.delta.y < 0 && this.pos.y < this.ep.y) 
		|| (this.delta.y > 0 && this.pos.y > this.ep.y) 
		|| (this.delta.x == 0 && this.delta.y == 0)) {

//alert("delta:" + this.delta.x + "," + this.delta.y + " pos:" + this.pos.x + "," + this.pos.y + " ep:" + this.ep.x + "," + this.ep.y);

		this.target.style.top = this.ep.y + "px";
		this.target.style.left = this.ep.x + "px";

		clearInterval(this.intervalId);
		this.isMoving = false;

		if (this.onAfterMove != null && this.isEventEnabled) {
			this.onAfterMove();
		}
		return;
	}

	var x = Math.round(this.pos.x);
	var y = Math.round(this.pos.y);

	this.target.style.top = y + "px";
	this.target.style.left = x + "px";
}

Move.prototype.setActionHandler = function(handler) {
	this.onAfterMove = handler;
}


