// set the starting image.
var slideshow_img = 0;			
var playing = 1;
var adjust_to;

// The number of images in the array.
var num_imags = image_slide.length;
var slideshow_status = 1;



// The Fade Function
function SwapImage(x,y) {
	clearTimeout(adjust_to);
	$(image_slide[x]).appear({ duration: trans_duration });
	$(image_slide[y]).fade({duration: trans_duration});
	hilite_thumb(x);
	updateCaptions(x);
	adjust_to = setTimeout('adjust_filmstrip_pos()', 300);
}

function updateCaptions(num){
	if(typeof(window.image_captions) !== 'undefined') {
		if (image_captions[num].length > 0){
			$('slide_title').show();
			$('slide_title').innerHTML = image_captions[num];
		}
		else{
			$('slide_title').hide();
			$('slide_title').innerHTML = '';
		}
	}
}

// the onload event handler that starts the fading.
function StartSlideShow() {
	$('image-'+ '0').appear({ duration: trans_duration });
	play = setInterval('Play()', wait);							
}

function Toggle(){
	if (slideshow_status == 1){
		Stop();
		$('play_btn').src = '../common/images/slideshow/play_on.gif';
		slideshow_status = 0;
	}
	else{
		clearInterval(play);
		$('play_btn').src = '../common/images/slideshow/pause.gif';
		var imageShow, imageHide;
	
		imageShow = slideshow_img+1;
		imageHide = slideshow_img;
		
		
		
	imageShow = slideshow_img+1;
	imageHide = slideshow_img;
	
	if (imageShow == (num_imags)) {
		imageShow = 0;					
	}
	SwapImage(imageShow,imageHide);			
	slideshow_img = imageShow; 
		play = setInterval('Play()',wait);	
		slideshow_status = 1;
	}
}

function Pause(){
	if (slideshow_status == 1){
		Stop();
		//$('play_btn').src = '../common/images/slideshow/play_on.gif';
		slideshow_status = 0;
	}
}

function Play() {
	slideshow_status = 1;
	var imageShow, imageHide;

	imageShow = slideshow_img+1;
	imageHide = slideshow_img;
	
	if (imageShow == (num_imags)) {
		imageShow = 0;					
	}
	SwapImage(imageShow,imageHide);			
	slideshow_img = imageShow; 
}

function Stop () {
	//clearInterval(play);
}

function GoNext() {
	//clearInterval(play);
	
	var imageShow, imageHide;

	imageShow = slideshow_img+1;
	imageHide = slideshow_img;
	
	if (imageShow == num_imags) {
		SwapImage(0,imageHide);	
		slideshow_img = 0;					
	} else {
		SwapImage(imageShow,imageHide);			
		slideshow_img++;
	}
	
	if (slideshow_status == 1){
		//play = setInterval('Play()',wait);	
	}
	
}

function GoPrevious() {
	//clearInterval(play);

	var imageShow, imageHide;
				
	imageShow = slideshow_img-1;
	imageHide = slideshow_img;
	
	if (slideshow_img == 0) {
		SwapImage(num_imags-1,imageHide);	
		slideshow_img = num_imags-1;		
					
	} else {
		SwapImage(imageShow,imageHide);			
		slideshow_img--;
	}
	
	if (slideshow_status == 1){
		//play = setInterval('Play()',wait);	
	}
	
	
}


//***********************************

// FOR THE THUMBNAILS

//*************************************************


//thumbnails
var imag_interval = 54;//dist from img_top to img_top
var thumb_strip_ht = 8;//number of pictures that fit into the strip at once


var thumb_max = Math.floor(-imag_interval*(num_imags-thumb_strip_ht));
var cur_pos = 0;

function advance_strip(dir, count){
	clearTimeout(adjust_to);
	if (dir == 'up'){
		dist = Math.floor(count*imag_interval);
		cur_pos -= dist;
		if (cur_pos < thumb_max){
			cur_pos = thumb_max;
		}
		var a = new Effect.Move($('thumb_container'), { y: cur_pos, duration: 0.5, mode: 'absolute'});
	}
	else if (dir == 'down'){
		dist = Math.floor(count*imag_interval);
		cur_pos += dist;
		if (cur_pos > 0){
			cur_pos = 0;
		}
		var a = new Effect.Move($('thumb_container'), { y: cur_pos, duration: 0.5, mode: 'absolute'});
	}
}


function hilite_thumb(num){
	var img_on = $$('img.on');
	if (img_on.length > 0){
		for (i = 0; i < img_on.length; i++){
			img_on[i].removeClassName('on');
		}
		$('thumb_'+num).addClassName('on');
	}
}

function goto_slide(num){
	if (num != slideshow_img){
		SwapImage(num,slideshow_img);
		slideshow_img = num;
		//clearInterval(play);
		if (slideshow_status == 1){
			//play = setInterval('Play()',wait);	
		}
	}
}

function adjust_filmstrip_pos(){
	//determine if the image falls outside the filmstrip bounds
	//alert(cur_pos);
	var i_pos = Math.floor(slideshow_img*(-imag_interval));//position of the image
	var strip_top = cur_pos - imag_interval;
	var strip_bot = cur_pos -(imag_interval*thumb_strip_ht) + 2*imag_interval;
	
	
	
	if (slideshow_img == 0){
		var a = new Effect.Move($('thumb_container'), { y: 0, duration: 0.5, mode: 'absolute'});
		cur_pos = 0;
	}
	
	else if (i_pos <= strip_bot){
		var moveby = Math.ceil(-(i_pos - strip_bot)/imag_interval);
		advance_strip('up', moveby);
	}
	else if (i_pos >= strip_top){
		var moveby = Math.ceil((i_pos - strip_top)/imag_interval);
		advance_strip('down', moveby);
	}
}
