///////////////////////////////////////////////////////////////////////////////////////////////////
// Package     : Animation
//-------------------------------------------------------------------------------------------------
// Purpose     : Provides animation services
// Description : Provided to replace animated GIF.
//-------------------------------------------------------------------------------------------------
// Authors     : Loic Le Chevalier (loic.lechevalier@artcoder.com)
// Copyright   : (c) 2004 Artcoder & Loic Le Chevalier
///////////////////////////////////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////////////////////////////////
// Variables
///////////////////////////////////////////////////////////////////////////////////////////////////

var animationHandle = null  // Handle of animation function
var animationArray  = null  // Store animation of the page


///////////////////////////////////////////////////////////////////////////////////////////////////
// Functions
///////////////////////////////////////////////////////////////////////////////////////////////////

function animationInit()
{
	if ( animationArray == null )
		animationArray = new Array()
	if ( animationHandle == null )
		animationStart()
}

function animationAdd(id, images, delay, rand)
{
	/*
	images = new Array(3)
	images[0] = new Image()
	images[0].src = "01.png"
	images[1] = new Image()
	images[1].src = "02.png"
	images[2] = new Image()
	images[2].src = "03.png"
	*/
	record = new Array(id, images, 0, delay, 0, rand)
	if ( animationArray == null )
		animationInit()
	animationArray.push(record)
	
	if ( rand )
	{
		n = Math.random()*images.length
		img = document.getElementById(id)
		img.src = eval('images['+Math.round(n)+'].src')
	}
}

function animationStart()
{
	period = 100
	for (i = 0; i < animationArray.length; ++i)
	{
		id     = animationArray[i][0]
		images = animationArray[i][1]
		index  = animationArray[i][2]
		delay  = animationArray[i][3]
		time   = ( animationArray[i][4] += period )
		rand   = animationArray[i][5]

		if ( time < delay )
			continue

		if( index >= images.length )
			index = ( animationArray[i][2] = 0 )

		img = document.getElementById(id)
		img.src = eval('images['+index+'].src')
		animationArray[i][4] = 0
		
		if ( rand )
		{
			n = Math.random()*images.length
			animationArray[i][2] = Math.round(n)
		}
		else
		{
			animationArray[i][2]++
		}
	}
	
	animationHandle = setTimeout("animationStart()", period)
}

function animationStop()
{
	clearTimeout(animationHandle)
}
		