/*
function parseXml(theXml) {
	try {
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	}
	catch(e)
		{
		try {
			xmlDoc=document.implementation.createDocument("","",null);
		}
		catch(e)
		{
			alert(e.message);
			return;
		}
	}
	xmlDoc.async=false;
	xmlDoc.load(theXml);
	return xmlDoc
}
*/

//============================================================
//                >> 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!
//============================================================
//  New GUI Design by Brian Hughes, NOAA/NESDIS/SAB 12/17/99
//  Assorted hacks added by T. Spindler NHC/TPC 7/25/00
//============================================================

//======================================================================
//Code modified by Anton Vasilescu, April 2009 to work for my needs
//If you need the original version check the links above.
//======================================================================
 
//===> Stop the animation
function stop(){
   //== cancel animation (timeID holds the expression which calls the fwd or bkwd function) ==
   if (ani_status == 1)
      clearTimeout(timeID);
   ani_status = 0;
}
 

//===> Display animation in fwd direction
function animate_fwd()
{
   current_image += image_increment;        //increment image number
 
   //== check if current image has exceeded loop bound ==
   if (current_image > last_image)
         current_image = first_image;
  
   document.getElementById("charts-img").src = theImages[current_image].src;
   document.getElementById("text_animation").innerHTML = imageText[current_image].childNodes[0].nodeValue

   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 ==
   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;
   }
 
//===> rewind to start of image sequence
function rewind()
{
   stop();
   current_image = first_image;
   document.getElementById("charts-img").src = theImages[current_image].src;
   document.getElementById("text_animation").innerHTML = imageText[current_image].childNodes[0].nodeValue
}

//===> fast forward to end of image sequence
function ffwd()
{
   stop(); 
   current_image = last_image;
   document.getElementById("charts-img").src = theImages[current_image].src;
   document.getElementById("text_animation").innerHTML = imageText[current_image].childNodes[0].nodeValue
}

//===> Increment to next image
function incrementImage(number)
{
   stop();
 
   number += image_increment;
   if (number > last_image) number = first_image; 
 
   current_image = number;
   document.getElementById("charts-img").src = theImages[current_image].src;
   document.getElementById("text_animation").innerHTML = imageText[current_image].childNodes[0].nodeValue
}
 
//===> Decrement to next image
function decrementImage(number)
{
   stop();
 
   number -= image_increment;
   if (number < first_image) number = last_image;
   
   current_image = number;
   document.getElementById("charts-img").src = theImages[current_image].src;
   document.getElementById("text_animation").innerHTML = imageText[current_image].childNodes[0].nodeValue
}
 
//===> "Play forward"
function fwd()
{
   stop();
   ani_status = 1;
   animate_fwd();
}

//===> Load and initialize everything once page is downloaded (called from 'onLoad' in <BODY>)
function launch()
{
   //for (var i = first_image; i <= last_image; i += image_increment)
   for (var i = 0; i <= 60; i += image_increment)
   {
      theImages[i] = new Image();
      theImages[i].src = "http://socalscubainfo.com/wprs/maindir/images/forecast/" + imgName[i].childNodes[0].nodeValue;
   }
	document.getElementById("charts-img").src = theImages[0].src;
	document.getElementById("text_animation").innerHTML = imageText[0].childNodes[0].nodeValue;
}
 
//===> Sets up interface - this is the one function called from the HTML body
function animation(theXml)
{
  parseXml(theXml)
  last_image = 60;
  
	first_image = 0;
	theImages = new Array();      //holds the images
	imgName = xmlDoc.getElementsByTagName("imageName");
	imageText = xmlDoc.getElementsByTagName("imageText");
	normal_delay = 500;
	delay = normal_delay;         //delay between frames in 1/100 seconds
	delay_step = 25;
	delay_max = 4000;
	delay_min = 50;
	dwell_multipler = 1;
	dwell_step = 1;
	end_dwell_multipler   = dwell_multipler;
	start_dwell_multipler   = 1;
	current_image = first_image;     //number of the current image
	image_increment = 1;
	timeID = null;
	ani_status = 0;                      // 0-stopped, 1-playing
	size_valid = 0;
	num_loops = 0;			   // initializes loop number counter
	
	//===> Preload the first image (while page is downloading)
   theImages[0] = new Image();
   theImages[0].src = "http://socalscubainfo.com/wprs/maindir/images/forecast/" + imgName[0].childNodes[0].nodeValue;

  count = first_image;
  launch();
  fwd();
}
