﻿/*** 
Simple jQuery Slideshow Script
Released by Jon Raasch (jonraasch.com) under FreeBSD license: free to use or modify, not responsible for anything, etc.  Please link out to me if you like it :)
***/
var interval;

function startSlideshow() {
    interval = setInterval("slideSwitch()", 4000);
}

function stopSlideshow() {
    clearInterval(interval);
    interval = "";
}

function slideSwitch() {
    var $active = $('#slideshow A.active');
    var $activeImage = $('#slideshowControls IMG.activeImage');

    if ($active.length == 0) $active = $('#slideshow A:last');
    if ($activeImage.length == 0) $activeImage = $('#slideshowControls IMG:last');

    // use this to pull the images in the order they appear in the markup
    var $next = $active.next().length ? $active.next() : $('#slideshow A:first');
    var $nextImage = $activeImage.next().length ? $activeImage.next() : $('#slideshowControls IMG:first');

    $active.addClass('last-active');

    $next.css({ opacity: 0.0 })
            .addClass('active')
            .animate({ opacity: 1.0 }, 500, function() {
                $active.removeClass('active last-active');
            });

    $nextImage.addClass('activeImage');
    $nextImage.attr("src", "/images/videos/red_block.gif");

    $activeImage.removeClass('activeImage');
    $activeImage.attr("src", "/images/videos/gray_block.gif");
}

function setActiveImage(activeIndex) {
    if (interval != "") {
        stopSlideshow();
    }

    var $active = $('#slideshow A.active');
    var $activeImage = $('#slideshowControls IMG.activeImage');

    var $next = $("#slideshow A:nth-child(" + activeIndex + ")");
    var $nextImage = $("#slideshowControls IMG:nth-child(" + activeIndex + ")");

    if ($active.attr('id') != $next.attr('id')) {
        $active.addClass('last-active');

        $next.css({ opacity: 0.0 })
                .addClass('active')
                .animate({ opacity: 1.0 }, 500, function() {
                    $active.removeClass('active last-active');
                });
    }

    if ($activeImage.attr('id') != $nextImage.attr('id')) {
        $nextImage.addClass('activeImage');
        $nextImage.attr("src", "/images/videos/red_block.gif");

        $activeImage.removeClass('activeImage');
        $activeImage.attr("src", "/images/videos/gray_block.gif");
    }
}