// JavaScript Document

var Tween = (function(){
	function constructor(obj,property,oHeight,fHeight,time,units){
		this.obj = obj;
		this.property = property;
		this.oh = oHeight;
		this.fh = fHeight;
		this.intervalRate = 10;
		this.position = 0;
		this.steps = Math.round(time/this.intervalRate);
		this.units = units;
		this.d;
		this.playing = null;
		this.ease = "easeSin";
	}
	
	function easeSin(instance){//returns a number between instance.oh and instance.fh 
		return (Math.sin((instance.position/instance.steps)*Math.PI/2)*(instance.fh-instance.oh)+instance.oh);
	}
	
	function startInterval(instance){
		instance.playing = setInterval(function(){instance.animate()}, instance.intervalRate);
	}
	
	constructor.prototype.play = function(){
		if(this.position == 0){
			this.stop();
			this.d = 1;
			startInterval(this);
		} else if(this.position == this.steps){
			this.stop();
			this.d = -1;
			startInterval(this);
		}
	};
	
	constructor.prototype.stop = function(){
		clearInterval(this.playing);
		this.playing = null;
	};
	
	constructor.prototype.animate = function(d){
		this.position += this.d;
		this.obj.style[this.property] = (eval(this.ease))(this)+this.units;
		if(this.position == this.steps || this.position == 0){this.stop();};
	};

	return constructor;
})();