// JavaScript Document

/* 
	animset class
	
	plays a sequence of images in the browser.
	
	Mark Zifchock and Blisslogic 09/08/2006
	
	
	methods:
	
	animSet()						Constructor.
	loadCel( srcIn )				adds an image file with path 'srcIn' to the animSet.
	setTargetById( target )			sets the target image in the html body that the anim will play in.
	setPlayQueue( celIndex, time )	adds a 'frame' to the play queue.
	play()							plays the animation.
	incrementQueue()				called for each time interval in play();
	
	
	nextLoop()						switches to next loop mode ( single, loop, pingpong ) 
									NOTE pingpong passes redundant endframes when flipping ( 3, 4, 5, 4, 3, etc. )
	slower( amount )				modify basetime longer in millisec.  argument optional.
	faster( amount )				modify basetime shorter in millisec.  argument optional.
	gotoFrame( frame )				stop playback, and go to the specified frame.
	gotoStart()						goto the first frame.
	gotoEnd()						goto the last frame.
	gotoNextFrame()					goto the next frame.
	gotoPreviousFrame()				goto the previous frame.
	
	external interfaces:
	updateStatus()					called each time a frame is accessed - must be defined in the main document window.
	
	

*/


function animSet()
{
	this._id = animSet.instances.length;
	animSet.instances[ this._id ] = this;	
	
	this.m_cellCount = 0;
	this.m_celSet = new Array();
	
	this.m_queueCount = 0;
	this.m_queue = new Array();
	this.m_timeSet = new Array();	

	this.m_Qindex = 0;						// the play position in the queue
	this.m_target = '';						// the target image in the dom for the animation
	this.m_baseTime = 15;					// 16.6 = 60 fps
	this.m_loop = 'pingpong';					// 'single', 'loop', 'pingpong' ( skips redundant endframes )
	this.m_loopstatus = 0;					// 0 normal, 1 reverse
	
	this.m_time = null;

	this.m_stopCondition = false;	
}

animSet.instances = new Array();

animSet.prototype.nextLoop = function()
{
	this.m_Qindex = 0;	
	this.m_loopstatus = 0;
	 
	if ( this.m_loop == 'single' ) { this.m_loop = 'loop'; }
	else if ( this.m_loop == 'loop' ) { this.m_loop = 'pingpong'; }
	else if ( this.m_loop == 'pingpong' ) { this.m_loop = 'single'; }	
	
	
//	if ( this.m_stopCondition == false ) { this.incrementQueue(); }
}

animSet.prototype.loadCel = function( srcIn )
{
	this.m_celSet[ this.m_cellCount ] = new Image();
	this.m_celSet[ this.m_cellCount ].src = srcIn;
	this.m_cellCount ++;
}

animSet.prototype.setBaseTime = function( basetime )
{
	this.m_baseTime = basetime;
}

animSet.prototype.setTargetById = function( target )
{
	this.m_target = getElement( target );
}

animSet.prototype.setPlayQueue = function( celIndex, time )
{
	if ( !time ) { time = 1; }
	
	this.m_queue[ this.m_queueCount ] = celIndex;
	this.m_timeSet[ this.m_queueCount ] =  time;
	this.m_queueCount ++;
}

animSet.prototype.slower = function( amount )
{
	if ( !amount ) { amount = 1 };
	this.m_baseTime += amount;
}

animSet.prototype.faster = function( amount )
{
	if ( !amount ) { amount = 1 };	
	this.m_baseTime -= amount;	
	if ( this.m_baseTime < 0 ) this.m_baseTime = 0 ;
}

animSet.prototype.play = function()
{
	if ( this.m_loop == 'single' && this.m_Qindex == this.m_queueCount - 1 ) { this.m_Qindex = 0; }
	this.m_stopCondition = false;
	this.incrementQueue();
}

animSet.prototype.stop = function()
{
	this.m_stopCondition = true;
}

animSet.prototype.gotoFrame = function( frame )
{
	this.m_stopCondition = true;
	this.m_Qindex = frame;
	this.incrementQueue();
}

animSet.prototype.gotoStart = function()
{
	this.gotoFrame( 0 );
}

animSet.prototype.gotoEnd = function()
{
	this.gotoFrame( this.m_queueCount - 1 );
}

animSet.prototype.gotoNextFrame = function()
{
	if ( this.m_Qindex == this.m_queueCount - 1 ) { this.m_Qindex = -1; }	
	this.gotoFrame( this.m_Qindex + 1 );	
}

animSet.prototype.gotoPreviousFrame = function()
{
	if ( this.m_Qindex == 0 ) { this.m_Qindex = this.m_queueCount; }		
	this.gotoFrame( this.m_Qindex - 1 );	
}

animSet.prototype.incrementQueue = function()
{
	clearTimeout( this.m_time );
	
	if ( this.m_loop == 'pingpong' )
	{
		if ( this.m_loopstatus == 0 && this.m_Qindex >=  this.m_queueCount ) 
		{ 
			this.m_Qindex = this.m_queueCount - 2;
			this.m_loopstatus = 1;
		} else if ( this.m_loopstatus == 1 && this.m_Qindex <= 0 )
		{
			this.m_Qindex = 0; 
			this.m_loopstatus = 0;
		}
	} else if ( this.m_loop == 'loop' && this.m_Qindex >= this.m_queueCount ) { this.m_Qindex = 0; }
	else if ( this.m_loop == 'single' && this.m_Qindex >= ( this.m_queueCount - 1 ) ) { this.m_stopCondition = true; }
	
	
	increment = ( this.m_loopstatus == 0 ) ? 1:-1;
	
	this.m_target.src = this.m_celSet[ this.m_queue[ this.m_Qindex ] ].src;

	if ( window.updateStatus )
	{
		window.updateStatus();	
	}

	if ( this.m_stopCondition == false )
	{
		this.m_time = setTimeout( 'animSet.instances[' + this._id + '].incrementQueue()', Math.round( this.m_baseTime * this.m_timeSet[ this.m_Qindex ]) );	
		this.m_Qindex = this.m_Qindex + increment;			
	} 
}


/*
	helper class 
*/

getElement = function(el_id){ 
	if ( document.all ){
		return document.all( el_id );
	} else if ( document.getElementById ) {
		return document.getElementById( el_id );
	}
}
