function SimpleAnimation(oncomplete)
{
	this.duration=500;
	this.from=0.0;
	this.to=1.0;
	if(oncomplete!=null){
		this.oncomplete=oncomplete;
	}
}
SimpleAnimation.prototype.animationComplete=function()
{
	delete this.animation;
	delete this.animator;
	this.post();
	if(this.oncomplete!=null){
		this.oncomplete();
	}
}
SimpleAnimation.prototype.stop=function()
{
	if(this.animation!=null){
		this.animator.stop();
		this.post();
		delete this.animation;
		delete this.animator;
	}
}
SimpleAnimation.prototype.restart=function(callback)
{
	this.stop();
	if (callback!=undefined&&callback!=null) {
		callback();
	}
}
SimpleAnimation.prototype.start=function()
{
	this.stop();
	this.pre();
	var self=this;
	var animator=new Animator(this.duration,13);
	animator.oncomplete=function(){
		self.animationComplete();
	};
	this.animator=animator;
	this.framecount=0;
	var update=function(animation,now,first,done){
		self.update(now);
		++self.framecount;
	}
	this.animation=new Animation(this.from,this.to,update);
	animator.addAnimation(this.animation);
	animator.start();
}
SimpleAnimation.prototype.pre=function()
{}
SimpleAnimation.prototype.post=function()
{}
SimpleAnimation.prototype.update=function(now){}

function Animator(duration,interval,optionalFrom,optionalTo,optionalCallback)
{
	this.startTime=0;
	this.duration=duration;
	this.interval=interval;
	this.animations=new Array;
	this.timer=null;
	this.oncomplete=null;
	this._firstTime=true;
	var self=this;
	this.animate=function(self){
		function limit_3(a,b,c){
			return a<b?b:(a>c?c:a);
		}
		var T,time;
		var ease;
		var time=(new Date).getTime();
		var dur=self.duration;
		var done;
		T=limit_3(time-self.startTime,0,dur);
		time=T/dur;
		ease=0.5-(0.5*Math.cos(Math.PI*time));
		done=T>=dur;
		var array=self.animations;
		var c=array.length;
		var first=self._firstTime;
		for(var i=0;i<c;++i)
		{
			array[i].doFrame(self,ease,first,done,time);
		}
		if(done)
		{
			self.stop();
			if(self.oncomplete!=null)
			{
				self.oncomplete();
			}
		}
		self._firstTime=false;
	}
	if(optionalFrom!==undefined&&optionalTo!==undefined&&optionalCallback!==undefined)
	{
		this.addAnimation(new Animation(optionalFrom,optionalTo,optionalCallback));
	}
}
Animator.prototype.start=function(){
	if(this.timer==null)
	{
		var timeNow=(new Date).getTime();
		var interval=this.interval;
		this.startTime=timeNow-interval;
		this.timer=setInterval(this.animate.bind(null,this),interval);
	}
}
Animator.prototype.stop=function(){
	if(this.timer!=null)
	{
		clearInterval(this.timer);
		this.timer=null;
	}
}
Animator.prototype.addAnimation=function(animation)
{
	this.animations[this.animations.length]=animation;
}
function Animation(from,to,callback)
{
	this.from=from;
	this.to=to;
	this.callback=callback;
	this.now=from;
	this.ease=0;
	this.time=0;
}
Animation.prototype.doFrame=function(animation,ease,first,done,time){
	var now;
	if(done)
		now=this.to;
	else
		now=this.from+(this.to-this.from)*ease;
	this.now=now;
	this.ease=ease;
	this.time=time;
	this.callback(animation,now,first,done);
}
