Resize.INTERVAL_LENGTH = 30;

function Resize(target, duration, widthTo, heightTo, widthFrom, heightFrom) {
	this.target = target;

	if (typeof target != 'object' || target.constructor != Array) {
	  this.target = new Array(1);
	  this.target[0] = target;
	}

	this.widthTo = widthTo;
	this.heightTo = heightTo;

	this.widthFrom = widthFrom;
	if (this.widthFrom == null) {
		this.widthFrom = this.target[0].offsetWidth;
	}
	this.heightFrom = heightFrom;
	if (this.heightFrom == null) {
		this.heightFrom = this.target[0].offsetHeight;
	}

	duration = duration ? duration : 500;
	this.numIntervals = (duration / Resize.INTERVAL_LENGTH);

	this.stepX = (this.widthTo - this.widthFrom) / this.numIntervals;
	this.stepY = (this.heightTo - this.heightFrom) / this.numIntervals;

	this.updateSize(this.widthFrom, this.heightFrom);

	this.currentWidth = this.widthFrom;
	this.currentHeight = this.heightFrom;
}

Resize.prototype.registerCallback = function(functionRef) {
	this.callback = functionRef;
}

Resize.prototype.start = function() {
	var obj = this;
	var functionReference = this.increment;
	var callback = function() { functionReference.call(obj) };
	this.interval = setInterval(callback, Resize.INTERVAL_LENGTH);
}

Resize.prototype.stop = function() {
	clearInterval(this.interval);
}

Resize.prototype.end = function() {
	clearInterval(this.interval);
	this.updateSize(this.widthTo, this.heightTo);
	if (this.callback) {
		this.callback();
		this.callback = null;
	}
}

Resize.prototype.cancel = function() {
	clearInterval(this.interval);
	this.updateSize(this.widthFrom, this.heightFrom);
}

Resize.prototype.increment = function() {
	this.numIntervals --;
	if (this.numIntervals > 0) {
		this.currentWidth += this.stepX;
		this.currentHeight += this.stepY;
		this.updateSize(this.currentWidth, this.currentHeight);
	} else {
		this.end();
	}
}

Resize.prototype.updateSize = function(newWidth, newHeight) {
	for (i = 0; i < this.target.length; i++) {
		if (this.stepX) {
			this.target[i].style.width = Math.round(newWidth);
		}
		if (this.stepY) {
			this.target[i].style.height = Math.round(newHeight);
		}
	}
}

