// jQuery Rotator
// Author: Sam Walsh - Webdirectionz / Tomahawk
// Date: 02/2010
(function($){
    $.fn.rotator = function(options) {
        var defaults = {
            sleep: 3,
            fade: 1
        }


        var options = $.extend(defaults, options);
        options.sleep = (options.sleep)*1000;
        options.fade = (options.fade)*1000;
        obj = $(this);
        this.each(function() {
            //Set the opacity of all list elements to 0
            obj.find('li').css({'opacity': 0.0, 'position':'absolute', 'list-style-type':'none'});
            obj.find('li:first').css({'display':'block'});
            //Call the rotate function to run the slideshow
            rotate();
        });
        function rotate(){
            //Call the rotate function to rotate the slide after a certain amount of time (options.delay)
            setTimeout(function(){ rotate(); }, options.sleep);

            //If you have set a function to load before it rotates, then run that function now
            (options.before)?options.before():'';

            //Get the first image
            var current = (obj.find('li.show')? obj.find('li.show') : obj.find('li:first'));

            //Get next image, when it reaches the end, rotate it back to the first image
            var next = ((current.next().length) ? ((current.next().hasClass('show')) ? obj.find('li:first') : current.next()) : obj.find('li:first'));

            //Set the fade in effect for the next image, the show class has higher z-index
            next.css({opacity: 0.0})
            .addClass('show')
            .css({'display':'block'})
            .animate({opacity: 1.0}, options.fade,function(){
                (options.after)?options.after():'';
            });

            //Hide the current image
            current.animate({opacity: 0.0}, options.fade).removeClass('show');

        }

    };
})(jQuery);

