﻿(function ($) {

    $.fn.Slide = function (options) {

        // set default options
        var defaults = {
            speed: 1000,
            pause: 2000
        },

        // Take the options that the user selects, and merge them with defaults.
		options = $.extend(defaults, options);

        // Needed to fix a tiny bug. If the pause is less than speed, it'll cause a flickr.
        // This will check for that, and if it is smaller, it increases it to just about the options.speed.
        if (options.pause <= options.speed) options.pause = options.speed + 100;

        // for each item in the wrapped set
        return this.each(function () {

            var $this = $(this);

            $this.children().css({
                'width': $this.children().width(),
                'position': 'absolute',
                'left': 0
            });

            for (var i = $this.children().length, y = 0; i > 0; i--, y++) {
                $this.children().eq(y).css('zIndex', i + 99999);
            }

            setInterval(function () {
                $this.children('li:first').animate({ 'opacity': 0 }, options.speed, function () {

                    $this
					   .children('li:first')
					   .css('opacity', 1)
					   .css('zIndex', $this.children('li:last').css('zIndex') - 1)
					   .appendTo($this);
                       
                })

            }, options.pause);

        });

    }

})(jQuery);