$(document).ready(function() {

	var itemLastPressed = new Date();
	itemLastPressed.setDate(0);

	selector = null;
	last     = null;
	uptodate = false;

	function itemHandleLoading() {
		itemLastPressed = new Date();
		itemFetch();
	}

	function itemFetch() {
		var t = new Date();
		if ( itemLastPressed.getTime() + 500 < t.getTime() ) {
			itemLastPressed = new Date();
			itemValidate();
		} else {
			setTimeout( itemFetch, 10 );
		}
	}

	function sorry() {
		if ( ! uptodate ) {
			var ouchie = "<h2>Oh noo bru! I'm beached as!</h2>Sorry for this hickup, chances are that the backend is fetching the latest posts<br />Give it a few seconds, the magic gnomes are working!";
			$("#about").hide();
			$("#about").html( ouchie );
			$("#about").animate({"opacity": 'show'}, 500);
		}
	}

	function itemValidate() {
		if ( selector != null && selector != last ) {
			last = selector;
			uptodate = false;
			var project = selector.getAttribute('alt');
			setTimeout( sorry, 3000 );
			$.getJSON("getText.php?p=" + project, function(data){
				$("#about").animate({"opacity": 'hide'}, 500, function() {
					$("#about").html( data['content'] );
					$("#about").animate({"opacity": 'show'}, 500);
					uptodate = true;
				});
				uptodate = true;
			});
		}
	}
	
	$('img').animate({"opacity": "0.60"}, 0);

	$('img').click( function() {
		itemValidate();
	});

	$('img').hover(
		function() {
			$(this).stop().animate({"opacity": "1"}, 500);
			itemHandleLoading();
			selector = this;
		},
		function() {
			$(this).stop().animate({"opacity": "0.60"}, 500);
			selector = null;
		}
	);

	itemValidate();
});


