// imageCourrante: current image
imageCourrante = 0;

var pause=10;
var imageTotText = '';
var startSelectedIndex = 0;
var endSelectedIndex = 0;
var click_play = 0;

// increment: loop flag 0=off, 1=on
increment = 0;

// incrementStep: direction of the animation 1=fwd, -1=reverse
incrementStep = 1;

// imageNum: current displayed image
imageNum = 0;

var Nav   = navigator.appName ;
var isDOM = (document.getElementById ? true : false);
var isIE4 = (document.all);

theImages = new Array();
theImagesComplete = new Array();

function set_theImages(deselectRadio) {
	//
	// set_theImages: fill the array 'theImages' with
	//                a list of images based on the start and
	//                end times in the SELECT widgets
	
	if (deselectRadio) deselectTimeFrame();
	
	// set increment to 0 to stop the animation loop
	increment = 0;
	
	// get the start and end index values from the SELECT widgets
	if (document.myform.TimeFrameStart) { 
		index_end = document.myform.TimeFrameEnd.selectedIndex;
		index_start = document.myform.TimeFrameStart.selectedIndex;
		imageTot = index_end - index_start + 1;
	}
	else
	{
		index_start = startSelectedIndex; 
		index_end = endSelectedIndex;
		imageTot = endSelectedIndex - startSelectedIndex + 1;
		//imageTot = numInitImages;
	}
	
	//alert ('index_start is ' + index_start + ' and index_end is ' + index_end + ' and imageTot is ' + imageTot);
	if ( imageTot < 0 ) {
	    alert('Invalid Time Frame: Start Time must be earlier than End Time');
	    return;
	}
	
	imageTotText = padTextPrefix('' + imageTot,'0',3);
	
	// Display the initial image number and time in the textboxes
//	imageCourranteText = padTextPrefix('' + '0','0',3);
//	document.myform.FrameText.value = imageCourranteText + '/' + imageTotText;
// updateImageText();
	
	setPause();
	
	// set the theImages array to the list of images
	for (imageCourrante=0; imageCourrante<imageTot; imageCourrante++) {
		//alert('setting src for theImages at index ' + imageCourrante + ' and index_start is ' + index_start);

		theImages[imageCourrante] = new Image();
		theImages[imageCourrante].src = theImagesComplete[imageCourrante+index_start];
	}

	// set the display to the first image in the list
	imageNum = 0;
	nextFrame(0);
}

function setPause () {
	//
	// setPause: set the end of loop pause based on
	//           the number of frames in the animation
	//
	if (imageTot > maxPause) {
		//alert('setting pause to ' + maxPause + ' since imagetot ' + imageTot + '>' + maxPause);
		pause = maxPause;
	} else {
		pause = imageTot-1;
		//alert('setting pause to ' + pause + ' which is ' + imageTot + '-1');
	}
}

function deselectTimeFrame() {
	//
	// deselectTimeFrame: unselect time frame radio buttons
	//
	if (document.myform.timeFrameRadio) {
		if ( document.myform.timeFrameRadio.length ) {
			for (var i in document.myform.timeFrameRadio) {
				document.myform.timeFrameRadio[i].checked = false;
			}
		}
	}
}

function setTimeFrame(num) {
    //
    // setTimeFrame: set the time frame based on the last $num images
    //
	if (document.myform.TimeFrameStart){
		if ( theImagesComplete.length > num ) {
        document.myform.TimeFrameStart.selectedIndex = theImagesComplete.length - num ;
		} else {
			document.myform.TimeFrameStart.selectedIndex = 0;
		}
		document.myform.TimeFrameEnd.selectedIndex = theImagesComplete.length -1;
	}	else {
		if ( theImagesComplete.length > num ) {
         startSelectedIndex = theImagesComplete.length - num ;
		} else {
			startSelectedIndex = 0;
		}
		endSelectedIndex = theImagesComplete.length -1;
	}
	set_theImages(0);
}

function animate() {
    //
    // animate: start animation loop
    //
    //window.status='animating with delay ' + delay + ' and increment is ' + increment;
    if (increment != 0) {  
        nextFrame(increment);
        //if (Nav != 'Netscape'  && !isIE4 && Nav != 'Konqueror') {
          setTimeout ('animate ()',delay);
        //}
    }
}

function nextFrame(inc) {
	//
	// nextFrame: display the next image in the array theImages
	//
	imageNum += inc;

	if (Nav == 'Konqueror') {
		// go back to the first image
		if(imageNum >= imageTot)
     	{	imageNum = 0;  }
			// go to the last image
			if(imageNum < 0)
              {	imageNum = imageTot - 1; }
 	} else {
		// go back to the first image
		if(imageNum >= imageTot+pause-1) {
			//alert ('going back to first image since imageNum of ' + imageNum + '>= ' + imageTot + ' + ' + pause + ' -1');
			imageNum = 0;
		}

		// go to the last image
		if(imageNum < 0-pause) {
			imageNum = imageTot - 1;  }
 	}

	if(imageNum >= imageTot) {
		// display the last image 
		imageCourrante=imageTot - 1;
	} else if(imageNum <0) {
		// display the first image
		imageCourrante = 0;
	} else {
		imageCourrante=imageNum;
	}

	if((imageNum < imageTot) && (document.myform.FrameText)){
 		updateFrameText();
	}
	//alert('imageCourrante in nextFrame inc ' + inc + ' is ' + imageCourrante);
	document.animation.src = theImages[imageCourrante].src;
}

