$(function() {
	// funciones
	function cambiar() {
		cuadro = Math.floor(Math.random()*$('.c').length);
		imgs = $('.c').eq(cuadro).children('.i').children();
		if(imgs.length>1){
			queue=imgs.filter('load:first');
			if(queue.length>0) {
				$(new Image())
					.load(function(){$(this).hide();queue.replaceWith($(this));prox();})
					.attr('src',queue.attr('src'));
			} else {
				prox();
			}
		} else { return false; }
	}
	function prox() {
		imgs = $('.c').eq(cuadro).children('.i').children('img');
		if(imgs.length>1) {
			coso=0;
			prom=0;
			imgs.each(function(i){if($(this).filter(':visible').length){coso=i;}});
			if(coso==imgs.length-1) { prom=0; } else { prom=coso+1; }
			imgs.eq(coso).fadeOut();
			imgs.eq(prom).fadeIn();
		}
	}
	// init
	setInterval(cambiar,1*1000);
	$('.masonry').masonry({columnWidth:220});
});
