var lastBlogId = false;
var nextWaiting = false;
var loading = false;

$(document).ready(function () {
	$("div#additional").html('<img src="design/loading.gif" />');
});

function addNewEntry() {
	var thisId;
	$.get('/prevblogentry.php?nummer='+lastBlogId, 
		function(data) {
			if (data != '') {
				finding = data.search(/\n/);
				thisId = data.substr(0, finding-1);
				content = '<div id="blog'+thisId+'">'+data.substr(finding+1)+'</div>';
				if (!document.getElementById('blog'+thisId)) {
					$('div#blogfield').append(content);
					lastBlogId = thisId;
				}
			} else {
				lastBlogId = false;
				$("div#additional").html('');
			}
		}
	);
}

$(window).scroll(function(){
	if (!loading) {
		loading = true;
		if (lastBlogId) {
			if  ($(window).scrollTop() > $(document).height() - $(window).height() - 100) {
				addNewEntry();
			}
		}
		loading = false;
	}
});
