var slide_path = '../outreach/slideshow/i';
var max_n_slides = 36;

function reset_slide_path(p, n)
{
	slide_path = p;
	if (n)
		max_n_slides = n;
}

// return filename for the n'th slide
function slide_filename(n)
{
	return slide_path + n + '.jpg';
}

var images = new Array();
var buttons = new Array();

// load a single named button into the buttons array
function preload_button(name, path)
{
	buttons[name] = new Image();
	buttons[name].src = path;
}

// load as many slides as there are in the repository
// in a random order
function preload_slides()
{
	// make random permutation of 0..n-1
	var r = new Array();
	for (var j = 0; j < max_n_slides; j++)
		r[j] = j;
	for (var k = 0; k < max_n_slides - 1; k++) {
		var m = k + Math.floor(Math.random() * (max_n_slides - k));
		var t = r[m]; r[m] = r[k]; r[k] = t;
	}
	// load in the random order
	for (var i = 0; i < max_n_slides; i++) {
		images[i] = new Image();
		images[i].src = slide_filename(r[i]);
	}
	preload_button('pause', 'images/pause.png');
	preload_button('pausegrayed', 'images/pausegrayed.png');
}

// current slide on the screen; -1 means nothing
var current_slide = -1;

// current direction of advance: +1 forward, -1 backward
var current_dir = 1;

// advance the current slide number to its next value
function advance_current_slide()
{
    current_slide = current_slide + current_dir;
    while (current_slide < 0)
    	current_slide = current_slide + max_n_slides;
    while (current_slide >= max_n_slides)
    	current_slide = current_slide - max_n_slides;
}

// show the next slide in the show
// force_p = false => make sure slide loaded successfully
// force_p = true  => show no matter what
function show_next_slide(force_p)
{
	var slide_area = document.getElementById("slideshow");
	if (!slide_area)
		slide_area = document['slideshow'];
	if (!slide_area)
		return;
	if (slide_area.filters)
		slide_area.style.filter = 'blendTrans(duration=1)';
	for (var i = 0; i < max_n_slides; i++) {
		advance_current_slide();
		w = images[current_slide].width;
		// unless forced, test to see if slide has downloaded successfully
		if (force_p || (typeof w != 'undefined' && w > 100)) {
			if (slide_area.filters) 
				slide_area.filters[0].apply();
			slide_area.src = images[current_slide].src;
			if (slide_area.filters) 
				slide_area.filters[0].play();
			return;
		}
	}
	// No slide was show-able.
	current_slide = -1;
}

var iid, ipending = 0;

// update slides every 6 seconds
function start_slide_rotator()
{
	if (!ipending) {
		iid = setInterval('show_next_slide(0)', 4000);
		ipending = 1;
	}
}

function stop_slide_rotator()
{
	if (ipending) {
		clearInterval(iid);
		ipending = 0;
	}
}

function on_load()
{
	// check for images support
	if (document.images) {
		preload_slides();
		start_slide_rotator();
	}
}

var click_pausing = 0, mouseover_pausing = 0;

function on_click_advance(dir)
{
	if (dir)
		current_dir = dir;
	document['pausebutton'].src = buttons['pause'].src;
	stop_slide_rotator();
	show_next_slide(0);
	if (!mouseover_pausing)
		start_slide_rotator();
	click_pausing = 0;
}

function on_click_pause()
{
	document['pausebutton'].src = buttons['pausegrayed'].src;
	stop_slide_rotator();
	click_pausing = 1;
}

function on_mouseover_pause()
{
	if (!click_pausing)
		stop_slide_rotator();
	mouseover_pausing = 1;
}

function on_mouseover_unpause()
{
	if (!click_pausing)
		start_slide_rotator();
	mouseover_pausing = 0;
}
