// wind.loop.v01.js
// v1 2007-05-11 mtd - Butchered IDR.loop.v06.js for the WATL Wind Forecast loops 

// Usage:
// (1) Call 'launch()' once (e.g. from BODY onload="..") to start this code.
// (2) Assumes that these Image dependent variables have already been defined:
		
// If there are some images, loop them.
// else when no images are available, exit now.
//	Assume that the enclosing html will provide a message.

// remember when this page started (so we can kill the loops later).
start_time = new Date();
start_seconds = start_time.getTime() / 1000;
loop_limit_seconds = 300+60;

var doc;	// Holds a pointer to MS or NN document (see launch).
  
// Now the general Looping code.
//============================================================
//                >> 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
//     modified by D. Watson and A. Earnhart (CIRA/CSU), 7/30/97
//     and Greg Thompson (NCAR/RAP) Dec. 11 1997
//============================================================

// step 1: define the images
// This is done in wind.js using Dynamic XMLHttpObject requests.
 
// step 2: define variables used to control images

var image_href = "";
var first_image = 0;
var last_image;

//=== 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 = 4000;
delay_min = 50;
dwell_multipler = 3;
dwell_step = 1;
end_dwell_multipler   = dwell_multipler;
start_dwell_multipler = dwell_multipler - 1;

current_image = first_image;     //number of the current image
timeID = null;
looping = 0;                      // 0-stopped, 1-playing
play_mode = 0;                   // 0-normal, 1-loop, 2-sweep
size_valid = 0;
 
//===> Load and initialize everything once page is downloaded 
//	(called from 'onLoad' in <BODY>)


function launch() {
	var i;
	last_image = nImages-1;
	doc = document;
	theImages = new Array();      //holds the images
	imageNum = new Array();       //keeps track of which images to omit from loop

	if(nImages==0) {
		doc.animation.src = ""	// Scrub the 'Please wait' image.
		return;
	}

	// If there is only 1 image, show it, but dont start the loop.
	if(nImages==1) {
		current_image = 0;
		theImages[current_image] = new Image();
		theImages[current_image].src = image_href + theImageNames[current_image];
		imageNum[current_image] = true;   // pretend it is ready.
		display_current_image();
		return;
	}
    
	// construct filenames for all images  in the loop
	for (i = first_image; i <= last_image; i++) {
		current_image = i;
		theImages[current_image] = new Image();
		theImages[current_image].src = image_href + theImageNames[current_image];
		imageNum[current_image] = false;  // image is not ready yet.
	}
	current_image = theStep;
	imageNum[current_image] = true;   // pretend it is ready.
 
   // this needs to be done to set the right mode when the page is manually reloaded
	change_mode (1);
	document.getElementById('h-value').value = theStep+1;

	display_current_image();
}





// Load the first next two images and previous two images from theStep
function launch_EC() {
	var i;
	last_image = nImages-1;
	doc = document;
	theImages = new Array();      //holds the images
	imageNum = new Array();       //keeps track of which images to omit from loop
  
	if(nImages==0) {
		doc.animation.src = ""	// Scrub the 'Please wait' image.
		return;
	}

	// construct filenames and preload the next two images
	for (i = theStep; i <= (theStep+2); i++) {
		if(i > last_image) break;
		current_image = i;
		theImages[current_image] = new Image();
		theImages[current_image].src = image_href + theImageNames[current_image];
		imageNum[current_image] = false;  // image is not ready yet.
	}
  
	// construct filenames and preload the previous two images
	for (i = theStep; i >= (theStep-2); i--) {
		if(i < first_image) break;
		current_image = i;
		theImages[current_image] = new Image();
		theImages[current_image].src = image_href + theImageNames[current_image];
		imageNum[current_image] = false;  // image is not ready yet.
	}

	current_image = theStep;
	imageNum[current_image] = true;   // pretend it is ready.

	display_current_image_EC();
}



function loop_time_OK ()
{
   // This whole web page should auto-refresh itself every nnn seconds
   // e.g. every 300 seconds (5 minutes).
   // If we see that the loops have been running for more than nnn seconds,
   // then assume that the browser has been iconised, which prevents html
   // refreshes, but does not stop the loop. The loop maybe uselessly
   // trying to fetch out of date images and generating a lot of 404's
   // swamping the web server.
   // So we stop the loop ourselves, and stop useless web traffic.
   // When the browser is un-iconised, the page-refresh kicks in an restarts it.
   // Usage:
   //    if (loop_time_OK ()) { continue looping }
   //    else { stop(); }
   /*
   now_time = new Date();
   now_seconds = now_time.getTime() / 1000;
   elaspsed_seconds = now_seconds - start_seconds;
   if (elaspsed_seconds < loop_limit_seconds) { return 1;}
   else { return 0;}
	*/
	return true;
}

