/*
 * Randomize plugin for jQuery
 * (C) LaMofeta 2010
 */ 
(function($) {
    
    $.randomize = {               
        /*
         * Initialize function
         */ 
        init: function(element, options) {
            $.randomize.instance = this;
            
            this.cache = [];
            this.timers = [];
            
            this.ul = $(element);
            
            this.options = options;
            
            this.elements = jQuery.makeArray(this.ul.find('li'));
            
            this.titles = options.titles;
            
            this.post_ids = options.sourceIds;
            
            this.copy_ids = this.shuffle(this.post_ids.slice());
            
            this.copy_elements = this.shuffle(this.elements.slice());
            
            this.randoms = [];
            
            if (options.randomItems > this.post_ids.length) {
                options.randomItems = this.post_ids.length;
            }

            for (var i = 0; i < options.randomItems; i++) {
                this.change_image();
            }
            
            this.timers.push(setInterval('$.randomize.instance.change_image()', this.options.interval));
        },
        
        change_image: function() {

            if (! this.copy_elements.length) {
                this.copy_elements = this.shuffle(this.elements.slice());
            }
            
            if (! this.copy_ids.length) {
                this.copy_ids = this.shuffle(this.post_ids.slice());
            }

            this.update_image(this.copy_elements.pop(), this.copy_ids.pop());
        },
        
        update_image: function(element, id) {
            var cache = this.cache;
            var titles = this.titles;

            if (cache[id] == undefined) {
                cache[id] = document.createElement('img');
                cache[id].src = this.options.urlDir + id + '/' + id + '.jpg';
            }
            
            $(element).find('img').fadeOut('slow', function() {
                $(this).parent().attr({'title': titles[id], 'href': id, 'target': '_blank'}).end().attr('src', cache[id].src).fadeIn('slow');
            });
            
        },
        
        shuffle: function(arr) {
            for(
              var j, x, i = arr.length; i;
              j = parseInt(Math.random() * i),
              x = arr[--i], arr[i] = arr[j], arr[j] = x
            );
            return arr;
        }
    };
    
    /*
     * Plugin declaration
     */ 
    $.fn.randomize = function(options) {
        return this.each(function() {
            var settings = jQuery.extend($.fn.randomize.defaults, options);
            var object = this;
            
            $.getJSON(settings.sourceIds, function(data){
              
              if (! jQuery.isEmptyObject(data)) {
                  var source_ids = [];
                  
                  for (id in data) {
                      source_ids.push(id);
                  }  
                    
                  settings.sourceIds = source_ids;
                  settings.titles = data;

                  $.randomize.init(object, settings);
              }
            });
                
            return this;
        });
    };
    
    /*
     * Default options
     */ 
    $.fn.randomize.defaults = {
        /*hideAnimation: $.sigcanvas.operations.rotate,
        showAnimation: */
        sourceIds: 'post/id_list',
        interval: 2500,
        randomItems: 10,
        urlDir: 'media/post/'
    };
})(jQuery);
