/*
	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_caption = false;
var show_more = false;
var delay = 8000;
var holder_class;
var div_index = 1;
var slideshow_target;
var slideshowDIV_1;
var slideshowDIV_2;

function initElements() {
	slideshow_target = document.getElementById('js_slideshow');
	
	slideshowDIV_1 = document.createElement("DIV");
	slideshowDIV_1.style.position = "absolute";
	slideshowDIV_1.style.left = "0px";
	slideshowDIV_1.style.top = "0px";
	slideshowDIV_1.id = "slideshowDIV_1";
	
	slideshowDIV_2 = document.createElement("DIV");
	slideshowDIV_2.style.position = "absolute";
	slideshowDIV_2.style.left = "0px";
	slideshowDIV_2.style.top = "0px";
	slideshowDIV_2.id = "slideshowDIV_2";
	
	slideshow_target.appendChild(slideshowDIV_1);
	slideshow_target.appendChild(slideshowDIV_2);

}

function initSlideshow(source_xml, slideshow_wrapper, slideshow_styles, arg_delay, arg_show_links, arg_show_caption, arg_show_more) {
	
	if ( typeof ( slideshow_wrapper ) == "undefined" ) {
		holder_class = "slideshow_container";
	} else {
		holder_class = slideshow_wrapper;
	}
	
	if ( typeof ( arg_delay ) == "undefined" ) {
		delay = 8000;
	} else {
		delay = arg_delay * 1000;
	}
	
	if ( typeof ( arg_show_links ) == "undefined" ) {
		show_links = false;
	} else if (arg_show_links == true) {
		show_links = true;
	}
	
	if ( typeof ( arg_show_caption ) == "undefined" ) {
		show_caption = false;
	} else if (arg_show_caption != '') {
		show_caption = arg_show_caption;
	}
	
	if ( typeof ( arg_show_more ) == "undefined" ) {
		show_more = false;
	} else if (arg_show_more != '') {
		show_more = arg_show_more;
	}
	
/* 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_links) aSlides[i].link = slides[i].getElementsByTagName('link').item(0).firstChild.nodeValue;
		if (show_caption != false) aSlides[i].caption = slides[i].getElementsByTagName('caption').item(0).firstChild.nodeValue;
		if (show_more != false) aSlides[i].more = slides[i].getElementsByTagName('more').item(0).firstChild.nodeValue;
		aSlides[i].preloaded = false;
		
		oImage = new Image;
		oImage.index = i;
		oImage.onload = function()
		{
			aSlides[this.index].src = this.src;
			aSlides[this.index].preloaded = true;
			//if (this.index == 0) setTimeout('doSlideshow(0)', 10);
		}
		oImage.src = aSlides[i].imgfile;
		
	}
	setTimeout('doSlideshow(0)', 10);
	// oPL = new ImagePreloader(aImages.shift(), 0, onPreload);
}

function doSlideshow(i) {
	Element.removeClassName(holder_class, 'loading');
	if (i < aSlides.length) {
		//if (aSlides[i].preloaded) {
			// alert(i);
			
			// 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) {
	
	// toggle the appear and fade DIVs
	if (div_index == 1) {
		var fadeDIV = slideshowDIV_2;
		var appearDIV = slideshowDIV_1;
		div_index = 2;
	} else {
		var fadeDIV = slideshowDIV_1;
		var appearDIV = slideshowDIV_2;
		div_index = 1;
	}
	
	// clear out the previous contents
	while (appearDIV.hasChildNodes()) {
		appearDIV.removeChild(appearDIV.lastChild);
	}
	
	// hide the DIV so it doesn't appear while being constructed
	Element.hide(appearDIV.id);
	
	// Set up the image
	var imageDIV = document.createElement("DIV");
	var imageID = "slideshow_image";
	imageSrc = aSlides[i].src;  	
	if (show_links) {
		imageDIV.innerHTML = '<a href="' + aSlides[i].link + '">' + '<img src="' + aSlides[i].imgfile + '" alt="' + aSlides[i].alttext + '" border="0" id="' + imageID + '" />' + '</a>';
	}
	appearDIV.appendChild(imageDIV);
		
	// Set up the caption
	if (show_caption != false) {
		var captionDIV = document.createElement("H3");
		if (show_links) {
			captionDIV.innerHTML = '<a href="' + aSlides[i].link + '">' + aSlides[i].caption + '</a>';
		} else {
			captionDIV.innerHTML = aSlides[i].caption;
		}
		captionDIV.setAttribute("class", show_caption);
		captionDIV.className = show_caption;
		appearDIV.appendChild(captionDIV);
	}
	
	// Set up the supplemental text
	if (show_more != false) {
		var moreDIV = document.createElement("p");
		if (show_links) {
			moreDIV.innerHTML = '<a href="' + aSlides[i].link + '">' + aSlides[i].more + '</a>';
		} else {
			moreDIV.innerHTML = aSlides[i].more;
		}
		moreDIV.setAttribute("class", show_more);
		moreDIV.className = show_more;
		appearDIV.appendChild(moreDIV);
	}
	
	// Set the styles for the slideshow object
	if ( typeof ( slideshow_styles ) != "undefined" ) {
		for (key in slideshow_styles) {
			appearDIV.style[ key ] = slideshow_styles[ key ];
		}
	}
	
	Effect.Fade(fadeDIV.id, { duration:1, from:1.0, to:0.0 });
	Effect.Appear(appearDIV.id, { duration:1, from:0.0, to:1.0 });

}