//==> Display the 'current_image'.

function display_current_image() {
   //display image onto screen
   document.animation.src = theImages[current_image].src;
   if(theImages.length>1) {
      document.getElementById('h-value').value = current_image+1;
		if(current_image <= 0) {
			document.getElementById('earlier_img_div').innerHTML = "<img src=\"/watl/images/symbols/earlier-0.gif\" id=\"earlier_img\" name=\"earlier_img\" alt=\"Previous forecast\" title=\"Previous forecast\" width=\"64\" height=\"11\" /></a><br /> ";
		} else {
			document.getElementById('earlier_img_div').innerHTML = "<a href=\"javascript:func();\" onclick=\"decrementImage();\"><img src=\"/watl/images/symbols/earlier.gif\" id=\"earlier_img\" name=\"earlier_img\" alt=\"Previous forecast\" title=\"Previous forecast\" width=\"64\" height=\"11\" /></a><br />";
		}
		if(current_image >= theImages.length-1) {
			document.getElementById('later_img_div').innerHTML = "<img src=\"/watl/images/symbols/later-0.gif\" id=\"later_img\" name=\"later_img\" alt=\"No later forecast available\" title=\"No later forecast available\" width=\"64\" height=\"11\" /><br />";
		} else {
			document.getElementById('later_img_div').innerHTML = "<a href=\"javascript:func();\" onclick=\"incrementImage();\"><img src=\"/watl/images/symbols/later.gif\" id=\"later_img\" name=\"later_img\" alt=\"Next forecast\" title=\"Next forecast\" width=\"64\" height=\"11\" /></a><br />";
		}  
		document.forms.control_form.time.selectedIndex = current_image;
   }
	s.setValue(current_image+1);
}

//==> Display the 'current_image' for the EC model.

function display_current_image_EC() {
   //display image onto screen
   document.animation.src = theImages[current_image].src;
   if(theImages.length>1) {
		if(current_image <= 0) {
			document.getElementById('earlier_img_div').innerHTML = "<img src=\"/watl/images/symbols/earlier-0.gif\" id=\"earlier_img\" name=\"earlier_img\" alt=\"Previous forecast\" title=\"Previous forecast\" width=\"64\" height=\"11\" /></a><br /> ";
		} else {
			document.getElementById('earlier_img_div').innerHTML = "<a href=\"javascript:func();\" onclick=\"decrementImageEC();\"><img src=\"/watl/images/symbols/earlier.gif\" id=\"earlier_img\" name=\"earlier_img\" alt=\"Previous forecast\" title=\"Previous forecast\" width=\"64\" height=\"11\" /></a><br />";
		}
		if(current_image >= theImages.length-1) {
			document.getElementById('later_img_div').innerHTML = "<img src=\"/watl/images/symbols/later-0.gif\" id=\"later_img\" name=\"later_img\" alt=\"No later forecast available\" title=\"No later forecast available\" width=\"64\" height=\"11\" /><br />";
		} else {
			document.getElementById('later_img_div').innerHTML = "<a href=\"javascript:func();\" onclick=\"incrementImageEC();\"><img src=\"/watl/images/symbols/later.gif\" id=\"later_img\" name=\"later_img\" alt=\"Next forecast\" title=\"Next forecast\" width=\"64\" height=\"11\" /></a><br />";
		}  
		document.forms.control_form.time.selectedIndex = current_image;
   }
}

//==> Display the 'current_image' from the slider.

