/*******************************************************************
*
* File    : animate.js
*
*
* Created : 1999/08/10 - Last Updated 1999/08/11
*
* Author  : Roy Whittle  (Roy@Whittle.com)
*
* Purpose : To create animated 'onMouseOver' buttons
*
* History
* Date         Version        Description
* 1999-08-10     1.0          First Version It works! amazing.
* 1999-08-11     1.1          Allowed for different extension.
*                             Animated jpegs!
*******************************************************************/

var AnimationRunning = false; /*** Global state of animation ***/
var FrameInterval    = 20;    /*** Time between frames in milliseconds   ***/

/***********************************************************
* Function   : Animated Image
*
* Parameters : name - the name of the button.
*              n    - number of frames in animation
*              ext  - the type of image (".GIF", ".JPG")
*              
* Description : Creates an object that can hold the current
*               state of the animation and the images for
*               the animation.
*               There must be 1 ".gif" file for every frame
*               of animation  and they must reside in a 
*               the directory "images/name/x.gif". 
*               E.g.
*                 "images/email/0.gif"
*                 "images/email/1.gif"
*                 ....
*                 "images/email/x.gif" //where x=(n-1);
***********************************************************/
function AnimatedImage(name, n, ext)
{
	if(document.images)
	{
		this.num_frames = n;
		this.name       = name;
		this.index      = 0;
		this.state      = "CLOSED";

		for(var i=0 ; i<n ; i++)
		{
			this[i]=new Image();
			this[i].src = "/images/" + name + "/" + i + ext;
		}
		
	}
	
}
/*******************************************************************
*
* Function    : Animate
*
* Description : Each animation object has a state.
*               The states normally go as follows
*                   CLOSED->OPENING->OPEN
*                   OPEN->CLOSING->CLOSED.
*               When the onMouseOver event is received, a button in the
*               CLOSED state is switched to the OPENING state until OPEN
*               is reached. When the onMouseOut event is received a button
*               in the OPEN state is switched to the CLOSING state until
*               the CLOSED state is reached. 
*
*               The special cases are what happens when we get onMouseOut when
*               in the middle of opening. In this case the path is :-
*               CLOSED->OPENING->OPEN_CLOSE->CLOSING->CLOSED.
*               in this way the button will fully "open" before it starts 
*               closing. This can be changed by always setting the state
*               to "CLOSING" when the onMouseOut event is received.
*
*               If the button is "CLOSING" and the onMouseOver event is
*               received then the state is set back to "OPENING and the
*               button will start opening again immediately.
*
*******************************************************************/
function Animate()
{	
	AnimationRunning = false; /*** Assume no more frames that need displaying ***/

	for(var i=0 ; i<Btn.numButtons ; i++)
		switch(Btn[i].state)
		{
			/*** Increment the frame index - display the next frame ***/
			/*** when fully open, set state to "OPEN"               ***/
			case "OPENING" :
				if(Btn[i].index < Btn[i].num_frames-1)
				{
					Btn[i].index++;
					document.images[Btn[i].name].src=Btn[i][Btn[i].index].src;
					AnimationRunning = true;
				}
				else
					Btn[i].state = "OPEN";

				break;

			/*** Increment the frame index - display the next frame ***/
			/*** when fully open, set state to "CLOSING"            ***/
			case "OPEN_CLOSE" :
				if(Btn[i].index < Btn[i].num_frames-1)
				{
					Btn[i].index++;
					document.images[Btn[i].name].src=Btn[i][Btn[i].index].src;
				}
				else
					Btn[i].state = "CLOSING";

				AnimationRunning = true;
				break;

			/*** Decrement the frame index - display the next frame ***/
			/*** when fully closed, set state to "CLOSED"           ***/
			case "CLOSING" :
				if(Btn[i].index > 0)
				{
					Btn[i].index--;
					document.images[Btn[i].name].src=Btn[i][Btn[i].index].src;
					AnimationRunning = true;
				}
				else
					Btn[i].state = "CLOSED";

				break;
		}
	/*** Check to see if we need to animate any more frames. ***/
	if( AnimationRunning )
		setTimeout("Animate()",FrameInterval);
}
/*****************************************************************
* Function    : start_animation
*
* Description : Call the animate routine and start the 
*               animation running
*****************************************************************/
function start_animation()
{
	if(!AnimationRunning)
		Animate();
}
/*****************************************************************
*
* Function   : turn_on
*
* Parameters : button_name - string containing the name of the
*                            button to start animating.
*
* Description: Checks that the button is in a valid state to
*              start "OPENING". If it is it sets the state to
*              "OPENING" and calls start_animation.             
*
*****************************************************************/
function turn_on(btn_name)
{
	for(var i=0; i<Btn.numButtons ; i++)
		if(Btn[i].name == btn_name )
		{
			switch(Btn[i].state)
			{
				case "CLOSED"     :
				case "CLOSING"    :
				case "OPEN_CLOSE" :
					Btn[i].state = "OPENING";
					start_animation();
					break;
				default :
					break;
			}
		}

}
/*****************************************************************
*
* Function   : turn_off
*
* Parameters : button_name - string containing the name of the
*                            button to start animating.
*
* Description: Checks that the button is in a valid state to
*              start "CLOSING". If it is it sets the state to
*              "CLOSING" and calls start_animation.             
*
*****************************************************************/
function turn_off(btn_name)
{

	for(var i=0; i<Btn.numButtons ; i++)
		if(Btn[i].name == btn_name )
		{
			switch(Btn[i].state)
			{
				case "OPEN"       :
					Btn[i].state = "CLOSING";
					start_animation();
					break;
				case "OPENING"  :
					Btn[i].state = "OPEN_CLOSE";
// Uncomment the following line if you want the button to start closing onMouseOut
//					Btn[i].state = "CLOSING";
					start_animation();
					break;
				default :
					break;
			}
		}

}
