/* slideshow code adapted from http://www.bigbold.com/snippets/posts/show/1068 */

/* SLIDESHOW DELAY, in milliseconds */
var delay = 7500;

/* SLIDESHOW FADE TIME, in milliseconds */
var fade_time = 450;

/* MODE: loop or playonce */
var mode = "loop";

/* slideshow state */
var start_frame = 1;
var end_frame = 5;
var current_frame = 1;
var timeout_id = -1;
var state = "playing";

window.onload = function() {
   start_slideshow(start_frame, end_frame, delay);
}
    
function start_slideshow(start_frame, end_frame, delay) {
    if (state == "done") {
	// restarting - remove the end frame which is (supposedly) still showing.
	Element.hide('slide' + end_frame);
	Element.show('slide' + start_frame);
	set_current_frame(start_frame);
    }
    state = "playing";

    set_current_frame(start_frame);
    timeout_id = setTimeout(switch_slides(start_frame,start_frame,end_frame, delay), delay);
}
                            
function switch_slides(frame, start_frame, end_frame, delay) {
   return (function() {
	       var previous_frame = frame;
	       if (mode == "playonce" && frame == end_frame) { 
		   slideshow_done();
		   return true;
	       } 
	       else { 
		   frame = (frame == end_frame) ? start_frame : frame + 1; 
	       }

	       // hide the previous slide and replace it with a new one
	       Element.hide('slide' + previous_frame);
	       Effect.Appear('slide' + frame);

	       set_current_frame(frame);
	   
	       timeout_id = setTimeout(switch_slides(frame, start_frame, end_frame, delay), delay + fade_time);
	   });
}

function slideshow_goto(frame) {
    if (frame < start_frame || frame > end_frame) {
	return false;
    }

    // weird hack to get the play button to behave normally...
    if (state == "done") {
	state = "playing";
    }

    if (state == "playing") {
	slideshow_pause();
    }

    if (frame != current_frame) {
	Element.hide("slide" + current_frame);
	Element.show("slide" + frame);
	set_current_frame(frame);
    }
}

function slideshow_previous() {
    if (current_frame <= start_frame) {	
	return false;
    }
    slideshow_goto(current_frame - 1);
}

function slideshow_next() {
    if (current_frame >= end_frame) {	
	return false;
    }
    slideshow_goto(current_frame + 1);
}

function slideshow_pause() {
    if (state != "playing") {
	// START THE SLIDESHOW
	if (mode == "playonce" && state != "done" && current_frame == end_frame) {
	    // on last frame - stop immediately
	    slideshow_done();
	    return true;
	}

	$('slideshow-pause').innerHTML = '<img src="/images/button-pause.gif" border="0" alt="Pause" />';

	var restart_frame;
	if (state == "paused") {
	    // start up the slideshow again, from the current frame
	    state = "playing";
	    timeout_id = setTimeout(switch_slides(current_frame, start_frame, end_frame, delay), delay + fade_time);
	}
	else if (state == "done") {
	    // restart!
	    start_slideshow(start_frame, end_frame, delay);
	}
    }
    else {
	// PAUSE THE SLIDESHOW
	$('slideshow-pause').innerHTML = '<img src="/images/button-play.gif" border="0" alt="Play" />';
	state = "paused";

	// cancel the timeout waiting to switch slides
	clearTimeout(timeout_id);
    }
}

function slideshow_done() {
    $('slideshow-state').innerHTML = 'restart';
    state = "done";

    // cancel the timeout waiting to switch slides
    clearTimeout(timeout_id);
}

function set_current_frame(current) {
    for (var i = start_frame; i <= end_frame; i++) {
	Element.removeClassName("goto" + i, "current");
    }
    current_frame = current;
    Element.addClassName("goto" + current_frame, "current");
}

