$.fn.wait = function(time, type) {
    time = time || 1000;
    type = type || "fx";
    return this.queue(type, function() {
        var self = this;
        setTimeout(function() {
            $(self).dequeue();
        }, time);
    });
};

(function($){

  $.img_overlayer = {};
  $.img_overlayer.make = function() {

  
  	var $imgPile = $(".img_overlay > img").each(function make(i) {
  	  var $this = $(this);
      if($this.data('img_overlayer:init')) return; 
      
  		var pileWidth = $('.img_overlay').css('width').replace("px", "");
  		var pileHeight = $('.img_overlay').css('height').replace("px", "");
		
  		var picWidth = $this.attr('width');
      var picHeight = $this.attr('height');

  		var posX = ((pileWidth/2 - picWidth/2) + Math.random() * 0).toFixed();
  		var posY = ((pileHeight/2 - picHeight/2) + Math.random() * 0).toFixed();
      
      $this.css('left', posX + 'px');
      $this.css('top', posY + 'px');
      
  		$this.data('img_overlayer:init',true);
    });

    
  	$('.img_overlay img').click(function(){
		
  		var imgs = $(this).parent().find('img');
    
      if(imgs.filter(':visible').length > 1) {
        $(this).hide();
      } else {
        $(this).hide();
        
        imgs.wait(200).each(function(i) {
          $(this).delay(200*i).fadeIn('slow');
        });
      }  		
  	}); 
			
  };

  $.img_overlayer.make(); 
  
})(jQuery);
