/*
 * Javascript for Republish
 * http://www.republish.eu/
 *
 */

$.fn.wpLazyLoad = function(){
	var posts = this.find('.post').length;

	$(window).scroll(function(){
		$.fn.wpLazyLoad.refresh();
	});
	
	return this.find('.post').each(function(i){
		/*var post = $(this);
		$(post).addClass('post-inactive').find('img, object').wrap('<div class="hidden media-wrapper"></div>');
		$(post).find('.media-wrapper').each(function(){
			if($(this).find('img').length > 0){
				var media = $(this).find('img');	
				$(media).data('src', $(media).attr('src')).removeAttr('src').load(function(){
					$(this).parent().fadeIn(250);
				});
			}else{
				$(post).removeClass('post-inactive');
			}	
		});
		$(post).find('.entry').removeClass('hidden');
		if((i+1) == posts){
			$.fn.wpLazyLoad.refresh();
		}*/
	});
};
$.fn.wpLazyLoad.refresh = function(){
	$('body #content').find('.post-inactive').each(function(i){
		var post = $(this);

		var fold = $(window).height() + $(window).scrollTop()-0;
		if(fold > $(post).offset().top){
			$(post).find('.media-wrapper').each(function(i){
				$(this).find('img').attr('src', $(this).find('img').data('src'));
			});
			$(post).removeClass('post-inactive');
		}
		
	});
	
	$('body #content').find('.post').each(function(i){
		var post = $(this);
			$(post).removeClass('active');
		if($(post).offset().top <= $(window).scrollTop() + $('#content').offset().top + 15){
			$(post).addClass('active');
		}
	});
};

$(function(){	
	$('body #content').wpLazyLoad();	
	$.keyboard.bind();
});