
function MapPath(shape, points) {

	this.v = 4;

	this.shape = shape;
	this.shape.path = " ";

	this.points = points;
	this.index = 0;
	this.endIndex;

	this.sp = {x:0,y:0};
	this.ep = {x:0,y:0};
	this.pos = {x:0,y:0};
	this.delta = {x:0,y:0};
	this.angle;

	this.vmlPath = "";

	this.intervalId;

	this.onAfterDrawing = null;
}

MapPath.prototype.draw = function(endIdx) {

	this.endIndex = endIdx;

	this.setPos();

	if (this.vmlPath == "") {
		this.vmlPath += "m " + this.sp.x + "," + this.sp.y + " l";
	}
	else {
		this.vmlPath += " " + this.sp.x + "," + this.sp.y;
	}

	var self = this;
	this.intervalId = setInterval(function() { self.stretchPolyline(); }, 80);
}

MapPath.prototype.setPos = function() {

	this.sp.x = this.points[this.index].x;
	this.sp.y = this.points[this.index].y;
	this.ep.x = this.points[this.index + 1].x;
	this.ep.y = this.points[this.index + 1].y;
	this.pos.x = this.sp.x;
	this.pos.y = this.sp.y;

	this.calcDelta();
}

MapPath.prototype.calcDelta = function() {

	this.angle = Math.atan2(this.ep.y - this.sp.y, this.ep.x - this.sp.x);

	this.delta.x = this.v * Math.cos(this.angle);
	this.delta.y = this.v * Math.sin(this.angle);
}

MapPath.prototype.stretchPolyline = 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)) {

		this.vmlPath += " " + this.ep.x + "," + this.ep.y;
		this.shape.path = this.vmlPath + " e";

		if (this.index == this.endIndex - 1) {
			this.index = this.endIndex;
			clearInterval(this.intervalId);
			if (this.onAfterDrawing != null) {
				this.onAfterDrawing();
			}
			return;
		}
		else {
			this.index++;
			this.setPos();
			this.pos.x += this.delta.x;
			this.pos.y += this.delta.y;
		}
	}

	var x = Math.round(this.pos.x);
	var y = Math.round(this.pos.y);
	this.vmlPath += " " + x + "," + y;
	this.shape.path = this.vmlPath + " e";
}

MapPath.prototype.setActionHandler = function(handler) {
	this.onAfterDrawing = handler;
}

