﻿(function($) {
    $.fn.slideshow = function(method, options) {

        var settings = {
            fadeTime: 500,
            navigationSelector: '.navigation > li',
            slidesSelector: '.slides > li',
            startIndex: 0,
            swapTime: 5000
        };

        var methods = {

            // gets the state data for the slideshow
            data: function() {
                var data = $(this).data('slideshow');

                if (typeof (data) == 'undefined') {
                    $(this).data('slideshow', data = {
                        slideArray: [],
                        slideIndex: Math.max(settings.startIndex, 0),
                        timer: null
                    });
                }

                return data;
            },

            // initializes the slideshow
            init: function(options) {
                var inst = this;

                if (options) {
                    $.extend(settings, options);
                }

                var button = this.find(settings.navigationSelector);
                var buttonNext = $('<li><a class="next">Next Slide</a></li>').click(function() { methods.next.apply(inst, [true]); return false; });
                var buttonPrev = $('<li><a class="back">Previous Slide</a></li>').click(function() { methods.previous.apply(inst, [true]); return false; });
                var data = methods.data.apply(this);

                this.find(settings.slidesSelector).each(function(i) {
                    data.slideArray.push($(this).css({ zIndex: (i == settings.startIndex ? 2 : 1) }).fadeTo(0, i == settings.startIndex ? 1 : 0));
                });

                button.first().before(buttonPrev);
                button.last().after(buttonNext);
                button.each(function(i) {
                    var slide = data.slideArray[i];
                    var slideButton = $(this);

                    slide.bind('slide-activated', function() { slideButton.addClass('active').siblings('li').removeClass('active'); });
                    slideButton.data('slideshow-slide-index', i);
                }).live('click', function() { methods.show.apply(inst, [$(this).data('slideshow-slide-index'), true]); return false; });

                return methods.start.apply(this);
            },

            // shows the next slide in the slideshow
            next: function(stop) {
                return methods.show.apply(this, [methods.data.apply(this).slideIndex + 1, stop]);
            },

            // starts a timer for the next slide
            nextTimer: function() {
                var inst = this;
                var data = methods.data.apply(this);
                data.timer = window.setTimeout(function() { methods.next.apply(inst); }, settings.fadeTime + settings.swapTime);
            },

            // shows the previous slide in the slideshow
            previous: function(stop) {
                return methods.show.apply(this, [methods.data.apply(this).slideIndex - 1, stop]);
            },

            // displays the slide with the specified index and optionally stops the slideshow
            show: function(index, stop) {
                var data = methods.data.apply(this);

                if (index < 0) {
                    index = data.slideArray.length - (Math.abs(index) % data.slideArray.length);
                }

                var index1 = data.slideIndex, slide1 = data.slideArray[index1].css({ zIndex: 1 });
                var index2 = data.slideArray.length > 0 ? index % data.slideArray.length : 0, slide2 = data.slideArray[index2].css({ zIndex: 2 });

                if (index1 != index2) {
                    slide2.fadeTo(settings.fadeTime, 1.0, function() { slide1.fadeTo(0, 0.0); });
                }

                slide2.trigger('slide-activated');

                data.slideIndex = index2;

                if (stop) {
                    methods.stop.apply(this);
                } else {
                    methods.nextTimer.apply(this);
                }
            },

            // starts the slideshow
            start: function() {
                var data = methods.data.apply(this);

                if (data.slideArray.length > 0) {
                    methods.stop.apply(this);
                    methods.show.apply(this, [settings.startIndex, false]);
                }
                
                return this;
            },

            // stops the slideshow
            stop: function() {
                var data = methods.data.apply(this);

                if (typeof (data.timer) != 'undefined') {
                    window.clearTimeout(data.timer);
                }

                data.timer = null;

                return this;
            }
        };

        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        }
        else if (typeof (method) === 'object' || !method) {
            return methods.init.apply(this, arguments);
        }
        else {
            $.error('Method ' + method + ' does not exist on jQuery.slideshow');
        }
        /*
        this.find(settings.navigationSelector).each(function(i) {

            if (slideshow != null) {
        slideshow.highlightSelector();
        slideshowButtons.each(function(i) { $(this).data("slide_index", i); }).click(function(e) {
        var active = $(this);
        var activeIndex = active.data("slide_index");

                    slideshow.showSlide(activeIndex, true);
        slideshowButtons.not(active).removeClass("selected");

                    active.addClass("selected");
        });

                $("#Feature .featureSelector").each(function(i) {
        $(this).find(".backButton").click(function(e) {
        slideshow.nextSlide();
        slideshow.stop();
        });

                    $(this).find(".forwardButton").click(function(e) {
        slideshow.nextSlide();
        slideshow.stop();
        });
        });
        }
        });
        */
    }
})(jQuery);

