/*
Javascript picture animator v1.5
*/

Animator.col=[];

function trapkey(n)
{
	if (event.keyCode==37)
	{
		var obj=Animator.col[n];
		obj.prev1();
	}
	if (event.keyCode==39)
	{
		var obj=Animator.col[n];
		obj.next1();
	}
	
}

// Constructor
function Animator(name,alt,w,h,path,imgArr,speed,autoplay,autorepeat)
{
	this.name=name;
	this.w=w;
	this.h=h;
	this.speed=speed;
	this.path=path||"";
	this.ctr=0;
	this.timer=0;
	this.pause=1;
	this.imagenames=imgArr;
	this.imgs=[];
	this.index=Animator.col.length;
	this.autorepeat=autorepeat;
	this.cont=0;
	Animator.col[this.index]=this;
	this.objStr="Animator.col["+this.index+"]";

	var Str="";
	var img=imgArr[0];
	var imgStr='<img name="'+this.name+'" src="'+this.path+img+'"';
	imgStr+=(typeof w=="number")?' width="'+w+'"':'';
	imgStr+=(typeof h=="number")?' height="'+h+'"':'';
	imgStr+=' border=0 alt="'+alt+'" title="Click me, and press cursorkeys <- and -> to go back and forward.">';
	Str+='<div onkeydown="trapkey'+this.index+'()" tabindex="0"><a href="" onclick="return false;">';
	Str+=imgStr;
	Str+='</a><br>';
	Str+='<input type="button" value=" |< " style="width:30;" onclick="Animator.begin('+this.index+');">';
	Str+='<input type="button" value=" < " style="width:30;" onclick="Animator.playReverse('+this.index+');">';
	Str+='<input type="button" value=" || " style="width:30;" onclick="Animator.pause('+this.index+');">';
	Str+='<input type="button" value=" > " style="width:30;" onclick="Animator.play('+this.index+');">';
	Str+='<input type="button" value=" >| " style="width:30;" onclick="Animator.end('+this.index+');">';
	Str+=' <span id="label'+this.index+'" style="font-size:9;"><nobr>('+img+')</nobr></span>';
	Str+='</div>';
	Str+='<script type="text/javascript"> function trapkey'+this.index+'(){ trapkey('+this.index+'); } </script>';
	document.write(Str);
	document.close();
	for(var i=0;imgArr[i];i++) this.addImage(imgArr[i]);

	if(autoplay&&Animator.ready)
	{
		this.ctr=-1;
		this.timer=setTimeout(this.objStr+".next()",this.speed);
	}
};

Animator.prototype.addImage=function()
{
	var img;
	for(var i=0;arguments[i];i++)
	{
		img=new Image();
		img.src=this.path+arguments[i];
		this.imgs[this.imgs.length]=img;
	}
};

Animator.play=function(n)
{
	var obj=Animator.col[n];
	if(obj.ctr==obj.imgs.length-1) obj.ctr=-1
	obj.next();
};

Animator.playReverse=function(n)
{
	var obj=Animator.col[n];
	if(obj.ctr==0) obj.ctr=obj.imgs.length;
	obj.prev();
};

Animator.pause=function(n)
{
	var obj=Animator.col[n];
	obj.setPause();
};

Animator.begin=function(n)
{
	var obj=Animator.col[n];
	clearTimeout(obj.timer);
	obj.timer=null;
	obj.ctr=0;
	obj.setImg();
};

Animator.end=function(n)
{
	var obj=Animator.col[n];
	clearTimeout(obj.timer);
	obj.timer=null;
	obj.ctr=obj.imgs.length-1;
	obj.setImg();
};

Animator.prototype.next=function()
{
	clearTimeout(this.timer);
	this.timer=null;
	this.cont=1;
	if(this.ctr<this.imgs.length-1) this.ctr++;
	else
	{
		if(this.autorepeat) this.ctr=0;
		else this.cont=0;
	}
	if(this.cont)
	{
		this.setImg();
		if(Animator.ready)
		{
			this.timer=setTimeout(this.objStr+".next()",this.speed);
		}
	}
};

Animator.prototype.prev=function()
{
	clearTimeout(this.timer);
	this.timer=null;
	this.cont=1;
	if(this.ctr>0)this.ctr--;
	else
	{
		if(this.autorepeat) this.ctr=this.imgs.length-1;
		else this.cont=0
	}
	if(this.cont)
	{
		this.setImg();
		if(Animator.ready)
		{
			this.timer=setTimeout(this.objStr+".prev()",this.speed);
		}
	}
};

Animator.prototype.next1=function()
{
	clearTimeout(this.timer);
	this.timer=null;
	if(this.ctr<this.imgs.length-1) this.ctr++;
	this.setImg();
};

Animator.prototype.prev1=function()
{
	clearTimeout(this.timer);
	this.timer=null;
	if(this.ctr>0)this.ctr--;
	this.setImg();
};

Animator.prototype.setImg=function()
{
	var imgObj=document.images[this.name];
	if(imgObj&&Animator.ready)
	{
		imgObj.src=this.imgs[this.ctr].src;
		document.all("label"+this.index).innerHTML="<nobr>("+this.imagenames[this.ctr]+")</nobr>";
	}
}

Animator.prototype.setPause=function()
{
	clearTimeout(this.timer);
	this.timer=null;
	this.pause=1;
}

Animator.ready=true;

