/*
	JavaScript slideshow script:
	
	This script and the preload script that it calls are based on a script by Guyon Roche, guyonroche@silver-daggers.co.uk.
	We've modified it in countless ways, but the primary change enables the slideshow to start playing
	once the first image has loaded, rather than having to wait for all of the images to load.
	Then the script waits (politely and sequentially) for the each image before attempting to display it.
	
	
	
	If you have questions about the slideshow scripts, please contact Craig Farris, cfarris@ucdavis.edu.
*/

var aSlides = new Array;
var show_links = false;
var show_captions = false;
var show_more = false;
var delay = 8000;
var holder_class;
var captions_array, more_array, links_array;
var sc_current = 0;
var sc_pause = 0;
var tid=0;

function initSlideshow(source_xml, slideshow_wrapper, arg_delay, arg_show_captions, arg_show_links, arg_show_more) {
	
	if ( typeof ( slideshow_wrapper ) == "undefined" ) {
		holder_class = "slideshow_container";
	} else {
		holder_class = slideshow_wrapper;
	}
	
	if ( typeof ( arg_show_captions ) == "undefined" ) {
		show_captions = false;
	} else if (arg_show_captions == true) {
		show_captions = true;
	}
	
	if ( typeof ( arg_show_links ) == "undefined" ) {
		show_links = false;
	} else if (arg_show_links == true) {
		show_links = true;
	}
	
	if ( typeof ( arg_show_more ) == "undefined" ) {
		show_more = false;
	} else if (arg_show_more == true) {
		show_more = true;
	}
	
	if ( typeof ( arg_delay ) == "undefined" ) {
		delay = 8000;
	} else {
		delay = arg_delay * 1000;
	}
	
/* Show the js_slideshow div and display the loading spinner graphic */
	var image_target = document.getElementById('js_slideshow');
	Element.show(image_target);
	Element.addClassName(holder_class, 'loading');

	var url = source_xml;
	var myAjax = new Ajax.Request(url, { method: 'get', onComplete: preloadSlideshow }); 
}

function preloadSlideshow(originalRequest) {
	var slideshow = originalRequest.responseXML;
	var slides = slideshow.getElementsByTagName('slide');
	for (i=0; i<slides.length; i++) {
		aSlides[i] = new Object;
		// These first two are mandatory, the rest are optional
		aSlides[i].imgfile = slides[i].getElementsByTagName('image').item(0).firstChild.nodeValue;
		aSlides[i].alttext = slides[i].getElementsByTagName('alttext').item(0).firstChild.nodeValue;
		if (show_captions) aSlides[i].caption = slides[i].getElementsByTagName('caption').item(0).firstChild.nodeValue;
		if (show_links) aSlides[i].link = slides[i].getElementsByTagName('link').item(0).firstChild.nodeValue;
		if (show_more) aSlides[i].more = slides[i].getElementsByTagName('more').item(0).firstChild.nodeValue;
		aSlides[i].preloaded = false;
	}
	oPL = new ImagePreloader(aSlides[0].imgfile, 0, onPreload);
	//alert("end of preloadSlideshow");
}

function onPreload(index, oImage, preloaded) {
	//alert("onPreload( " + (index) + ", , " + preloaded);
	aSlides[index].src = oImage;
	aSlides[index].preloaded = preloaded;
	//alert("got here" + " " + preloaded + " " + index);
	
	if (index < (aSlides.length - 1)) 
		setTimeout("loadNext(" + (index+1) + ");", 50);  // delay the next load a bit to workaround IE stack overflow bug
	if (index == 0) 
		doSlideshow(0);  // if we just loaded the first slide, show it.

}

function loadNext(index) {
	if (!aSlides[index].preloaded) oPL = new ImagePreloader(aSlides[index].imgfile, index, onPreload);
}

function stopSlideshow() {
	if (tid) {clearTimeout(tid); tid = 0;}
	sc_pause = 1;
}

function startSlideshow() {
		sc_pause = 0;
		if (tid == 0) doSlideshow(sc_current);
}

function toggleSlideshow() {
	if (sc_pause==1) {
		document.getElementById("ss_toggle").src = "sc_slideshow/control_pause_1.gif";
		startSlideshow();
	} else {
		document.getElementById("ss_toggle").src = "sc_slideshow/control_play_1.gif";
		stopSlideshow();
	}
}

function ssMouseover() {
	if (sc_pause==1) {
		document.getElementById("ss_toggle").src = "sc_slideshow/control_play_0.gif";
	} else {
		document.getElementById("ss_toggle").src = "sc_slideshow/control_pause_0.gif";
	}
}
function ssMouseout() {
	if (sc_pause==1) {
		document.getElementById("ss_toggle").src = "sc_slideshow/control_play_1.gif";
	} else {
		document.getElementById("ss_toggle").src = "sc_slideshow/control_pause_1.gif";
	}
}

function doSlideshow(i) {
	var next=0;
	if (tid) {clearTimeout(tid); tid = 0;}
	if (i < 0) i = aSlides.length-1;
	i = i % aSlides.length;
	Element.removeClassName(holder_class, 'loading');
	//if (sc_pause) return;
	//alert("got here"+ i + " " + aSlides.length+ " " + aSlides[i].preloaded);
	if (i < aSlides.length) {
		if (aSlides[i].preloaded) {
			Element.hide('js_slideshow');
			//setTimeout('showSlide(' + i + ')', 10);
			
			if (i == aSlides.length - 1) {next = 0} else {next=i+1;}
			showSlide(i);
			if (sc_pause == 0) tid=setTimeout('doSlideshow(' + next + ')', delay);
		} else {
			setTimeout('doSlideshow(' + i + ')', 1000);
		}
	}
}

function showSlide(i) {
	sc_current = i;  // track the current slide
	var image_target = document.getElementById('slideshow_image');
	imageSrc = aSlides[i].src;  	
	image_target.src = imageSrc.src;
	image_target.alt = aSlides[i].alttext;
    if (show_links) {
		var image_wrapper_target = document.getElementById('slideshow_img_wrapper');
		image_wrapper_target.innerHTML = '<a href="' + aSlides[i].link + '">' + '<img src="' + image_target.src + '" alt="' + aSlides[i].alttext + '" border="0" id="slideshow_image" />' + '</a>';
	}

	if (show_captions) {
		var caption_target = document.getElementById('slideshow_caption');
		Element.update(caption_target, aSlides[i].caption);
		Element.show(caption_target);
		if (show_links) caption_target.innerHTML = '<a href="' + aSlides[i].link + '">' + aSlides[i].caption + '</a>';
	}
	
	if (show_more) {
		var more_target = document.getElementById('slideshow_more');
		Element.update(more_target, aSlides[i].more);
		Element.show(more_target);
		if (show_links) more_target.innerHTML = '<a href="' + aSlides[i].link + '">' + aSlides[i].more + '</a>';
	}
	
	Effect.Appear('js_slideshow');
}