function slower() {
    //
    // slower: increase the delay between images
    //
    if (speed > 1) {
	speed-=1;
	setDelay();
        // updateSpeedText();
    }
}

function faster() {
    //
    // faster: decrease the delay between images
    //
    if (speed < numberOfSpeeds) {
	speed+=1;
        setDelay();
        // updateSpeedText();
    }

}

function lastImage() {
    //
    // lastImage: display the last image in the array
    //
    increment = 0;
    imageNum = imageTot-1;
    nextFrame(0);
}

function firstImage() {
    //
    // firstImage: display the first image in the array
    //
    increment = 0;
    imageNum = 0;
    nextFrame(0);
}

function stepBackward() {
    //
    // backward: display the previous image
    //
    increment = 0;
    if (imageNum > 0) {
        nextFrame(-1);
	}	
}

function stepForward() {
    //
    // forward: display the next image
    //
    increment = 0;
    if (imageNum < imageTot -1 )
    {    nextFrame(1);    }
}

function setDelay() {
    // 
    // setDelay: set delay based on speed
    //

    delay = (numberOfSpeeds - speed) * 100;

    if (delay == 0) { delay = 50; }
    //window.status='setDelay to ' + delay;
    //alert('setDelay to ' + delay);
}

function speedReset() {
    // 
    // speedReset: reset the frame delay to its initial value
    //
    speed = initSpeed;
    setDelay();
    // updateSpeedText();
}

function stopPlay() {
    //
    // stopPlay: turn off the animation loop
    //
    increment = 0;
}

function updateFrameText() {
    //
    // updateFrameText: update the text widget with the current frame status
    //
    var imageCourranteText;
    imageCourranteText = imageCourrante + 1;
	
    // add leading zero(s)
    imageCourranteText = padTextPrefix('' + imageCourranteText,'0',3);

    document.myform.FrameText.value = imageCourranteText + '/' + imageTotText;
    //alert('updating frame text to ' + imageCourranteText + '/' + imageTotText);
    updateImageText();
}

function updateImageText() {
    //
    // updateImageText: update the text widget with the current frame status
    //
    var offset ;
    var t ; 
	var coffset ;
	if (document.myform.TimeFrameStart) {
		offset = document.myform.TimeFrameStart.selectedIndex;
		coffset = imageCourrante + offset - 0 ;
		//alert('offset'+offset);
		t = document.myform.TimeFrameEnd[coffset].text;
		//alert (t);
		document.myform.ImageText.value = t;
	} else {
		offset = startSelectedIndex;
		coffset = imageCourrante + offset + 0;
		t = a_frames[coffset].date;
		document.myform.ImageText.value = t;
	}
}

function updateSpeedText() {
    //
    // updateSpeedText: update tge Frame delay text widget with the current 
    //                  frame delay

    document.myform.SpeedText.value= speed;
}

function startPlay() {
    //
    // startPlay: change the direction of the animation
    //
	if(click_play == 0) {
		document.myform.pause.src=icon_dir+'/pause.gif';
		document.myform.first.src=icon_dir+'/first.gif';
		document.myform.last.src=icon_dir+'/last.gif';
		document.myform.prev.src=icon_dir+'/prev.gif';
		document.myform.next.src=icon_dir+'/next.gif';
		document.myform.slowdown.src=icon_dir+'/slowdown.gif';
		document.myform.speedup.src=icon_dir+'/speedup.gif';
		if (lang == 'en') {
			document.myform.speedreset_en.src=icon_dir+'/speedreset_en.gif';
		} else {
			document.myform.speedreset_fr.src=icon_dir+'/speedreset_fr.gif';
		}
	}
	
	click_play = 1;
	//alert ('increment is ' + increment + ' and imageTot is ' + imageTot);
	if (increment == 0) {
		if ( imageTot < 2 ) {
			//alert('defaulting to last 6 images since imageTot is less than 2');
			if (document.myform.timeFrameRadio) {   
				if ( document.myform.timeFrameRadio.length ) {
	        		 // default to the last 6 images
                	document.myform.timeFrameRadio[0].checked=true;
				}
			}
			//if (document.myform.TimeFrameStart) { 
				setTimeFrame(numInitImages); 
			//}
		 }
	    increment = incrementStep;
       animate();  
    } 
}

function change_direction(value) {
    //
    // change_direction: change the direction of the animation
    //

    incrementStep = value;

    // if an animation is running, change the increment
    // value so that the animation changes direction in
    // mid stream
    if (increment != 0) increment = value;
}

function padTextPrefix (InString, PadChar, DefLength)  { 
        if (InString.length>=DefLength)
                return (InString); 
        OutString=InString 
        for (Count=InString.length; Count<DefLength; Count++)  { 
                OutString=PadChar+OutString; 
        } 
        return (OutString); 
}

function debug (what) {
    //alert('debug: ' + what);
    window.status=what;
}
