﻿//Image rotator.
(function($) {
    $.fn.spotlights = function(options) {
        //Option defaults.
        var defaults = {
            contentUrl: '',
            contentArea: 'featureContent',
            previousButton: 'btnPrevious',
            nextButton: 'btnNext',
            startPaused: false,
            slideShowIntervalSeconds: 10,
            dependencyValue: ''
        };

        var options = $.extend(defaults, options);
        var dateTimeStamp = new Date();

        //Main body.
        return this.each(function() {
            obj = $(this);
            obj.css("overflow", "hidden");

            var templateItems = $("li", obj);
            var currentIndex = 0;
            var currentSlideIndex = 1;
            var contentArea = $("#" + options.contentArea);
            var slideShowTimeout = null;
            var isPaused = options.startPaused;

            if (templateItems.length > 0) {
                $("#" + options.previousButton).click(function() { movePrevious(); });
                $("#" + options.previousButton).dblclick(function() { movePrevious(); });
                $("#" + options.nextButton).click(function() { moveNext(); });
                $("#" + options.nextButton).dblclick(function() { moveNext(); });
            }

            wireItemEvents();

            if (!options.startPaused)
                resetTimer();

            //Move previous.
            function movePrevious() {
                var links = $("li a", obj);

                var trueIndex = currentSlideIndex - 1;
                var prevIndex = 0;

                if (currentSlideIndex == 0)
                    prevIndex = templateItems.length - 2;
                else {
                    if (trueIndex == 0)
                        prevIndex = templateItems.length - 1;
                    else
                        prevIndex = trueIndex - 1;
                }

                showSlide(links[prevIndex].id);

                currentSlideIndex = (prevIndex + 2 > templateItems.length) ? 0 : prevIndex += 1;

                resetTimer();
            }

            //Move next.
            function moveNext() {
                var links = $("li a", obj);
                showSlide(links[currentSlideIndex].id);
                currentSlideIndex = (currentSlideIndex + 2 > templateItems.length) ? 0 : currentSlideIndex += 1;
                resetTimer();
            };

            //Generate current items HTML from template.
            function generateHtmlFromTemplate() {
                var html = "";

                for (var i = currentIndex; i < templateItems.length; i++)
                    html += "<li>" + $(templateItems[i]).html() + "</li>";

                for (var i = 0; i < currentIndex; i++)
                    html += "<li>" + $(templateItems[i]).html() + "</li>";

                return "<ul>" + html + "</ul>";
            }

            //Wire item events.
            function wireItemEvents() {
                if (contentArea != null && contentArea.length > 0) {
                    var links = $("li a", obj);

                    links.click(function() {
                        isPaused = false;
                        for (var i = 0; i < links.length; i++)
                            if (this.id == links[i].id) {
                            currentSlideIndex = i;
                            break;
                        }
                        showSlide(this.id);
                        currentSlideIndex = (currentSlideIndex + 2 > templateItems.length) ? 0 : currentSlideIndex += 1;
                    });
                }
            }

            //Start timer.
            function resetTimer() {
                stopTimer();
                slideShowTimeout = setTimeout(playSlideShow, options.slideShowIntervalSeconds * 1000);
            }

            //Show slide.
            function showSlide(id) {
                stopTimer();
                $.get(
                    options.contentUrl,
                    { id: id, dateTimeStampVisit: dateTimeStamp, dependencyValue: options.dependencyValue },
                    function(data, textStatus) {
                        contentArea.html(data);

                        if (!isPaused)
                            resetTimer();

                    }, "html");
            }

            //Play slide show.
            function playSlideShow() {
                var links = $("li a", obj);
                if (links.length > currentSlideIndex)
                    showSlide(links[currentSlideIndex].id);

                currentSlideIndex = (currentSlideIndex + 2 > templateItems.length) ? 0 : currentSlideIndex += 1;
                resetTimer();
            }

            //Stop timer.
            function stopTimer() {
                if (slideShowTimeout != null)
                    clearTimeout(slideShowTimeout);
            }
        });
    };
})(jQuery);
