// 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.fade = (options.fade)*1000;
        options.sleep = ((options.sleep)*1000)+options.fade;
        
       this.each(function() {
            obj = $(this);
            //Set the opacity of all list elements to 0
            obj.find('li').css({'opacity':0,'position':'absolute', 'list-style-type':'none','display':'block','top':0,'left':0});
            obj.find('li').children().css({'opacity':0});
            obj.find('li:first-child').css({'opacity':1}).addClass('show');
            obj.find('li:first-child').children().css({'opacity':1}).addClass('show');
            //Call the rotate function to run the slideshow
            setTimeout(function(){ rotate(); }, (options.sleep-options.fade));
        });
        
        function rotate(){
            

            //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-child'));

            //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}).addClass('show')
            .animate({'opacity':1}, options.fade);

            next.children().css({'opacity':0}).addClass('show')
            .animate({'opacity':1}, options.fade);
            //Hide the current image
            current.animate({'opacity':0}, options.fade, function(){
                (options.before)?options.after():'';
            }).removeClass('show');
            setTimeout(function(){ rotate(); }, options.sleep);


            //Call the rotate function to rotate the slide after a certain amount of time (options.delay)
            
        }
    };
})(jQuery);
function before(){
    alert('before');
}
function after(){
    alert('after');
}
