﻿var slideShowIntervalID = 0;

function slideSwitch(forward) {
    var $active = $('#slideshow IMG.active');


    if (forward) {
        if ($active.length == 0) $active = $('#slideshow IMG:last');
        var $next = $active.next().length ? $active.next()
        : $('#slideshow IMG:first');
    } else {
        if ($active.length == 0) $active = $('#slideshow IMG:first');
        var $next = $active.prev().length ? $active.prev()
        : $('#slideshow IMG:last');
    };

    $active.addClass('last-active');

    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function() {
            $active.removeClass('active last-active');
        });
}

function slideStart() {
    var $active = $('#slideshow IMG');
    if ($active.length > 1) {
        slideShowIntervalID = setInterval("slideSwitch(true)", 5000);
    }
}

function slideStartStop() {
    if (slideShowIntervalID == 0) {
        $("#stopstartslideshow").attr({ src: "/Content/images/stop.png", alt: "Stop slide show" });
        slideStart();
    } else {
        clearInterval(slideShowIntervalID);
        slideShowIntervalID = 0;
        $("#stopstartslideshow").attr({ src: "/Content/images/play.png", alt: "Start slide show" });
    };
}

function slidePrevious() {
    if (slideShowIntervalID > 0) {
        slideStartStop();
    }
    slideSwitch(false);
}

function slideNext() {
    if (slideShowIntervalID > 0) {
        slideStartStop();
    }
    slideSwitch(true);
}

$(document).ready(function() {
    //wire slide show related buttons
    $("#stopstartslideshow").click(function() { slideStartStop(); return false; });
    $("#prevslide").click(function() { slidePrevious(); return false; });
    $("#nextslide").click(function() { slideNext(); return false; });

    //start show
    slideStart();

});

