/* slideshow.js - Originally Created by Ruddick "Vinny" Lawrence
 * -------------------------------------------------------------
 * Picture slideshow. Preloads images. Requires Prototype.
 * Capitalized properties control the show. ID is the id of the
 * img tag showing the pictures. TIME is the amount of time a
 * a picture is displayed for, in seconds. FOLDER_URL is the url to
 * the folder holding the images (include trailing slash).
 * IMAGE_FILES is an array of the images to be shown on the
 * slideshow. Requires Prototype, Scriptaculous, and utils.js.
 */

var slideshow = {
    init: function() {
        var ID = "slideshow";
	var TIME = 3;
	var FOLDER_URL = site.MEDIA_URL + "pictures/slideshow/";
	var IMAGE_FILES = slideshow_images.filenames;
	utils.array_shuffle(IMAGE_FILES);

	slideshow.elem = $(ID);
	if(slideshow.elem == null) return;
	slideshow.time = TIME*1000;
	slideshow.folder = FOLDER_URL;
	slideshow.imgs = IMAGE_FILES
	slideshow.currImage = 0;
        slideshow.image = new Image();
	slideshow.image.src = slideshow.folder + slideshow.imgs[slideshow.currImage];

	setTimeout(slideshow.imageTransition, slideshow.time);
    },

    imageTransition: function() {
	Effect.Fade(slideshow.elem, {afterFinish: slideshow.nextImage});
    },

    nextImage: function() {
	if(++slideshow.currImage >= slideshow.imgs.length) slideshow.currImage = 0;
	slideshow.elem.src = slideshow.image.src;
        slideshow.elem.width = slideshow.image.width
	Effect.Appear(slideshow.elem, {afterFinish: function(){setTimeout(slideshow.imageTransition, slideshow.time);}});

	slideshow.image = new Image();
	slideshow.image.src = slideshow.folder + slideshow.imgs[slideshow.currImage];
    }
};

Event.observe(window, 'load', slideshow.init);
