// (c) 2000 Jim Abramson for Spill Industries, Inc.
// version 1.1 :: 05-16-2001

var AC = new Array();
var AC_uniqID = 0;
var FA_uniqID = 0;

function returnTrue()
{
	return true;
}

function returnFalse()
{
	return false;
}

function AnimationController(delay)
{
	this.id = AC_uniqID++;
	AC[this.id] = this;
	this.delay = delay;
	this.jobs = new Array();
	this.animations = new Array();
	this.addFrameAnimation = AnimationController_addFrameAnimation;
	this.addToQueue = AnimationController_addToQueue;
	this.execute = AnimationController_execute;
	this.running = false;
}

function AnimationController_addFrameAnimation(frameLength,behavior,object)
{
	this.animations[this.animations.length] = new FrameAnimation(this,frameLength,behavior,object);
	return this.animations[this.animations.length-1];
}

function AnimationController_addToQueue(frameAnimation)
{
	for(var i=0; i<this.jobs.length; i++)
	{
		if(this.jobs[i]==frameAnimation) return;
	}
	this.jobs[this.jobs.length] = frameAnimation;
	if(!this.running) this.execute();
}

function AnimationController_execute()
{
	var runAgain = false;
	var nextJobs = new Array();
	for(var i=0; i<this.jobs.length; i++)
	{
		if(this.jobs[i].playNextFrame())
		{
			runAgain=true;
			nextJobs[nextJobs.length]=this.jobs[i];
		}
	}
	if(runAgain) this.running = window.setTimeout('AC['+this.id+'].execute()',this.delay);
	else this.running=false;
	this.jobs = nextJobs;
}

function playNextFrame_def()
{
	nextFrame = this.getNextFrame();
	if(nextFrame!=null)
	{
		this.setFrame(nextFrame);
		return true;
	}
	return this.whenFinished();
}

function playNextFrame_pause()
{
	return true;
}

function atTo_def()
{
	this.frameTo=null;
	return null;
}

function atTo_rtz()
{
	if(!this.infRTZ)
	{
		this.frameTo = null;
	}
	return 0;
}

function atTo_autor()
{
	if(this.infAUTOR) this.frameTo = (this.frameAt==this.lastFrame ? 0 : this.lastFrame);
	else this.frameTo = (this.frameAt==this.lastFrame ? 0 : null);
	return(this.frameAt<this.frameTo ? this.frameAt+1 : this.frameAt-1);
}

function FrameAnimation_proto()
{
	this.frameAt = 0;
	this.frameTo = null;
	this.paused = false;

	this.RTZ = false;
	this.infRTZ = false;
	this.AUTOR = false;
	this.infAUTOR = false;

	this.setRTZ = function(infinite)
	{
		this.RTZ = true;
		if(infinite) 
		{
			this.infRTZ = true;
		}
		this.atTo = atTo_rtz;
	}

/*	this.releaseRTZ = function()
	{
		this.RTZ = false;
		this.absoluteRTZ = false;
		this.atTo = atTo_def;
	}
*/				
	this.setAUTOR = function(infinite)
	{
		this.AUTOR = true;
		if(infinite) 
		{
			this.infAUTOR = true;
		}
		this.atTo = atTo_autor;
	}

/*	this.releaseAUTOR = function()
	{
		this.AUTOR = false;
		this.absoluteAUTOR = false;
		this.atTo = atTo_def;
	}
*/		
	this.setFrame = function(frame)
	{
		this.execScope['doFrameBehavior'+this.id](frame);
		this.frameAt = frame;
	}

	this.playNextFrame = playNextFrame_def;
	this.atTo = atTo_def;
	
	this.getNextFrame = function()
	{
		if(this.frameTo==null) return null;
		if(this.frameAt==this.frameTo) return this.atTo();
		else return(this.frameAt<this.frameTo ? this.frameAt+1 : this.frameAt-1);
	}

	this.whenFinished = returnFalse;
	
	this.setTarget = function(f)
	{
		this.frameTo = f;
	}
	
	this.atTarget = function()
	{
		return (this.frameAt+1 == this.frameTo);
	}
	
/*	this.getCurrentFrame = function()
	{
		return this.frameAt;
	}
*/	
	this.play = function()
	{
//		this.unPause();
		this.controller.addToQueue(this);
	}
	
	this.playFwd = function()
	{
		this.frameTo = this.lastFrame;
		this.play();
		return this.frameTo;
	}

/*	this.playRev = function()
	{
		this.frameTo = 0;
		this.play();
		return this.frameTo;
	}
*/
	this.playTo = function(frame)
	{
		this.frameTo = (frame>this.lastFrame ? this.lastFrame : frame);
		this.play();
	}

	this.playToTarget = function()
	{
		this.playTo(this.frameTo);
	}
/*
	this.stop = function()
	{
//		this.unPause();
		this.frameTo = null;
		return this.frameAt;
	}
*/
/*	this.pause = function()
	{
		if(!this.paused)
		{
			this.paused = true;
			this.playNextFrame = playNextFrame_pause;
		}
	}

	this.unPause = function()
	{
		if(this.paused)
		{
			this.paused = false;
			this.playNextFrame = playNextFrame_def;
		}
	}
*/	
	this.goToStart = function()
	{
		this.frameTo = null;
//		this.unPause();
		this.setFrame(0);
	}
	
	this.goToEnd = function()
	{
		this.frameTo = null;
//		this.unPause();
		this.setFrame(this.lastFrame);
	}
	
/*	this.debugAlert = function()
	{
		alert('frameAt: '+this.frameAt+' frameTo: '+this.frameTo+' nextFrame: '+this.getNextFrame());
	}*/
}

function FrameAnimation(controller,lastFrame,execFunc,execFuncScope)
{
	this.id = FA_uniqID++;
	this.controller = controller;

//	this.doFrameBehavior = frameBehavior;

	execFuncScope = execFuncScope || window;
	this.execScope = execFuncScope;
	this.execScope['doFrameBehavior'+this.id] = execFunc;

	this.lastFrame = lastFrame;
}
FrameAnimation.prototype = new FrameAnimation_proto();