//------------------------------------------------------
// The Image Animation Object
Animator.prototype = new CBSObject();
Animator.prototype.constructor = Animator;
function Animator()
{
  this.superClass = CBSObject.prototype;
  this.superClass.constructor.call(this);
	this.objectType = "Animator";
	_objects[this.objectId] = this;
	
	this.offsetX = 0;
	this.offsetY = 0;
}
Animator.prototype.cloneImage = function(img) {
	var originalImage;
	if (typeof(img) == "object") {
		originalImage = img;
	} else {
		originalImage = document.getElementById(img);
	}

	var pos = getElementPosition(originalImage);

	var image = document.createElement("img");
	image.setAttribute("src", originalImage.src);
	image.setAttribute("width", originalImage.width);
	image.setAttribute("height", originalImage.height);
	image.setAttribute("border", originalImage.border);
	image.style.position = "absolute";
	image.style.left = pos.x;
	image.style.top = pos.y;
	document.body.appendChild(image);
	
	return image;
}
Animator.prototype.createImage = function(src, x, y, width, height, border) {
	var image = document.createElement("img");
	this.setImage(image, src, x, y, width, height, border);
	document.body.appendChild(image);
	
	return image;
}
Animator.prototype.setImage = function(image, src, x, y, width, height, border) {
	image.setAttribute("src", src);
	image.style.position = "absolute";
	image.style.left = x;
	image.style.top = y;
	if (width) image.setAttribute("width", width);
	if (height) image.setAttribute("height", height);
	if (border) image.setAttribute("border", border);
	
	return image;
}
Animator.prototype.animate = function(o, x, y, width, height, steps, speed) {
	var object;
	if (typeof(o) == "object") {
		object = o;
	} else {
		object = document.getElementById(o);
	}
	
	return new Animation(this, object, x, y, width, height, steps, speed);
}


//------------------------------------------------------
// The Animation Object
Animation.prototype = new CBSObject();
Animation.prototype.constructor = Animation;
function Animation(animator, object, x, y, width, height, steps, speed)
{
  this.superClass = CBSObject.prototype;
  this.superClass.constructor.call(this);
	this.objectType = "Animation";
	_objects[this.objectId] = this;

	this.animator = animator;
	this.object = object;
		
	var pos = getElementPosition(this.object);
	this.startX = pos.x
	this.startY = pos.y;
	this.startWidth = this.object.width;
	this.startHeight = this.object.height;	

	this.endX = x;
	this.endY = y;
	this.endWidth = (width < 0) ? this.startWidth : width;
	this.endHeight = (height < 0) ? this.startHeight : height;

	this.speed = speed;
	this.numSteps = steps;
	this.currentStep = 0;

	window.setTimeout("_objects[" + this.objectId + "].callback()", this.speed);
}
Animation.prototype.callback = function() {
	this.currentStep = this.currentStep + 1;
	var x = this.animator.offsetX + this.startX + (this.endX - this.startX) * this.currentStep / this.numSteps;
	var y = this.animator.offsetY + this.startY + (this.endY - this.startY) * this.currentStep / this.numSteps;
	var width = this.startWidth + (this.endWidth - this.startWidth) * this.currentStep / this.numSteps;
	var height = this.startHeight + (this.endHeight - this.startHeight) * this.currentStep / this.numSteps;
	
	this.object.width = width;
	this.object.height = height;
	this.object.style.left = x;
	this.object.style.top = y;
	
	this.animator._fireEvent("onAnimationStep", this);
	
	if (this.currentStep < this.numSteps) {
		window.setTimeout("_objects[" + this.objectId + "].callback()", this.speed);
	} else {
		this.animator._fireEvent("onAnimationDone", this);
	}
}

