(function($){
    
    $.rotator = {
        
        container:      null,
        currentAction:  'play',
        currentIndex:   0,
        items:          [],
        timer:          null,
        delay:          10000,
        
        populate:   function( items ){
            
            $.rotator.items = [];
            $(items).each(function(i,item){
                
                if ( typeof item == 'string' ) {
                    $.rotator.items[ i ] = {
                        index:  i,
                        src:    item,
                        loaded: false,
                        img:    false,
                        div:    false
                    };
                    
                    $.rotator.preload( $.rotator.items[i].index );
                    
                    $.rotator.items[ i ].div =
                        $('<div />').css({
                            'position': 'absolute',
                            'top': 0,
                            'left': 0,
                            'display': 'none',
                            'background-image': "url("+$.rotator.items[i].src+")"
                        })
                    
                    $.rotator.container.append( $.rotator.items[ i ].div );
                }
            });
            
            return true;
        },
        preload: function( index ){
            
            //if ( index > $.rotator.items.length ) return false;
            //if ( $.rotator.items[index] == 'undefined' ) return $.rotator.preload( index+1 );
            
            var image = 
                $('<img />')
                    .bind('load',function(){
                        $.rotator.items[index].loaded = true;
                    })
                    .attr('src', $.rotator.items[index].src );
            
            return image;
        },
        rotate: function( new_index ){
            
            var current_index = $.rotator.currentIndex;
            if ( new_index == undefined ) new_index = 0;
            if ( new_index >= this.items.length ) new_index = 0;
            $.rotator.currentIndex = new_index;
            
            var fadeDelay = $.rotator.container.find('div:not(:hidden)').length == 0 ? 0 : 1500;
            
            $.rotator.items[current_index].div.fadeOut(fadeDelay);
            $.rotator.items[new_index].div.fadeIn(fadeDelay);
            
            if ( $.rotator.currentAction == 'play' ) {
                
                window.clearTimeout( this.timer );
                
                this.timer = window.setTimeout( 
                    (function() { $.rotator.rotate(new_index+1); }), this.delay
                );
            }
        }
    }
    
    $.fn.rotator = function(images,delay){
        if(this.length==0) return this; // quick fail
        
        $.rotator.delay = delay;
        $.rotator.container = this;
        
        $.rotator.populate( images );
        $.rotator.rotate();
        
        return this;
    };
})(jQuery);
