

//ONLY ANIMATION CODE BELOW HERE
// 

//============================================================
//                >> jsImagePlayer 1.0 <<
//            for Netscape3.0+, September 1996
//============================================================
//                  by (c)BASTaRT 1996
//             Praha, Czech Republic, Europe
//
// feel free to copy and use as long as the credits are given
//          by having this header in the code
//
//          contact: xholecko@sgi.felk.cvut.cz
//          http://sgi.felk.cvut.cz/~xholecko
//
//============================================================
// Thanx to Karel & Martin for beta testing and suggestions!
//============================================================
//
//     modified by D. Watson and A. Earnhart (CIRA/CSU), 7/30/97
//     and Greg Thompson (NCAR/RAP) May 07, 2000
//     and Mathew L. Powers (College of DuPage) October 24, 2001
//============================================================
 
//********* SET UP THESE VARIABLES - MUST BE CORRECT!!!*********************

//modImages = new Array();
//Set this in another function
/*
	modImages[0] = "/grads_images/20080930/eu_1_max_temp_20080930.png";
	modImages[1] = "/grads_images/20080930/eu_2_max_temp_20080930.png";
	modImages[2] = "/grads_images/20080930/eu_3_max_temp_20080930.png";
	modImages[3] = "/grads_images/20080930/eu_4_max_temp_20080930.png";
	modImages[4] = "/grads_images/20080930/eu_5_max_temp_20080930.png";
	modImages[5] = "/grads_images/20080930/eu_6_max_temp_20080930.png";
	modImages[6] = "/grads_images/20080930/eu_7_max_temp_20080930.png";

first_image = 1;
last_image = 7;
*/


//first_image = 1;
//last_image = 15;
 
//**************************************************************************
 
//=== THE CODE STARTS HERE - no need to change anything below ===
 
//=== global variables ====
theImages = new Array();      //holds the images
imageNum = new Array();       //keeps track of which images to omit from loop
normal_delay = 300;
delay = normal_delay;         //delay between frames in 1/100 seconds
delay_step = 50;
delay_max = 6000;
delay_min = 50;
dwell_multipler = 3;
dwell_step = 1;
end_dwell_multipler   = dwell_multipler;
start_dwell_multipler = dwell_multipler;
current_image = first_image;     //number of the current image
timeID = null;
status = 0;                      // 0-stopped, 1-playing
play_mode = 0;                   // 0-normal, 1-loop, 2-sweep
size_valid = 0;
 
//===> Make 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 first image (while page is downloading)
   //theImages[0] = new Image();
   //theImages[0].src = modImages[0];
   //imageNum[0] = true;
 
//==============================================================
//== All previous statements are performed as the page loads. ==
//== The following functions are also defined at this time.   ==
//==============================================================
 
//===> Stop the animation
function stop()
{
   //== cancel animation (timeID holds the expression which calls the fwd or bkwd function) ==
   clearTimeout (timeID);
   if (status == 1)
      clearTimeout (timeID);
   status = 0;
}
 
 
//===> Display animation in fwd direction in either loop or sweep mode
function animate_fwd()
{
   current_image++;                      //increment image number
   /*
 
   //== check if current image has exceeded loop bound ==
   if (current_image > last_image) {
      if (play_mode == 1) {              //fwd loop mode - skip to first image
         current_image = first_image;
      }
      if (play_mode == 2) {              //sweep mode - change directions (go bkwd)
         current_image = last_image;
         animate_rev();
         return;
      }
   }
   
 
   //== check to ensure that current image has not been deselected from the loop ==
   //== if it has, then find the next image that hasn't been ==
   while (imageNum[current_image-first_image] == false) {
         current_image++;
         if (current_image > last_image) {
            if (play_mode == 1)
               current_image = first_image;
            if (play_mode == 2) {
               current_image = last_image;
               animate_rev();
               return;
            }
         }
   }
   */
   
   
 
   //TESTdocument.animation.src = theImages[current_image-first_image].src;   //display image onto screen
   //mattchangeImage(current_image-first_image);
   mattchangeImage(current_image);
   
   //TESThighlightPeriod(current_image);

   delay_time = delay;
   if (current_image == first_image) delay_time = start_dwell_multipler*delay;
   if (current_image == last_image)   delay_time = end_dwell_multipler*delay;
 
   //== call "animate_fwd()" again after a set time (delay_time) has elapsed ==
   //REMOVE$('#debug').append("<br><br>animate_fwd() delay_time="+delay_time+" <br><br>");
   timeID = setTimeout("animate_fwd()", delay_time);
}
 
 

 
 
//===> Changes playing speed by adding to or substracting from the delay between frames
function change_speed(dv)
{
   delay+=dv;
   //== check to ensure max and min delay constraints have not been crossed ==
   if(delay > delay_max) delay = delay_max;
   if(delay < delay_min) delay = delay_min;
}
 
//===> functions that changed the dwell rates.
function change_end_dwell(dv) {
   end_dwell_multipler+=dv;
   if ( end_dwell_multipler < 1 ) end_dwell_multipler = 0;
   }
 
function change_start_dwell(dv) {
   start_dwell_multipler+=dv;
   if ( start_dwell_multipler < 1 ) start_dwell_multipler = 0;
   }
 
