var AnimationPlayer = Class.create({

	/*
		AnimationPlayer v1.0 by Juan Carlos Ospina Gonzalez (www.piterwilson.com) Feb 28,2009
	*/
	
  initialize : function(frames,targetDiv) {
	
	this.frames  = frames;
	this.frameImages = new Array();
    this.frameRate = 100;
    this.timeOut = null;
    this.currentFrame = 1;
    this.currentImg = null;
    this.targetDiv = targetDiv;
    
    for(var i=0;i<this.frames.length;i++){
    	var img = new Element('img', {
				src : this.frames[i]
			});
		this.frameImages.push(img);
	}
    
  },

  play : function() {
    
    if(this.frames.length>0){
    	
    	this.goToFrame(this.currentFrame);
		this.timeOut = setTimeout(this.goToNextFrame.bind(this), this.frameRate);
    	
    }
    
  },
  
  stop : function(){
	
	if(this.playing){
	
		this.pause();	
		this.gotoFrame(1);
	
	}
	
  },
  
  goToFrame : function(num){
	if(num>0 && num<=this.frames.length){
		this.currentFrame = num;
		this.targetDiv.innerHTML="";
		this.currentImg = this.frameImages[this.currentFrame-1];
		this.targetDiv.appendChild( this.currentImg );
	}
	
  },
  
  goToNextFrame : function(){
	if(this.currentFrame < (this.frames.length)){
		this.currentFrame++;
		this.goToFrame(this.currentFrame);
	}else{
		this.currentFrame = 1;
		this.goToFrame(this.currentFrame);
	}
	this.play();
	
  },
  
  pause : function(){
  	
  	clearTimeout(this.timeOut);	
  	
  }
  
});