function display_current_image_from_slider(position) {
	current_image = position;
   //display image onto screen
	if((position >= 0) && (position < nImages)) {
   	document.animation.src = theImages[position].src;
		if(theImages.length>1) {
			document.getElementById('h-value').value = position+1;
			//alert(document.earlier_img.src + " ... " + document.later_img.src);
			if(position <= 0) {
				document.getElementById('earlier_img_div').innerHTML = "<img src=\"/watl/images/symbols/earlier-0.gif\" id=\"earlier_img\" name=\"earlier_img\" alt=\"Previous forecast\" title=\"Previous forecast\" width=\"64\" height=\"11\" /></a><br /> ";
			} else {
				document.getElementById('earlier_img_div').innerHTML = "<a href=\"javascript:func();\" onclick=\"decrementImage();\"><img src=\"/watl/images/symbols/earlier.gif\" id=\"earlier_img\" name=\"earlier_img\" alt=\"Previous forecast\" title=\"Previous forecast\" width=\"64\" height=\"11\" /></a><br />";
			}
			if(position >= theImages.length-1) {
				document.getElementById('later_img_div').innerHTML = "<img src=\"/watl/images/symbols/later-0.gif\" id=\"later_img\" name=\"later_img\" alt=\"No later forecast available\" title=\"No later forecast available\" width=\"64\" height=\"11\" /><br />";
			} else {
				document.getElementById('later_img_div').innerHTML = "<a href=\"javascript:func();\" onclick=\"incrementImage();\"><img src=\"/watl/images/symbols/later.gif\" id=\"later_img\" name=\"later_img\" alt=\"Next forecast\" title=\"Next forecast\" width=\"64\" height=\"11\" /></a><br />";
			}  
			document.forms.control_form.time.selectedIndex = position;
		}
	}
}


 
//===> Stop the animation

function stop() {
//== cancel animation (timeID holds the expression which calls the fwd or bkwd function) ==
	if(looping == 1) {
		clearTimeout (timeID);
	}
	looping = 0;
	return;
}

//===> Display animation in fwd direction in either loop or sweep mode

function animate_fwd() {
   if(nImages<=1) return;
   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] == false) {
		if(theImages[current_image].complete) {
			imageNum[current_image] = true;
			break;
		}
		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;
			}
		}
	}
 
	display_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 ==
	// (Maybe stop the loops if we are iconised).
	// if (loop_time_OK()) {	timeID = setTimeout("animate_fwd()", delay_time); }
	if(looping == 1) {	timeID = setTimeout("animate_fwd()", delay_time); }
	else { stop(); }
}
 
 

//===> Increment to next image

function incrementImage()
{
	var number;
	if(nImages<=1) return;
	stop();
	current_image++;
	number = current_image;

	//== 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] == false) {
		if(theImages[number].complete) {
			imageNum[number] = true;
			break;
		}
		number++;
		if(number > last_image) number = first_image;
	}
 
	current_image = number;
	display_current_image();
}


// **********************************************************************************************
// We need a separate function for the EC images since they do not have the loop function

function incrementImageEC()
{
	var number;
	if(nImages<=1) return;

	current_image++;
	number = current_image;
	if(((current_image+2) <= last_image) && (imageNum[current_image+2] == null)) {
		theImages[current_image+2] = new Image();
		theImages[current_image+2].src = image_href + theImageNames[current_image+2];
		imageNum[current_image+2] = false;  // image is not ready yet.
	}

	//== check to ensure that image is loaded
	while(imageNum[number] == false) {
		if(theImages[number].complete) {
			imageNum[number] = true;
			break;
		}
	}
 
	display_current_image_EC();
}

 

//===> Decrement to next image

function decrementImage()
{
	var number;
	if(nImages<=1) return;
	stop();
	current_image--;
	number = current_image;
 
	//== 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] == false) {
		if(theImages[number].complete) {
			imageNum[number] = true;
			break;
		}
		number--;
		if(number < first_image) number = last_image;
	}
 
	current_image = number;
	display_current_image();
}


function decrementImageEC()
{
	var number;
	if(nImages<=1) return;

	current_image--;
	number = current_image;
	if(((current_image-2) >= first_image) && (imageNum[current_image-2] == null)) {
		theImages[current_image-2] = new Image();
		theImages[current_image-2].src = image_href + theImageNames[current_image-2];
		imageNum[current_image-2] = false;  // image is not ready yet.
	}

	//== check to ensure that image is loaded
	while(imageNum[number] == false) {
		if(theImages[number].complete) {
			imageNum[number] = true;
			break;
		}
	}
 
	display_current_image_EC();
}


 
//===> 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;
}
 
 

 
//===> "Play forward"

function fwd()
{
	stop();
	slider_interrupt = 0;
	looping = 1;
	play_mode = 1;
	animate_fwd();
}
 
 
//===> Change play mode (normal, loop, swing)

function change_mode(mode)
{
	play_mode = mode;
}
 
 
//==> Empty function - used to deal with image buttons rather than HTML buttons
function func()
{
}
 