var offset = 10, limit = 10;

hljs.initHighlightingOnLoad();

$(document).ready(function(){
	var $loadPosts = $('#load-next-posts');
	
	$loadPosts.click(function(e){
		e.preventDefault();
		
		var original_text = $(this).text();
		$(this).text('loading...');
		
		$.ajax({
			type:'GET',
			url: '/posts/next',
			cache: false,
			data: { offset:offset, limit:limit },
			dataType: 'json',
			success: function(msg){
				$loadPosts.text(original_text);
				
				if (msg.err){
					//error! :(
				}
				else{
					for (var i in msg.posts){
						$('#posts').append(msg.posts[i]).fadeIn((i + 1) * 450);
					}
					
					if (msg.more_pages != true){
						$loadPosts.hide();
					}
					
					offset += limit;
				}
			}
		});
	});
});
