/*
	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;

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);
}

function onPreload(index, oImage, preloaded) {
	aSlides[index].src = oImage;
	aSlides[index].preloaded = preloaded;
	
	if (index == 0) {
		doSlideshow(0);
		oPL = new ImagePreloader(aSlides[index + 1].imgfile, index + 1, onPreload);
	} else if (index < (aSlides.length - 1)) {
		oPL = new ImagePreloader(aSlides[index + 1].imgfile, index + 1, onPreload);
	}	
}

function doSlideshow(i) {
	Element.removeClassName(holder_class, 'loading');
	if (i < aSlides.length) {
		if (aSlides[i].preloaded) {
			Element.hide('js_slideshow');
			setTimeout('showSlide(' + i + ')', 10);
			
			if (i == aSlides.length - 1) {i = 0} else {i++;}
			setTimeout('doSlideshow(' + i + ')', delay);
		} else {
			setTimeout('doSlideshow(' + i + ')', 1000);
		}
	}
}

function showSlide(i) {
	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 + '" TARGET="_blank">' + '<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 + '" TARGET="_blank">' + 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 + '" target="_blank">' + aSlides[i].more + '</a>';
	}
	
	Effect.Appear('js_slideshow');
	
}