﻿//http://jonraasch.com/blog/a-simple-jquery-slideshow
// Transitions the images
function slideSwitch() {
    // Get ths active image
    var $active = $('#slideshow IMG.active');
    if ($active.length == 0) $active = $('#slideshow IMG:last');

    // Get the next image randomly
    var $siblings = $active.siblings();
    var randomNumber = Math.floor(Math.random() * $siblings.length);
    var $next = $($siblings[randomNumber]);

    // Get the next image by order
    //var $next = $active.next().length ? $active.next() : $('#slideshow IMG:first');


    // Set the currently active image to last so it is not chosen next
    $active.addClass('last-active');

    // set the next image class to active and animate it in
    // then remove the active class from the active image
    $next.css({ opacity: 0.0 })
                .addClass('active')
                .animate({ opacity: 1.0 }, 2000, function() {
                    $active.removeClass('active last-active');
                });
}

// slides interval id holder
var intervalId = 0;
var timeoutId = 0;

// Sets the interval for the transitions
function setSlideShowInterval() {
    intervalId = setInterval("slideSwitch()", 10000);
}

// clears the interval for the transition slides
function clearSlideShowInterval() {
    if (intervalId != 0) clearInterval(intervalId);
}

// clears the interval for the transition slides
function clearSlideShowTimeout() {
    if (timeoutId != 0) clearTimeout(timeoutId);
}

// function to start the slideshow from paused
function StartSlideShow() {
    timeoutId = setTimeout("slideSwitch();setSlideShowInterval()", 5000);
}

// function to pause the slideshow
function PauseSlideShow() {
    clearSlideShowTimeout();
    clearSlideShowInterval();
}

// Initiation
$(document).ready(function() {
    setSlideShowInterval();
});
