// slideshow.js -- Run the slideshow

// Note that this script relies upon the jQuery library available from jquery.com
$(function() {
    if (typeof(show)!=undefined) { // Ensure we have a show to view
        if ($('#slideshow').length > 0) { // Make sure we have a place to watch it
            setupShow();
        }
    }
});

var slideshow = {};

function setupShow() {
    var h = show.height;
    var w = show.width;
    var v = show.images[0];
    var n = show.images[1];
    var setup='<img id="visible_slide" height="' + h + '" width="' + w + '" src="' + v + '" />';
    setup += '<img id="preload_slide" height="1" width="1" src="' + n + '" />';
    $('#slideshow').html(setup);
    slideshow.slide = 0;
    slideshow.next = 1;
    slideshow.playing = true;
    if (slideshow.next >= show.images.length) slideshow.next = 0;
    $('#visible_slide').attr('src',show.images[slideshow.slide][0]);
    $('#preload_slide').attr('src',show.images[slideshow.next][0]);
    updateColor();
    updateTitle();
    $('#prev').click(prev);
    $('#pause').click(pause);
    $('#next').click(next);
    slideshow.timer = setTimeout('fadeSlide()',6000);
}

function fadeSlide() {
    if (slideshow.playing) {
        $('#visible_slide').fadeOut(1000,swapSlides);
        if (show.colors) {
            $('#slideshow_stage').animate({backgroundColor: show.images[slideshow.next][1]},2000);
        }
    }
}

function swapSlides() {
    slideshow.slide++;
    if (slideshow.slide >= show.images.length) slideshow.slide = 0;
    slideshow.next++;
    if (slideshow.next >= show.images.length) slideshow.next = 0;
    $('#visible_slide').attr('src',show.images[slideshow.slide][0]);
    $('#preload_slide').attr('src',show.images[slideshow.next][0]);
    $('#visible_slide').fadeIn(1000,waitSlide);
    updateTitle();
}

function waitSlide() {
    if (slideshow.playing) slideshow.timer = setTimeout('fadeSlide()', 6000);
}

function updateTitle() {
    if (show.titles) {
        var title = show.images[slideshow.slide][0];
        title = title.slice(0,title.indexOf('.'));
        $('#slideshow_title').text(title);
    }    
}

function updateColor() {
    if (show.colors) {
        $('#slideshow_stage').animate({backgroundColor: show.images[slideshow.slide][1]},10);
    }
}

function resetTimer() {
    if (slideshow.playing) {
        clearTimeout(slideshow.timer);
        slideshow.timer = setTimeout('fadeSlide()',6000);
    }
}

function prev() {
    slideshow.slide--;
    if (slideshow.slide < 0) slideshow.slide = show.images.length - 1;
    slideshow.next--;
    if (slideshow.next < 0) slideshow.next = show.images.length - 1;
    $('#visible_slide').attr('src',show.images[slideshow.slide][0]);
    updateTitle();
    updateColor();
    resetTimer();
}

function next() {
    slideshow.slide++;
    if (slideshow.slide >= show.images.length) slideshow.slide = 0;
    slideshow.next++;
    if (slideshow.next >= show.images.length) slideshow.next = 0;
    $('#visible_slide').attr('src',show.images[slideshow.slide][0]);
    updateTitle();
    updateColor();
    resetTimer();
}

function pause() {
    if (slideshow.playing) {
        $('#playpause').attr('src','play.gif').attr('alt','&raquo;').attr('title','play');
        clearTimeout(slideshow.timer);
        slideshow.playing = false;
    } else {
        $('#playpause').attr('src','pause.gif').attr('alt','| |').attr('title','pause');
        slideshow.playing = true;
        fadeSlide();
    }
}
