// JavaScript Document

var Animation = function( am, img, seconds, effects )
{
  this.img = img;
  this.AnimationManager = am;
  this.seconds = seconds;
  this.effects = effects;
  this.startMS = 0;
}

Animation.prototype = {
	
	start:function(){

	  this.AnimationManager.add( this );
	  this.startMS = 0;
	
	  this.img.hide();
	  
	  for( var e in this.effects )
	  	if (this.effects.hasOwnProperty(e)) {
		  {
			this.effects[e].apply( 0 );
		  }
	  
	  }

		this.img.show();

	},
	animate:function(){
	  var d = new Date();
	  if ( this.startMS == 0 )
		this.startMS = d.valueOf();
	
	  var p = (((d.valueOf()-this.startMS)/1000)/this.seconds)*100;
	  for( var e in this.effects ){
		if(this.effects.hasOwnProperty(e)){
			this.effects[e].apply( p );
		}
	  }
	},
	
	done:function(){
	  var d = new Date();
	  return ( ( d.valueOf() - this.startMS ) / 1000 ) > this.seconds;
	}
	
}

var AnimationManager = function( speed )
{
   this.Animations = [];
   var self = this;
   window.setInterval( function() { self.idle(); }, speed );
}

AnimationManager.prototype = {
	add:function(anim){
		this.Animations.push( anim );
	},
	
	idle:function(){
	  if ( this.Animations.length > 0 )
	  {
		this.Animations[0].animate();
		if ( this.Animations[0].done() )
		  this.Animations.shift();
		if ( this.Animations.length == 0 )
		  this.on_finished();
	  }		
	},
	
	on_finished:function(){
		
	}
}
