$(document).ready(function(){
//	initMessageScreen();
	
	if ( $('body.home.normal #highlights').length ){
		initNewsScroller();
	}
	
	if ( $('body.home.normal #kolommen .kolom2').length ){
		initHeadlines();
	}
	
});

var ie6 = $.browser.msie && $.browser.version=='6.0';
var ie7 = $.browser.msie && $.browser.version=='7.0';

function initHeadlines(){
	var headlinesTop = $("#kolommen .kolom").offset().top-20; 
	
	$(window).scroll(function () {
        if ( $(window).scrollTop() > headlinesTop ) {
			if ( ie7 || ie6 || isTouchScreen ){
				$("#kolommen .kolom2").css('marginTop',$(window).scrollTop()-headlinesTop);
			} else{
				$("#kolommen .kolom2").addClass('stick-like-glue').css('marginTop',-headlinesTop);
			}
        }else{
			$("#kolommen .kolom2").removeClass('stick-like-glue').css('marginTop',0);
		}
    });
}

var messageScreen = null;
function initMessageScreen(){
	$('<div id="messageScreen" style="position:fixed; z-index:2000; left:0; top:0; width:400px; color:#000; background-color:#ffa; border:3px solid #ccc; padding:5px; height: 300px; overflow:auto;" />').prependTo('body');
	messageScreen = $("#messageScreen");
}

function message(msg){
	if ( $("#messageScreen").length ){
		if ( $("#messageScreen").html().length>0 ) $("#messageScreen").append('<br/>');
		$("#messageScreen").append(msg);
	}
}


var bottomBlock;
var pageBottom;
var spaceAtBottom;

function initNewsScroller(){
	bottomBlock = $("#highlights").delay(1000).offset().top+$("#highlights").height();
	pageBottom = $(document).height();
	spaceAtBottom = pageBottom - bottomBlock;

	message('space='+spaceAtBottom+'<br/>bottomBlock='+bottomBlock+'<br/>pageBottom='+pageBottom);

	//Fill the bottom of the window if there is empty space
	if (spaceAtBottom > 0) loadnextNewsPage(true);

	//If the window resizes, fill the bottom again if necessary
	$(window).resize(function () {
		bottomBlock = $("#highlights").delay(1000).offset().top+$("#highlights").height();
		pageBottom = $(document).height();
		spaceAtBottom = pageBottom - bottomBlock;
		//Fill the bottom of the window if there is empty space
		if (spaceAtBottom > 100) loadnextNewsPage(true);
	});
		
	//Create an infinite scroller
	$(window).scroll(function () {
        if ( ($(document).height() - $(window).height() - $(document).scrollTop() ) <= 210 ) {
			loadnextNewsPage(true);
        }
    });
}

var currentNewsPage = 1;
var loadingNews = false;
var allNewsFetched = false;

function loadnextNewsPage(fillBottom){
	if ( loadingNews || allNewsFetched ) return;
	
	loadingNews = true;
	
	//Show loading image
	$("#loading").show();
	
	var data = {
		action: 'getNewsPage',
		pageNr: currentNewsPage
	};
	$.post('/php/ajaxController.php', data, function(data){
		allNewsFetched = !data['more'];

		//remove the spacer div
		$("#highlights .spacer.bottom").remove();
		
		//Hide the loading image
		$("#loading").hide();
		
		$("#highlights").append(data['html']+'<div class="spacer bottom"></div>');
		currentNewsPage++;

		loadingNews = false;

		bottomBlock = $("#highlights").delay(1000).offset().top+$("#highlights").height();
		pageBottom = $(document).height();
		spaceAtBottom = pageBottom - bottomBlock;
	
		if ( fillBottom ){
			if (spaceAtBottom > 50) {
				loadnextNewsPage(true);
			}else{
				message(spaceAtBottom+'<=100');
			}
		}

	}, 'json');
	
}

