﻿jQuery.fn.slideshow = function(options)
{
    var config = {
        activeTabClass: "slide-tab-active",
        fadeDuration: 500,
        interval: 5000,
        scrollThreshold: 3,
        slideOverlayQuery: ".slide-overlay",
        slideQuery: ".slide",
        slideTabQuery: ".slide-tab"
    };

    if (options)
    {
        jQuery.extend(config, options);
    }

    return this.each(function()
    {
        var currentIndex = 0;
        var slideshow = $(this);
        var slides = slideshow.find(config.slideQuery);
        var tabs = slideshow.find(config.slideTabQuery);
        var timer = null;

        var nextSlide = function()
        {
            showSlide((currentIndex + 1) % slides.length);
            timer = window.setTimeout(nextSlide, config.interval);
        }

        var showSlide = function(index, duration)
        {
            var currIndex = currentIndex;
            var nextIndex = typeof (index) == "undefined" ? currentIndex : index;

            if (typeof (duration) == "undefined")
            {
                duration = config.fadeDuration;
            }

            if (currIndex != nextIndex)
            {
                var currSlide = slides.eq(currIndex);
                var nextSlide = slides.eq(nextIndex);

                currSlide.css({ zIndex: 10 }).data("tab").removeClass(config.activeTabClass);
                nextSlide.data("tab").addClass(config.activeTabClass);
                nextSlide.css({ zIndex: 20 }).fadeTo(duration, 1, function()
                {
                    currSlide.fadeTo(0, 0);

                    if (jQuery.support.opacity == false)
                    {
                        nextSlide.get(0).style.removeAttribute("filter");
                    }
                });

                if (tabs.length > config.scrollThreshold)
                {
                    tabs.parent().animate({ top: -nextSlide.data("tab").outerHeight() + "px" }, duration, function()
                    {
                        tabs.parent().css({ top: "0px" }).append(currSlide.data("tab").detach());
                    });
                }

                currentIndex = nextIndex;
            }
        }

        var start = function()
        {
            showSlide(0, 0);
            timer = window.setTimeout(nextSlide, config.interval);
        }

        var stop = function()
        {
            window.clearInterval(timer);
        }

        slideshow.find(config.slideOverlayQuery).each(function(i)
        {
            var overlay = $(this);
            var overlayShadow = $("<div class='slide-overlay-shadow' />");

            overlayShadow.css({ height: overlay.outerHeight() + "px", width: overlay.outerWidth() + "px" });
            overlay.css({ background: "transparent", zIndex: "5" }).parent().append(overlayShadow);
        });

        slides.each(function(i)
        {
            $(this).css({ zIndex: (i == 0) ? 20 : 10 }).data("index", i).data("tab", tabs.eq(i)).fadeTo(0, (i == 0) ? 1 : 0);

            if (jQuery.support.opacity == false)
            {
                this.style.removeAttribute("filter");
            }
        });

        tabs.each(function(i)
        {
            var tab = $(this).data("index", i);

            if (i == 0)
            {
                tab.addClass(config.activeTabClass);
            }
        });

        tabs.click(function(e)
        {
            showSlide($(this).data("index"));
            stop();

            e.preventDefault();
        });

        start();
    });
}
