/*  all code written by Jesse Gilbert.  Copyright ParkBench Fine Artists, 1997, 1998 */


/* directory name where the images are */
var dirName="xxx";

/* number of radio buttons across top of screen -  now constant at 20,
thanks to Math.round().  */
var numRadio = 20;

/* counter is central var -  where are we in the animation?  */
var counter=1;

/*  frameRate controls the Timeout of the changer function -  in milliseconds. */
var frameRate=4000;

/*  maxFrames equals the total number of images in the archive.  */
var maxFrames=100;

/*  inc is the increment for the additions to counter.  */
var inc=1;

/*  timerId is what's used to end bugs with position seeking */
var timerId = null;

/* aniMarker is the array which holds the results of the getPosition() function, rather than having to do this calculation each time through the animation. */
aniMarker = new Array(21);



/*  start()  intializes two variables (counter and dirName)to the values passed by
 the onLoad() function in the html page, then triggers the beginning of the 
animation by calling doit().  It is only called once.  */


function start(newDirName,newMaxFrames,metafile)  {

	dirName = newDirName;
	maxFrames = newMaxFrames;

	for (i = 0; i <= 20 ;  i++)  
		aniMarker[i] = getPosition(i) ;

	var index = document.framespeed.fr.selectedIndex;
	var newFR = document.framespeed.fr.options[index].value;
	
	frameRate = newFR * 1000;

	if (metafile) {
		document.location = metafile;
	}

	doit();
}




/*  doit()  is the function that controls the speed of the animations, basically a call back
to changer().   */


function doit()  {

	timerId = setTimeout("changer()",frameRate);
}





/*  changer() is the main funciton of the script.  Increments counter, adjusts counter for
extended name, selects appropriate radio button, calls the next image using the new name.  */


function changer()  {

	counter = counter + inc;

	if (counter > maxFrames)  {
		counter = 1;
	}
	
		var newCounter = adjustCounter(counter);

		doPosition(counter);

		var fileName = dirName + "/" + newCounter +  ".jpg";

		document.animation.src =  fileName;
		//status = "just loaded " + fileName;

		doit();
	
}




/*  adjustCounter()  takes the counter var and makes it a string with the right # of leading
0's.  */


function adjustCounter(counter)  {

	var newC;

	if (counter >= 1 && counter < 10)  {
		newC = "000" + counter;	}

	if (counter > 9 && counter < 100)  {
		newC = "00" + counter; }

	if (counter > 99 && counter < 1000)  {
		newC = "0" + counter;	}

	if (counter > 999)  {
		newC = counter;	}
	

	return newC;
}




/*  doPosition()  checks to see where we are in the playback, and selects the appropriate radio button by calling changePosition().  It references the aniMarker[] array
 to get the valid bounds for its if test applied to the value of counter, 
which is passed from changer() as var theCounter.  */


function doPosition(theCounter)  {

	if ((theCounter >= aniMarker[0]) && (theCounter < aniMarker[1]))   {
		changePosition(0);
	}

	if ((theCounter >= aniMarker[1]) && (theCounter < aniMarker[2]))   {
		changePosition(1);
	}

	if ((theCounter >= aniMarker[2]) && (theCounter < aniMarker[3]))   {
		changePosition(2);
	}

	if ((theCounter >= aniMarker[3]) && (theCounter < aniMarker[4]))   {
		changePosition(3);
	}

	if ((theCounter >= aniMarker[4]) && (theCounter < aniMarker[5]))   {
		changePosition(4);
	}

	if ((theCounter >= aniMarker[5]) && (theCounter < aniMarker[6]))   {
		changePosition(5);
	}

	if ((theCounter >= aniMarker[6]) && (theCounter < aniMarker[7]))   {
		changePosition(6);
	}

	if ((theCounter >= aniMarker[7]) && (theCounter < aniMarker[8]))   {
		changePosition(7);
	}

	if ((theCounter >= aniMarker[8]) && (theCounter < aniMarker[9]))   {
		changePosition(8);
	}

	if ((theCounter >= aniMarker[9]) && (theCounter < aniMarker[10]))   {
		changePosition(9);
	}

	if ((theCounter >= aniMarker[10]) && (theCounter < aniMarker[11]))   {
		changePosition(10);
	}

	if ((theCounter >= aniMarker[11]) && (theCounter < aniMarker[12]))   {
		changePosition(11);
	}

	if ((theCounter >= aniMarker[12]) && (theCounter < aniMarker[13]))   {
		changePosition(12);
	}

	if ((theCounter >= aniMarker[13]) && (theCounter < aniMarker[14]))   {
		changePosition(13);
	}

	if ((theCounter >= aniMarker[14]) && (theCounter < aniMarker[15]))   {
		changePosition(14);
	}

	if ((theCounter >= aniMarker[15]) && (theCounter < aniMarker[16]))   {
		changePosition(15);
	}

	if ((theCounter >= aniMarker[16]) && (theCounter < aniMarker[17]))   {
		changePosition(16);
	}

	if ((theCounter >= aniMarker[17]) && (theCounter < aniMarker[18]))   {
		changePosition(17);
	}

	if ((theCounter >= aniMarker[18]) && (theCounter < aniMarker[19]))   {
		changePosition(18);
	}

	if ((theCounter >= aniMarker[19]) && (theCounter < aniMarker[20]))   {
		changePosition(19);
	}
}




/*  changePosititon() takes an argument from doPosition() and selects the appropriate radio
button, automatically deselecting the presently checked one.  Note that the radio
buttons must be named 'p', and the form named 'position'.  */


function changePosition(value)  {

	if (document.position) {

		var isChecked = document.position.p[value].checked;

		if (isChecked) { ; }

		else document.position.p[value].checked = true;

	}

}




/*  getPosition() returns a value to the onClick() function used by the radio buttons, 
and also to doPosition().  It's passed an argument by the calling function called multiplier
 that is used to give equal units back -   for example   100 frames / 20 radio ...  
increment = 5   value returned depends on multiplier for this increment */


function getPosition(multiplier)  {

	var newP = 0;

	if (multiplier == 0)  {
		newP = 2;
	}

	else {

		newP = Math.round((maxFrames / numRadio) * multiplier);
	}


	return newP;
}





/*  newPosition() is called by the user clicking on one of the radio buttons.  It sets the value 
of counter to the argument passed by the calling function, which is in turn a value returned 
by getPosition().  */


function newPosition(theCounter)  {

	clearTimeout(timerId);

	if (theCounter == 2)  counter = 0;
	else counter = theCounter - 1;

	doit();
}




/*  newFrameRate()  is called by the user clicking the "framespeed" form.  It takes the 
Select object as an argument, and then changes the frameRate var accordingly.  */


function newFrameRate(theSelect)  {

	clearTimeout(timerId);

	var index = theSelect.selectedIndex;
	var newFR = theSelect.options[index].value;
	
	frameRate = newFR * 1000;
	doit();
}
