


(function($){
  $.fn.shuffle = function() {
    return this.each(function(){
      var items = $(this).children();
      return (items.length)
        ? $(this).html($.shuffle(items))
        : this;
    });
  }
 
  $.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;
  }
})(jQuery);






$.fn.fader = function() {
    
    return this.each(function() {
    
        var $container = jQuery(this);
        var $images = $container.find("img");
		$images = $.shuffle($images);


        var $first_image = $images.filter(":first");
        
        $images.filter(":not(:first)").hide();
        
        $container.height($first_image.height());
        $container.width($first_image.width());
        
        $images.css("position", "absolute");

        
        $images.filter(":not(:last)").each(function(index) {
            $(this).data("next", index + 1);
        });
        $images.filter(":last").data("next", 0);
        
        function fade_image() {
        
            var $current_image;
            var next_id;
            var $next_image;
            
            $current_image = $images.filter(":visible");
            next_id = $current_image.data("next");
            $next_image = $images.filter(":eq(" + next_id + ")");
            
            $current_image.fadeOut(1500);
            $next_image.fadeIn(1500);
        }
        
        window.setInterval(fade_image, 6000);
    });

}












$(document).ready(function() {
    $("div.fader").fader();
});