//===> Increment to next image
function incrementImage(number)
{
	mattchangeImage(number);
	stopImage();
	return;
   stop();
   //$('div#debug').append('<br>NUMBER='+number+' first='+first_image+' last='+last_image+':<br>');
 
   //== if image is last in loop, increment to first image ==
   if (number > last_image) number = first_image;
 
   //== check to ensure that image has not been deselected from loop ==
   while (imageNum[number-first_image] == false) {
         number++;
         if (number > last_image) number = first_image;
   }
 
   current_image = number;
   //document.animation.src = theImages[current_image-first_image].src;   //display image
   //$('#animation_image').html("<img name='animation' class='hiddenElements' src='"+theImages[current_image-first_image].src+"' border='0'>");
   
   //Show the requested image (still not visible b/c another one is in front of it
   curImageNum=current_image-1;
   $('#loop_image_'+curImageNum+' img').removeClass("hiddenElements");
   
   //Hide the previously displayed image
   $('#loop_image_'+prevImageNum+' img').addClass("hiddenElements");
   
   highlightPeriod(current_image);
   stopImage();
   
   prevImageNum=curImageNum;
}

function mattchangeImage(number){
	
	//REMOVE$('#debug').html("**Begin mattchangeImage("+number+")<br>");
	//REMOVE$('#debug').append("**Begin mattchangeImage("+number+")<br>");
	
	stop();	
	
	if (number > last_image) {
		//== if image is last in loop, increment to first image ==
		//REMOVE$('#debug').append("  number > "+last_image+"; new value number = "+first_image+"<br>");
		number = first_image;
	}
	else if(number < first_image) {
		//== if image is less than the first in loop, decrement to last image ==
		//REMOVE$('#debug').append("  number < "+first_image+"; new value number = "+last_image+"<br>");
		number = last_image;
		
	}
	else if(number >= first_image && number <= last_image) {
		//New image is w/in the bounds
		//REMOVE$('#debug').append("  W/in bounds number >= "+first_image+" && number <= "+last_image+"<br>");
	}
	else{
		//Number appears to be out of bounds
		alert("Invalid image number ("+number+")");
		return;
	}
	
	
	//== check to ensure that image has not been deselected from loop ==
	while (imageNum[number-first_image] == false) {
		number++;
		if (number > last_image) number = first_image;
	}
	current_image = number;
	
	
	//Show the requested image (still not visible b/c another one is in front of it
	curImageNum=number-1;
	currentImageID=currentBaseImageID+"_"+curImageNum;

	//$('#'+period+'_loop_image_'+curImageNum+' img').removeClass("hiddenElements");
	$('#'+currentImageID+' img').removeClass("hiddenElements");

	//Hide the previously displayed image, as long as it is not the same as the current one
	if(prevImageNum == curImageNum && period == prevImagePeriod){
		//Same as the previous image
	}
	else{
		//Not the same

		$('#'+prevImageID+' img').addClass("hiddenElements");
	}

	//REMOVE$('#debug').append("  HIGHLIGHT PERIOD ("+number+")");
	highlightPeriod(number);
	//stopImage();

	prevImageNum=curImageNum;
	prevImagePeriod=period;
	prevImageID=currentImageID;
	
	//REMOVE$('#debug').append("**End mattchangeImage("+number+")<br><br>");
	
	//return 1;
	
	
	
}
 
//===> Decrement to next image
function decrementImage(number)
{
	mattchangeImage(number);
	stopImage();
	return;
   stop();
 
   //== if image is first in loop, decrement to last image ==
   if (number < first_image) number = last_image;
 
   //== check to ensure that image has not been deselected from loop ==
   while (imageNum[number-first_image] == false) {
         number--;
         if (number < first_image) number = last_image;
   }
 
   current_image = number;
   document.animation.src = theImages[current_image-first_image].src;   //display image
   highlightPeriod(current_image);
   stopImage();
}
 
//===> "Play forward"
function playFwd()
{
   stop();
   status = 1;
   play_mode = 1;
   animate_fwd();
}

//===> jumps to a given image number
function go2image(number)
{
   stop();
   if(number > last_image){
   	 number = last_image;
   }
   else if(number < first_image){
   	 number = first_image;
   }
   else{
   	 
   }
   current_image = number;
   document.animation.src = theImages[current_image-1].src;
   //document.control_form.frame_nr.value = current_image;
   highlightPeriod(current_image);
}

 
//===> Change play mode (normal, loop, swing)
function change_mode(mode)
{
   play_mode = mode;
}
 
//===> Load and initialize everything once page is downloaded (called from 'onLoad' in <BODY>)
function launch()
{
   for (var i = first_image + 1; i <= last_image; i++)
   {
      theImages[i-first_image] = new Image();
      theImages[i-first_image].src = modImages[i-first_image];
      imageNum[i-first_image] = true;
      
      //document.animation.src = theImages[i-first_image].src;
      //document.control_form.frame_nr.value = i;
   }
 
   // this needs to be done to set the right mode when the page is manually reloaded
   //change_mode (1);
   change_mode (0);
   //fwd();
   //stop();
   decrementImage(1);
   //return;
   
}
 
//===> Check selection status of image in animation loop
function checkImage(status,i)
{
   if (status == true)
      imageNum[i] = false;
   else imageNum[i] = true;
}
 
//==> Empty function - used to deal with image buttons rather than HTML buttons
function func()
{
}
 
//===> Sets up interface - this is the one function called from the HTML body
function animation()
{
  count = first_image;
}