 $(document).ready(init_project_index);

var thumbnails ;
var thumbnails_info;
var total_images = 0;
var total_loaded = 0;
var total_fadein = 0;
var info_boxes = new Array(); 
function init_project_index(){
  var images = $('#content .thumbnail_index img');
  total_images = images.length;
  // images.bind('load', thumb_image_loaded );
  
  thumbnails = $('#content .thumbnail_index');
  thumbnails_info = $('#content .thumbnail_index .info_box');

  /*
  if(document.all){
    thumbnails.each(function(){
      // remova all info box
       info_boxes.push( $(this).children('.info_box').clone());
      $(this).children('.info_box').remove();
      
    });
  }
  */
  
  //thumbnails_info.bind('mouseover', function(){ $(this).animate({opacity:1},{duration:500,queue:false}); });
  //thumbnails_info.bind('mouseout', function(){ $(this).animate({opacity:0},{duration:1000,queue:false});  });
  
  
  /* dispatch animation if loading too long */
 // setTimeout(function(){ if(total_loaded < total_images ){show_thumbnails();} } , 2500); 

  show_thumbnails();
}

function thumb_image_loaded(){
  total_loaded++;
  if(total_loaded == total_images){
    show_thumbnails();  
  }
}


function show_thumbnails(){
  
  var columns = 5;
  var delay = 100;

  //return;

  thumbnails.css('opacity',0);
  
  /* Fade in randomly */
  var sort = new Array();
  for(var i = 0; i< thumbnails.length; i++)sort.push(i);  
  shuffle(sort);
  for(var i = 0; i< sort.length; i++){
    delay_show($(thumbnails[sort[i]]), (i * delay));  
  }
  return;


  /* Fade in from top left to bottom right */
  var rows = Math.ceil(thumbnails.length / columns) ;
  var idx = 0;
  var offset = 0;
  for(var i = 0; i< rows; i++){
    for(var j = 0 ; j < columns && idx < thumbnails.length ; j++){
      delay_show($(thumbnails[idx]), (offset + delay*j) );
      idx++;      
    }
    offset += delay;
  }

}

function delay_show(obj, delay){
 
 setTimeout( function(){ obj.animate({opacity: 1},1000, "linear" , fadein_end ) } , delay ); 

}

function fadein_end(){
  return; 
  total_fadein++;
  if(total_fadein == total_images){
    if(document.all){
      z = 0;
      thumbnails.each(function(){
        $(this).append(info_boxes[z]);
        z++;
      });
          
        
    }    
  }
}

shuffle = function(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};