Object.clone = function (o) {
  function c(o) {
    for (var i in o) {
      this[i] = o[i];
    }
  }
 
  return new c(o);
};

Object.updateProperties = function(dest, src) {
    for (var i in src) {
      dest[i] = src[i];
    }
};

var ScalingImage = function (options) {
	Object.updateProperties(this, ScalingImage.defaults);
	Object.updateProperties(this, options);
	this.img = document.getElementById(this.id);
	this.origWidth = this.img.width;
	this.origHeight = this.img.height;
	this.origScale = this.currentScale;
}

ScalingImage.defaults = {
	id: null,
	currentScale: 1,
	finalScale: 2,
	scaleIncrement: 0.1,
	interval: 500,
	scaleFunction: null,
	offset: null,
	stopped: false,
	doneFunction: function () {}
}

ScalingImage.prototype.setOpacity = function (opacity) {
	if (navigator.appName.indexOf("Netscape")!=-1 &&parseInt(navigator.appVersion)>=5)
		    this.img.style.MozOpacity=opacity/100
	else if (navigator.appName.indexOf("Microsoft")!= -1 &&parseInt(navigator.appVersion)>=4)
	    this.img.filters.alpha.opacity=opacity
}

ScalingImage.prototype.setScale = function (scale) {
	this.currentScale = scale;
	this.img.width = parseInt(scale * this.origWidth);
	this.img.height = parseInt(scale * this.origWidth);
	
	if (this.offset) {
		this.img.style.position = 'relative';
		this.img.style.left = parseInt(Math.floor(-1 * scale * this.offset[0]) + this.offset[0]) + "px";
		this.img.style.top = parseInt(Math.floor(-1 * scale * this.offset[1]) + this.offset[1]) + "px"
	}
}

ScalingImage.prototype.run = function () {
	if (this.stopped) return;
	
	// avoid rounding errors
	if (this.scaleIncrement < 0 && this.currentScale <= this.finalScale) {
		this.doneFunction()
		return;
	} else if (this.scaleIncrement > 0 && this.currentScale >= this.finalScale) {
		this.doneFunction();
		return;
	}
	if (!this.scaleFunction) {
		this.setScale(this.currentScale + this.scaleIncrement);
	} else {
		this.scaleFunction(this);
	}
	var _self = this;
	setTimeout(function () { _self.run() }, this.interval )
}

