/**
 * @author rpl
 */

$(document).ready(function(){
	
	once();
	
});

function once() {
	
	$(window).bind('scroll', scrollLoad);
	
	function scrollLoad(){
		
		
		 if( ( $(window).scrollTop() + $(window).height() )  > ($(document).height()-300) ){
			//console.log( 'WE ARE SCROLLING: $(window).scrollTop() = '+$(window).scrollTop() +' window.height='+ $(window).height() + ' document.height = '+ $(document).height() );
			$(window).unbind('scroll', scrollLoad);
			newContentPlease();
		} 
	}
	
	function newContentPlease() {
		
		var amountItems = $('div.noteren').length; 
		// console.log(amountItems + ' items');
		// de current amount of thumbs wordt meegegeven aan de ajax-call, zodat de volgende rits (oudere) items met de juiste offset kan worden opgevraagd. Dus als er al 120 thumbs staan, dan vraagt ie de volgende batch uit met een offset van 120 thumbs.
		
		 $.ajax({
				type: "POST",
				url: "getOlderContent.php",
				data: 	"offset=" + amountItems,
				success: function(html){
					var htmlparsed = unescape(decodeURI(html));
					$("#allContent").append(htmlparsed);
					//$("#thumbnails").fadeIn("normal");
					//$("#pagenr").attr("value",page);
					
					$(window).bind('scroll', scrollLoad);
					
					//init();
					
				}
		});
	}
	
	setTimeout(getFreshContent,40 * 1000);
	
	function getFreshContent() {
		
		 $.ajax({
				type: "POST",
				url: "getFreshContent.php",
				data: 	"offset=",
				success: function(html){
					var htmlparsed = unescape(decodeURI(html));
					$("#allContent").prepend(htmlparsed);	
					setTimeout(getFreshContent,40 * 1000);
					
					//init();
					
				}
		});
		
	}

		$("#butt_day0").click(function(){
			$("a.selecteded").removeClass("selecteded");
			$(this).addClass("selecteded");
			$("#allContent").fadeOut("normal");		
			 $.ajax({
				type: "POST",
				url: "daycontent.php",
				data: 	"day=0",
				success: function(html){
					var htmlparsed = unescape(decodeURI(html));
					$("#allContent").html(htmlparsed);			
					$("#allContent").fadeIn("normal");		
					//init();
					
				}
			});
		});
		
		$("#butt_day1").click(function(){
			$("a.selecteded").removeClass("selecteded");
			$(this).addClass("selecteded");
			$("#allContent").fadeOut("normal");		
			 $.ajax({
				type: "POST",
				url: "daycontent.php",
				data: 	"day=1",
				success: function(html){
					var htmlparsed = unescape(decodeURI(html));
					$("#allContent").html(htmlparsed);			
					$("#allContent").fadeIn("normal");		
					//init();
					
				}
			});
		});
		
		$("#butt_day2").click(function(){
			$("a.selecteded").removeClass("selecteded");
			$(this).addClass("selecteded");
			$("#allContent").fadeOut("normal");		
			 $.ajax({
				type: "POST",
				url: "daycontent.php",
				data: 	"day=2",
				success: function(html){
					var htmlparsed = unescape(decodeURI(html));
					$("#allContent").html(htmlparsed);			
					$("#allContent").fadeIn("normal");		
					//init();
					
				}
			});
		});
		
		$("#butt_day3").click(function(){
			$("a.selecteded").removeClass("selecteded");
			$(this).addClass("selecteded");
			$("#allContent").fadeOut("normal");		
			 $.ajax({
				type: "POST",
				url: "daycontent.php",
				data: 	"day=3",
				success: function(html){
					var htmlparsed = unescape(decodeURI(html));
					$("#allContent").html(htmlparsed);			
					$("#allContent").fadeIn("normal");		
					//init();
					
				}
			});
		});
		
		$("#butt_day4").click(function(){
			$("a.selecteded").removeClass("selecteded");
			$(this).addClass("selecteded");
			$("#allContent").fadeOut("normal");		
			 $.ajax({
				type: "POST",
				url: "daycontent.php",
				data: 	"day=4",
				success: function(html){
					var htmlparsed = unescape(decodeURI(html));
					$("#allContent").html(htmlparsed);			
					$("#allContent").fadeIn("normal");		
					//init();
					
				}
			});
		});
}
