
(function($){
    
    $.rotator = {
        
        currentAction:  'play',
        currentIndex:   0,
        items:          [],
        timer:          null,
        delay:          10000,
        
        populate:   function( items ){
            
            $.rotator.items = [];
            $(items).each(function(i,item){
                
                if ( typeof item == 'object' ) {
                    $.rotator.items[ i ] = {
                        index:  i,
                        src:    item.src,
                        url:    item.url,
                        loaded: false,
                        img:    false
                    };
                    
                    $.rotator.items[ i ].img = $.rotator.preload( $.rotator.items[i].index );
                    
                    $.rotator.items[ i ].btn = $('<span />').text( $.rotator.items[i].index+1 ).click(function(){
                        $.rotator.rotate( $.rotator.items[i].index );
                    });
                    $('#rotator .numbers').append( $.rotator.items[ i ].btn );
                }
            });
            
            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 );
            
            if ( $.rotator.items[index].url != '' ) {
                image.bind('click',function(){
                    location.href = $.rotator.items[index].url;
                });
            }
            
            return image;
        },
        rotate:     function( new_index ){
            
            if ( new_index == undefined ) new_index = this.currentIndex+1;
            if ( new_index >= this.items.length ) new_index = 0;
            $.rotator.currentIndex = new_index;
            
            var fadeDelay = $('#rotator img').length == 0 ? 0 : 1500;
            
            $('#rotator img').fadeOut(fadeDelay,function(){ $(this).remove() });
            $('#rotator').append( $.rotator.items[new_index].img.hide() ).children('img').fadeIn(fadeDelay);
            
            $('#rotator .numbers span').removeClass('active');
            $.rotator.items[new_index].btn.addClass('active');
            
            
            if ( $.rotator.currentAction == 'play' ) {
                
                window.clearTimeout( this.timer );
                
                this.timer = window.setTimeout( 
                    (
                        function() { $.rotator.rotate(new_index+1); }
                    )
                , this.delay);
            }
        },
        start:      function(){
            
            if ( this.currentAction == 'play' ) return true;
            
            this.currentAction = 'play';
            this.rotate();
        },
        stop:       function(){
            
            window.clearTimeout( this.timer );
            this.timer = null;
            this.currentAction = 'stop';
        }
    }
    
    $.fn.rotator = function(settings){
        if(this.length==0) return this; // quick fail
        
        $('#rotator .controls').append(
            $('<span />').addClass('pause').click(function(){
                $(this).siblings('.active').removeClass('active');
                $(this).addClass('active');
                $.rotator.stop();
            })
        ).append(
            $('<span />').addClass('play').addClass('active').click(function(){
                $(this).siblings('.active').removeClass('active');
                $(this).addClass('active');
                $.rotator.start();
            })
        );
        
        $.rotator.populate( settings.images );
        $.rotator.rotate();
        
        return this;
    };
})(jQuery);