﻿var lastIndex = 0;
var $banners;
var intervalId = 0;

function removeLastActive() {
    $('#slideshow A.last-active').each(function () {
        $(this).removeClass('last-active');
    });
}

function nextSlide() {
    var $active = $('#slideshow A.active');
    var nextIndex;
    var mylastIndex = lastIndex;

    if ($active.length == 0) {
        $active = $('#slideshow A:last');
        mylastIndex = $banners.length - 1;
    }

    nextIndex = mylastIndex + 1;

    if (nextIndex == $banners.length) {
        nextIndex = 0;
    }

    var $next = $($banners[nextIndex]);

    removeLastActive();
    $active.addClass('last-active');

    $next.css({ opacity: 0.0 })
			.addClass('active')
			.animate({ opacity: 1.0 }, 750, function () {
			    $active.removeClass('active');
			});

    lastIndex = nextIndex;
}

function prevSlide() {
    var $active = $('#slideshow A.active');
    var prevIndex;
    var myLastIndex = lastIndex;

    if ($active.length == 0) {
        $active = $('#slideshow A:first');
        myLastIndex = 0;
    }

    prevIndex = myLastIndex - 1;

    if (prevIndex == -1) {
        prevIndex = $banners.length - 1;
    }

    var $prev = $($banners[prevIndex]);
    removeLastActive();

    $active.addClass('last-active');

    $prev.css({ opacity: 0.0 })
			.addClass('active')
			.animate({ opacity: 1.0 }, 500, function () {
			    $active.removeClass('active last-active');
			});

    lastIndex = prevIndex;
}

$(function () {
    intervalId = setInterval('nextSlide()', 5000);
    $banners = $('#slideshow A');

    $('#slideshowNav').css("display", "none");
    nextSlide();
});

$(document).ready(function () {
    $("#slideshow").mouseover(function () {
        $('#slideshowNav').css("display", "block");
    });

    $("#slideshow").mouseout(function () {
        $('#slideshowNav').css("display", "none");
    });

    $("#rightArrow").click(function () {
        clearInterval(intervalId);
        nextSlide();
        intervalId = setInterval('nextSlide()', 5000);
    });

    $("#leftArrow").click(function () {
        clearInterval(intervalId);
        prevSlide();
        intervalId = setInterval('nextSlide()', 5000);
    });

    $("#pausePlay").click(function () {
        if (intervalId == 0) {
            intervalId = setInterval('nextSlide()', 5000);
            $('#pausePlay').removeClass("slidePlay");
            $('#pausePlay').addClass("slidePause");
            nextSlide();
        }
        else {
            clearInterval(intervalId);
            intervalId = 0;
            $('#pausePlay').removeClass("slidePause");
            $('#pausePlay').addClass("slidePlay");
        }
    });
});
