jQuery(function($){
	/*
	 * Masonry
	 */

	$blocks = $('#main');

	$blocks.imagesLoaded(function(){
		$blocks.masonry({
			itemSelector: '.grid-item',
			columnWidth: '.grid-item',
			stamp: '.stamp',
			// slow transitions
			transitionDuration: '1s'
		});

		// Fade blocks in after images are ready (prevents jumping and re-rendering)
		$('.grid-item').fadeIn();
		$blocks.find( '.grid-item' ).animate( {
			'opacity' : 1
		} );

	});

	$(document).ready( function() { setTimeout( function() { $blocks.masonry(); }, 2000); });

	$(window).on( 'resize', function () {
		$blocks.masonry();
	});

	// When Jetpack Infinite scroll posts have loaded
	$( document.body ).on( 'post-load', function () {
		var $container = $('#main');
		$container.masonry( 'reloadItems' );

		$blocks.imagesLoaded(function(){
			$blocks.masonry({
				itemSelector: '.grid-item',
				columnWidth: '.grid-item',
				stamp: '.stamp',
				// slow transitions
				transitionDuration: '1s'
			});

			// Fade blocks in after images are ready (prevents jumping and re-rendering)
			$('.grid-item').fadeIn();
			$blocks.find( '.grid-item' ).animate( {
				'opacity' : 1
			} );

		});
		
		$(document).ready( function() { setTimeout( function() { $blocks.masonry(); }, 2000); });
	});
});
