/**
 * Created by JetBrains PhpStorm.
 * User: barba
 * Date: 25.12.10
 * Time: 21:38
 * To change this template use File | Settings | File Templates.
 */

$(function(){
  $('.thumbs').height(Math.floor($('.thumbs').width() / 90) * 90);

  $(window).resize(function(){
    $('.thumbs').height(Math.floor($('.thumbs').width() / 90) * 90);
  })

  $('.renew input').click(function(){
    var processed = [];
    var quantity = $('div.thumbs').height() / 90;
    
    quantity *= quantity;
    
    while (processed.length < quantity) {
      var item = Math.floor(Math.random() * (quantity));
      if ($.inArray(item, processed) == -1)
        processed.push(item);

    }

    var thumbs = $('div.thumbs a');

    $.post('thumbs_renew.php',{quantity: quantity},function(data){

      for (var i = 0; i < quantity; i++) {

        thumbs.eq(processed[i]).children().delay(i * 20).fadeOut(function(){
          var index = $(this).parent().prevAll().length;
          $(this).attr('src','/img/thumbs/' + data[index]).fadeIn();
          $(this).parent().attr('href','/img/full/' + data[index]);
        });
      }
    }, 'json');
    


    return false;
  });

  $("div.thumbs a").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
    overlayColor: '#000'
	});
});

