//
// Adapted from jsImagePlay, by Martin Holecko
// jsImagePlay website no longer exists
//

image_name = "latest";		// the base "path/name" of the image set without the numbers
image_type = "jpg";		// Image extension
first_image = 1;		// First image number
last_image = 24;		// Last image number
image_prefix = 1;		// Prefix the image number? (1 => 01)
// ! The size is very important - if incorrect, browser tries to 
// ! resize the images and slows down significantly
animation_height = 704;		// Height of the images in the animation
animation_width = 480;		// Width of the images in the animation

//
// Global variables
//
theImages = new Array();
normal_delay = 300;
delay = normal_delay;  //delay between frames in 1/100 seconds
delay_step = 50;
delay_max = 4000;
delay_min = 50;
current_image = first_image;     //number of the current image
timeID = null;
status = 0;            // 0-stopped, 1-playing
size_valid = 0;
resetloop = 0;
old_delay = 0;

// Makes sure the first image number is not bigger than the last image number
if (first_image > last_image) 
{
   var help = last_image;
   last_image = first_image;
   first_image = help;
};
// Preload the images - gets executed first, while downloading the page
for (var i = first_image; i <= last_image; i++) 
{
	theImages[i] = new Image();
	if(image_prefix == 1 && i < 10) {
		i_pre = "0" + i;
	} else {
		i_pre = i;
	}
	theImages[i].src = image_name + "_" + i_pre + "." + image_type;
};
// Displays image depending on the play mode in forward direction
function animate_fwd() 
{
   current_image--;
   if(current_image < first_image)
   { 
	if(resetloop) {
	resetloop = 0;
	delay = old_delay;
        current_image = last_image;	//RESET LOOP
	} else {
	resetloop = 1;
	current_image = first_image;    //LOOP PAUSE
	old_delay = delay;
	delay = 1000;
	}
   }
  document.animation.src = theImages[current_image].src;
  document.control_form.frame_nr.value = current_image;
  timeID = setTimeout("animate_fwd()", delay);
}
// Displays image depending on the play mode in reverse direction
function animate_rev() 
{
   current_image++;
   if(current_image > last_image)
   { 
         current_image = first_image; //LOOP
   }
   document.animation.src = theImages[current_image].src;
   document.control_form.frame_nr.value = current_image;
   timeID = setTimeout("animate_rev()", delay);
}
// Changes playing speed by adding to or substracting from the delay between frames
function change_speed(dv) 
{
   delay+=dv;
   if(delay > delay_max) delay = delay_max;
   if(delay < delay_min) delay = delay_min;
}
// Stop the movie
function stop() 
{
	if(status == 1) {
		clearTimeout(timeID);
	}
	status = 0;
}
// Function to play animation forward
function fwd() 
{
   stop();
   status = 1;
   animate_fwd();
}
// Jumps to a given image number
function go2image(number)
{
   stop();
   if (number > last_image) number = last_image;
   if (number < first_image) number = first_image;
   current_image = number;
   document.animation.src = theImages[current_image].src;
   document.control_form.frame_nr.value = current_image;
}
// "Play reverse"
function rev() 
{
   stop();
   status = 1;
   animate_rev();
}
// Sets everything once the whole page and the images are loaded
function launch() 
{
   stop();
   current_image = last_image;
   document.animation.src = theImages[current_image].src;
   document.control_form.frame_nr.value = current_image;
   document.control_form.start_but.value = "First (#" + first_image + ")";
   document.control_form.end_but.value = "Last (#" + last_image + ")";
}
