/*
Animation Class: Animation(int framesPerSecond,int numberOfFrames,string animationKey)*;

*a function called 'FrameCode(currentFrame,key)' must be declared
where the currentFrame and key are passed from the animation class.
The 'key' should be used to identify which set of animation code should
be executed (using a switch or if statements) and the 'currentFrame' to
be used as a counter to render your current frame.

Methods:

 - play();

 - pause();

 - stop();

 - rewind(speed);

 - forward(speed);

Example: 

var animation1 = new Animation(60,100,"anim1");
var animation2 = new Animation(60,100,"anim2");
animation1.play();
animation2.play();

function FrameCode(currentFrame,key)
{
	switch(key)
	{
		case "anim1":
		{
			//my animation (use currentFrame for timeline)
			break;
		}
		case "anim2":
		{
			//my animation (use currentFrame for timeline)
			break;
		}
	}
}

*/
var animationIdCount = 0;
var animations = new Array();

function Animation(fps,frames,key)
{
	this.currentFrame = 1;
	this.frameTempo = Number(1000 / fps).toFixed(0);
	this.frameCount = frames;
	this.animationKey = key;
	this.animationId = animationIdCount;
	this.pauseBool = false;
	this.stopBool = false;
	this.rewindBool = false;
	this.playing = false;
	this.frameSpeed = 1;
	animations[animationIdCount] = this;
	animationIdCount++;
	
	this.loop = function()
	{
		try
		{
			FrameCode(this.currentFrame,this.animationKey);
			
			if(!this.rewindBool)
			{
				this.currentFrame += this.frameSpeed;
			}
			else
			{
				this.currentFrame -= this.frameSpeed;
			}
			
			if(this.currentFrame > 0 && this.currentFrame < this.frameCount && !this.pauseBool && !this.stopBool)
			{
				setTimeout("animations[" + this.animationId + "].loop()",this.frameTempo);
			}
			else
			{
				this.playing = false;
				
				if(!this.pauseBool)
				{
					this.currentFrame = 1;
					
					if(this.stopBool || this.rewindBool)
					{
						FrameCode(1,this.animationKey);
					}
					else
					{
						FrameCode(this.frameCount,this.animationKey);
					}
				}
			}
		}
		catch(e)
		{
			Alert("Your animation frame codes must all be placed in a function called 'FrameCode(currentFrame,key)'");
		}
	};
	
	this.play = function()
	{
		this.frameSpeed = 1;
		
		this.rewindBool = false;
		this.pauseBool = false;
		this.stopBool = false;
		
		if(!this.playing)
		{
			this.playing = true;
			this.loop();
		}
	};
	
	this.pause = function()
	{
		this.pauseBool = true;
	};
	
	this.stop = function()
	{
		this.rewindBool = false;
		this.pauseBool = false;
		this.stopBool = true;
		
		if(!this.playing)
		{
			this.playing = true;
			this.loop();
		}
	};
	
	this.rewind = function(speed)
	{
		this.frameSpeed = speed;
		
		this.rewindBool = true;
		this.pauseBool = false;
		this.stopBool = false;
		
		if(!this.playing)
		{
			this.playing = true;
			this.loop();
		}
	};
	
	this.forward = function(speed)
	{
		this.frameSpeed = speed;
		
		this.rewindBool = false;
		this.pauseBool = false;
		this.stopBool = false;
		
		if(!this.playing)
		{
			this.playing = true;
			this.loop();
		}
	};
}
