﻿var carousel = {
    slideItem: ".carousel .carouselItem",
    navItem: "#carouselNav .slide",
    playPause: "#playPause a",
    fadeSpeed: "fast",
    transitionTime: 5000,
    intervalHandle: null,
    currentIndex: 0,
    autoPlay: true,
    inAnimation: false,

    initialize: function (params) {
        if (params.transitionTime)
            carousel.transitionTime = params.transitionTime;
        if (params.currentIndex)
            carousel.currentIndex = params.currentIndex;

        $(carousel.navItem).click(function () {
            carousel.stopAutoPlay();
            var navIndex = $(carousel.navItem).index(this);
            carousel.changeSlide(navIndex);
            return false;
        });
        $(carousel.playPause).click(function () {
            if (carousel.autoPlay)
                carousel.stopAutoPlay();
            else
                carousel.startAutoPlay();
            return false;
        });

        if (carousel.autoPlay) {
            window.setTimeout(function () { carousel.startAutoPlay(); }, carousel.transitionTime);
        }

        $(".carousel").bind('swipeleft', function () {
            carousel.showNextSlide();
        });

        $(".carousel").bind('swiperight', function () {
            carousel.showPreviousSlide();
        });
    },
    startAutoPlay: function () {
        var imgPlayPause = $(carousel.playPause + " img");
        imgPlayPause.attr("src", imgPlayPause.attr("src").replace("Play", "Pause"));

        carousel.showNextSlide();
        carousel.autoPlay = true;
        carousel.intervalHandle = window.setInterval(function () { carousel.showNextSlide(); }, carousel.transitionTime);
    },
    stopAutoPlay: function () {
        var imgPlayPause = $(carousel.playPause + " img");
        imgPlayPause.attr("src", imgPlayPause.attr("src").replace("Pause", "Play"));

        carousel.autoPlay = false;
        window.clearInterval(carousel.intervalHandle);
    },
    showNextSlide: function () {
        var nextSlideIndex = (carousel.currentIndex < carousel.getLastIndex()) ? carousel.currentIndex + 1 : 0;
        carousel.changeSlide(nextSlideIndex);
    },
    showPreviousSlide: function () {
        var prevSlideIndex = (carousel.currentIndex > 0) ? carousel.currentIndex - 1 : carousel.getLastIndex();
        carousel.changeSlide(prevSlideIndex);
    },
    changeSlide: function (index) {
        if (carousel.inAnimation)
            return;

        carousel.inAnimation = true;

        carousel.currentIndex = index;
        carousel.updateNav(index);

        var currentSlide = $(carousel.slideItem + ":visible");
        var newSlide = $(carousel.slideItem).eq(index);

        currentSlide.hide(carousel.fadeSpeed, function () {
            newSlide.fadeIn(carousel.fadeSpeed);
            carousel.inAnimation = false;
        });
    },
    updateNav: function (index) {
        $(carousel.navItem + " img").each(function () {
            var src = $(this).attr("src");
            $(this).attr("src", src.replace("Selected", ""));
        });

        var selectedImg = $(carousel.navItem).eq(index).find("img");
        selectedImg.attr("src", selectedImg.attr("src").replace(".gif", "Selected.gif"));
    },
    getLastIndex: function () {
        return $(carousel.slideItem).length - 1;
    }
}

