$(document).ready( function(){ 
	$("#title").fadeTo(0, 0.5);
	$("#menu").fadeTo(0, 0.5);
	$("#info_1").fadeTo(0, 0.5);
	$("#info_2").fadeTo(0, 0.5);
	$("#butprev").fadeTo(0, 0.5);
	$("#butnext").fadeTo(0, 0.5);
	$("#butoverview").fadeTo(0, 0.5);
	
	$("#title").hover( function () { $(this).fadeTo("fast", 1); }, function () { $(this).fadeTo("fast", 0.5); });
	$("#menu").hover( function () { $(this).fadeTo("fast", 1); }, function () { $(this).fadeTo("fast", 0.5); });
	$("#info_1").hover( function () { $(this).fadeTo("fast", 1); }, function () { $(this).fadeTo("fast", 0.5); });
	$("#info_2").hover( function () { $(this).fadeTo("fast", 1); }, function () { $(this).fadeTo("fast", 0.5); });
	$("#butprev").hover( function () { $(this).fadeTo("fast", 1); }, function () { $(this).fadeTo("fast", 0.5); });
	$("#butnext").hover( function () { $(this).fadeTo("fast", 1); }, function () { $(this).fadeTo("fast", 0.5); });
	$("#butoverview").hover( function () { $(this).fadeTo("fast", 1); }, function () { $(this).fadeTo("fast", 0.5); });
	
	$('#selectmenu').change( function() { 
		var page = $('#selectmenu').val();
		if (page !=''){
			laadProject(page);
		}
	});
	$("#butoverview").click( function () { laadOverview(); $('html, body').scrollTop(0); });
	$("#butprev").click( function () { 
		var index = $('#selectmenu')[0].selectedIndex;
		var page = $('#selectmenu')[0].options[index-1].value;
		if (page !=''){
			laadProject(page);
			$("#selectmenu").val(page);
		}
	} );
	$("#butnext").click( function () { 
		var index = $('#selectmenu')[0].selectedIndex;
		var page = $('#selectmenu')[0].options[index+1].value;
		if (page !=''){
			laadProject(page);
			$("#selectmenu").val(page);
		}
	} );
	slideSwitch();
	var timer2 = setInterval(slideSwitch, 2000);	
	var timer = setInterval(initState, 100);
});

function slideSwitch() {
    var $active = $('#slideshow img.active');
    if ( $active.length == 0 ) $active = $('#slideshow img:last');
    var $next =  $active.next().length ? $active.next() : $('#slideshow img:first');
    $active.addClass('last-active');
    $next.addClass('active');
    $active.removeClass('active last-active');
}

var initLoc = "";
function initState() {
	if (window.location.hash==initLoc) {
		if (window.location.hash!=""){
			return;
		}
	}
	initLoc = window.location.hash;
	var hashDeel = initLoc.substring(2,10);
	if (hashDeel=="overview") {
		laadOverview();
	} else if (hashDeel=="projects") {
		var hashNr = initLoc.substring(11);
		if (hashNr.length<=3) {
			laadProject(hashNr);
		} else {
			laadOverview();
		}
	} else {
		laadOverview();
	}
}

function laadProject(p){ 
	window.location.hash = "/projects/" + p;
	initLoc = window.location.hash;
	clearFull(); 
	clearOverview();
	$("#content").fadeOut("slow", function(){ 
		$("#selectmenu").val(p);
		$("#content").load("project.php", { id: p }, function (){ 
			$("#content").fadeIn("slow"); 
		}); 
	}); 
}
function clearFull(){ $("#full").fadeOut("slow", function(){ $("#full").empty(); }); }
function clearOverview(){ $("#overview").fadeOut("slow"); }
function laadFull(p){ $("#full").fadeOut("slow", function(){ $("#full").load(p, "", function (){ $("#full").fadeIn("slow"); }); }); }
var m = 0;
function laadOverview(){ 
	window.location.hash = "/overview";
	initLoc = window.location.hash;
	clearFull(); 
	$("#selectmenu").val(""); 
	$("#content").fadeOut("slow", function(){ 
		$("#content").empty();
		if (m == 0){
			$("#overview").load("overview.php", "", function (){ 
				$(".overviewmetadata").fadeTo(0, 0);
				$(".project").hover( function() {  $(this).children('div:first').fadeTo("fast", 0.85); }, function () { $(this).children('div:first').fadeTo("fast", 0); });
				$("#overview").masonry();
				$("#overview").css("display","inline");
				$("#overview").fadeTo("slow", 1);
				m++;
			});	
		} else {
			$("#overview").css("display","inline");
			$("#overview").fadeTo("slow", 1);
		}
	}); 
}