var AnimationPlayer = Class.create();
AnimationPlayer.prototype = {

	/*
		AnimationPlayer v1.0 by Juan Carlos Ospina Gonzalez (www.piterwilson.com) Feb 28,2009
	*/
	
  initialize : function(frames,targetDiv,width,height) {
	this.frames  = frames;
	this.frameImages = new Array();
    this.frameRate = 500;
    this.timeOut = null;
    this.currentFrame = 1;
    this.currentImg = null;
    this.targetDiv = targetDiv;
    this.targetDiv.innerHTML = '';
    this.currentImg = document.createElement('img');
    if (height)
    {
	    this.currentImg.setAttribute('height', height);
    }
    if (width)
    {
	    this.currentImg.setAttribute('width', width);
    }
    this.currentImg.src = this.frames[0];
    this.targetDiv.appendChild( this.currentImg );
    
    for (var i = 0; i < this.frames.length; i++)
    {
	    this.frameImages.push(this.frames[i]);
    }
  },

  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;
		var image = null;
		for (var i = 1; i < this.frames.length; i++)
		{
			var image = new Image();
			image.src = this.frameImages[i];
		}
		this.currentImg.src = this.frameImages[this.currentFrame-1];
	}
	
  },
  
  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);	
  	
  }
  
};

