// animatelib
// based on webfx animation toolkit by erik arvidsson <erik@eae.net>

var animIndex = 0;
var animArray = new Array();

function Animator(id, p, period) {
	this.play = int_play;
	this.pause = int_pause;
	this.stop = int_stop;
	this.onanimationdone;
	this.elstyle = null;	
	this.path = p;
	this.msec = period;
	this.id = id;
	
	this.index = animIndex;
	animArray[this.index] = this;
	this.thisString = "animArray[" + this.index + "]";
	animIndex++;
	
	function int_play() {
		if (this.elstyle == null) {
			this.elstyle = (document.all != null) ? document.all[this.id].style : document.layers[this.id];
		}
		if (this.path.step()) {
			this.elstyle.left = this.path.x;
			this.elstyle.top  = this.path.y;
			animArray[this.index].timer = setTimeout(this.thisString + ".play()", this.msec);
		}
		else if (this.onanimationdone != null)
			eval(this.onanimationdone);
	}
	
	function int_pause() {
		clearTimeout(animArray[this.index].timer);
	}
	
	function int_stop() {
		clearTimeout(animArray[this.index].timer);
		this.path.reset();
	}
}

function Path() {
	this.concat = int_concat;
	this.add = int_add;
	this.rotate = int_rot;
	
	function int_concat(p) {
		return new PathList(new Array(this, p));
	}
	
	function int_add(p) {
		return new PathAdd(this, p);
	}
	
	function int_rot(xc,yc,v) {
		return new RotatePath(this, xc, yc, v);
	}
}

function StraightPath(fromX, fromY, toX, toY, n) {
	// all path objects must have these 5 methods
	this.x  = fromX; // retrieves the current x value
	this.y  = fromY;
	this.step = int_step;			// move to next step. returns true if the step was succesful, returns false when the path has been done

	this.reset = int_reset;
	// The rest may vary from different path objects

	this.startX = fromX;
	this.startY = fromY;
	this.endX = toX;
	this.endY = toY;

// Initiate steps
 	this.steps = n;
 	this.totalSteps = n;
 	if (this.totalSteps < 1) {	// No Amimation!
 		this.x = this.endX;
 		this.y = this.endY;
 		this.deltaX = 0;	// NN work around
 		this.deltaY = 0;
 	}
	else {
	 	this.deltaX = (this.endX - this.startX) / this.totalSteps;
		this.deltaY = (this.endY - this.startY) / this.totalSteps;
	}

	function int_step() {
		if (this.steps >= 0) {
			this.steps--;
			this.x += this.deltaX;
			this.y += this.deltaY;
		}
		return (this.steps >= 0 );
	}
	
	function int_reset() {
		if (this.totalSteps < 1) {
			this.steps = 0;
			this.x = this.endX;
			this.y = this.endY;
		}
		else {
			this.steps = this.totalSteps;
			this.x = this.startX;
			this.y = this.startY;
		}
	}
}

StraightPath.prototype = new Path;
