
var AnimationRunning = false; 
var FrameInterval    = 30;  

var AniImage = new Array();
var AniFrame = new Array();

var BaseHref="images/";
var Sep     = "/";
var Timer   = null;


function ImageError()
{
	alert("animate.js has detected an error\nImage not found\n" + this.src);
}

function CreateAnimationFrames(aniName, n, ext)
{
	this.num_frames = n;
	for(var i=0 ; i<n ; i++)
	{
		this[i]=new Image();
		this[i].src = BaseHref + aniName + Sep + i + ext;

		this[i].onerror=ImageError;
	}
}

function CreateAnimatedImage(imgName, aniName)
{
	if(document.images)
	{
		this.img_name   = imgName;
		this.ani_name   = aniName;
		this.next_on    = null;
		this.next_off   = null;
		this.index      = 0;
		this.target_frame= 0;
		this.state      = "CLOSED";
		this.img	    = null;
	}
	
}

function AnimatedImage(imgName, aniName)
{
	AniImage[ imgName ] = new CreateAnimatedImage( imgName, aniName);

	if(AniFrame[aniName]==null)
		alert("animate.js has detected a possible error\n       --------------------------------\n"
		    + "Error	: AnimationFrames \"" + aniName + "\" not defined\n"
		    + "Function	: AnimatedImage(\"" + imgName + "\",\"" + aniName + "\")");

}

function AnimationFrames(aniName, n, ext)
{
	
	if(AniFrame[aniName] == null)
		AniFrame[ aniName ]= new CreateAnimationFrames(aniName, n, ext);
	else

		;
}

function AnimatedGif(name, n)
{
	AnimationFrames(name, n, ".gif");
	AnimatedImage( name, name);
}
function AnimatedJpg(name, n)
{
	AnimationFrames(name, n, ".jpg");
	AnimatedImage( name, name);
}


function getImage(n, d) 
{
	var img = d.images[n];
	if(!img && d.layers)  
		for(var i=0 ; !img && i<d.layers.length ; i++) 
			img=getImage(n,d.layers[i].document); 
	return img;
}


function startAnimation()
{
	if(!AnimationRunning)
		Animate();
}

function turn_on(imgName, aniName)
{
	if(!ErrorCheck("turn_on", imgName, aniName))
	{
		var b=AniImage[ imgName ];

		if(b.state == "CLOSED" )
		{
			b.state = "OPENING";
			if(aniName)
				b.ani_name=aniName;
			startAnimation();
		}
		else if ( b.state == "OPEN_CLOSE"
			||  b.state == "CLOSING" 
			||  b.state == "CLOSE_OPEN") 
		{
			if(!aniName || b.ani_name==aniName)
				b.state = "OPENING";
			else
			{
				b.next_on=aniName;
				b.state = "CLOSE_OPEN";
			}
		}
		
		else if( b.state == "OPENING"
			|| b.state == "OPEN")
		{
			if(aniName && b.ani_name != aniName)
			{
				b.ani_name=aniName;
				b.index=0;
				b.state="OPENING";
				startAnimation();
			}
		}
	}
}

function turn_off(imgName, aniName)
{
	if(!ErrorCheck("turn_off", imgName, aniName))
	{
		var b=AniImage[ imgName ];

		if( b.state == "OPEN")		
		{
			if(aniName)
			{
				b.ani_name=aniName;
				b.index=AniFrame[aniName].num_frames-1;
			}
			b.state = "CLOSING";
			startAnimation();
		}
		else if(b.state == "CLOSE_OPEN")
		{
			b.next_off=null;
			b.state="CLOSING"
		}
		else if( b.state == "OPENING" )
		{
			b.next_off = aniName;
			b.state = "OPEN_CLOSE";
		}
	}
}

function Animate()
{	
	AnimationRunning = false;

	for(var i in AniImage)
	{
		var b=AniImage[i];
		var a=AniFrame[b.ani_name];

		if(b.state == "OPENING")
		{
			
			if(++b.index < a.num_frames)
			{
				b.img.src=a[b.index].src;
				AnimationRunning = true;
			}
			else
			{
				b.index=a.num_frames-1;
				b.state = "OPEN";
			}
		}
		else if(b.state == "OPEN_CLOSE")
		{
			
			if(++b.index < a.num_frames)
			{
				b.img.src=a[b.index].src;
			}
			else
			{
				if(b.next_off)
				{
					b.ani_name=b.next_off;
					a=AniFrame[b.ani_name];
					b.next_off=null;
				}
				b.index=a.num_frames-1;
				b.state = "CLOSING";
			}
			AnimationRunning = true;
		}
		else if(b.state == "CLOSING")
		{
			
			if(--b.index >= 0)
			{
				b.img.src=a[b.index].src;
				AnimationRunning = true;
			}
			else
			{
				b.index=0;
				b.state = "CLOSED";
			}
		}
		else if(b.state == "CLOSE_OPEN")
		{
			
			if(--b.index >= 0)
			{
				b.img.src=a[b.index].src;
			}
			else
			{
				b.index=0;
				b.ani_name=b.next_on;
				b.state = "OPENING";
			}
			AnimationRunning = true;
		}
		else if(b.state == "ROTATE_UP")
		{
			
			if(b.index != b.target_frame)
			{
				if(++b.index == a.num_frames)
					b.index = 0;
				b.img.src=a[b.index].src;
				AnimationRunning = true;
			}
			else
				b.state = "CLOSED";
		}
		else if(b.state == "ROTATE_DOWN")
		{
			
			if(b.index != b.target_frame)
			{
				if(--b.index < 0)
					b.index = a.num_frames-1;
				b.img.src=a[b.index].src;
				AnimationRunning = true;
			}
			else
				b.state = "CLOSED";
		}
	}
	
	if(AnimationRunning)
	{
		if(!Timer)
			Timer=setInterval("Animate()",FrameInterval);
	}
	else
	{
		clearInterval(Timer);
		Timer=null;
	}
}




function ErrorCheck(funcName, imgName, aniName)
{
	var err_str="";

	if(AniImage[imgName]==null)
		err_str += "Error	: AnimatedImage \"" + imgName + "\" not defined\n";
	else
	{
		var b=AniImage[imgName];
		if(b.img == null)
			b.img=getImage(imgName, document);

		if(b.img==null)
			err_str += "Error	: Document <IMG NAME=\"" + imgName + "\"> not defined\n";

		/*** Check the AnimationFrames(b.ani_name, n, ext) has been defined ***/
		if(AniFrame[b.ani_name]==null)
			err_str += "Error	: AnimationFrames \"" + b.ani_name + "\" not defined\n";

	}

	if(aniName)
		/*** Check the AnimationFrames(aniName, n, ext) has been defined ***/
		if(AniFrame[aniName]==null)
			err_str += "Error	: AnimationFrames \"" + aniName + "\" not defined\n";

	if(err_str)
	{
		var extra = aniName ? ("\",\"" + aniName + "\")") : ("\")");
		alert("animate.js has detected an error\n       --------------------------------\n"
		    + err_str
		    + "Function	: " + funcName + "(\"" + imgName + extra);
	
		return true;
	}

	return false;
}

function animate_to(frameNo, imgName)
{
	if(!ErrorCheck("animate_to", imgName) )
	{
		var b = AniImage[ imgName  ];
		var a = AniFrame[ b.ani_name];
		var s = frameNo - b.index;

		b.target_frame = frameNo;

		if(Math.abs(s) > (a.num_frames/2))
		{
			if(s < 0)
				b.state = "ROTATE_UP";
			else
				b.state = "ROTATE_DOWN";
		}
		else
		{
			if(s > 0)
				b.state = "ROTATE_UP";
			else
				b.state = "ROTATE_DOWN";
		}
		startAnimation();
	}
}

function animate_upto(frameNo, imgName)
{
	if(!ErrorCheck("animate_upto", imgName) )
	{
		var b=AniImage[ imgName ];

		b.target_frame = frameNo;
		b.state       = "ROTATE_UP";

		startAnimation();
	}
}

function animate_downto(frameNo, imgName)
{
	if(!ErrorCheck("animate_downto", imgName) )
	{
		var b=AniImage[ imgName ];

		b.target_frame = frameNo;
		b.state       = "ROTATE_DOWN";
		startAnimation();
	}
}


