$(document).ready( function(){ 
	$('#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);
		}
	} );
	$('#butclosenews').click( function () { $('#news').hide(); } );
	$('#linknews').click( function () {
		if ($('#news').is(':visible')){
			$('#news').hide();
		} else {
			$('#news').show();
		}
	});
	$('#butcloseconnect').click( function () { $('#connect').hide(); } );
	$('#linkconnect').click( function () {
		if ($('#connect').is(':visible')){
			$('#connect').hide();
		} else {
			$('#connect').show();
		}
	});

	var timer2 = setInterval(slideSwitch2, 2000);
	var timer1 = setInterval(slideSwitch1, 2000);	
	var timer = setInterval(initState, 100);
});

function openPress() {
	if ($('#press').is(':visible')){
		$('#press').hide();
	} else {
		$('#press').show();
	}
}
function closePress() {	$('#press').hide(); }

function slideSwitch1() {
    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');
}

function slideSwitch2() {
    var $active = $('#slideshow2 img.active');
    if ( $active.length == 0 ) $active = $('#slideshow2 img:last');
    var $next = $active.next().length ? $active.next() : $('#slideshow2 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();
	$("#load").show();
	$("#content").fadeOut("fast", function(){ 
		$("#selectmenu").val(p);
		$('html, body').scrollTop(0);
		$("#content").load("project.php", { id: p }, function (){ 
			var $links = $('.projectimages img');
			$.preload( $links, {
				onComplete:function(data){
					if (data.index==10){
						$("#content").fadeIn("fast"); 
						$("#load").hide();
					}
				},
				onFinish:function(){
					$("#content").fadeIn("fast"); 
					$("#load").hide();
				}
			});
		}); 
	}); 
}
function clearFull(){ $("#full").fadeOut("fast", function(){ $("#full").empty(); }); }
function clearOverview(){ $("#overview").fadeOut("fast"); }
function laadFull(p){ $("#full").fadeOut("fast", function(){ $("#full").load(p, "", function (){ $("#full").fadeIn("fast"); }); }); }
var m = 0;
function laadOverview(){ 
	window.location.hash = "/overview";
	initLoc = window.location.hash;
	clearFull(); 
	$("#selectmenu").val(""); 
	$("#content").fadeOut("fast", function(){ 
		$("#content").empty();
		if (m == 0){
			$("#load").show();
			$("#overview").fadeTo(0,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();
				
				var $links = $('.project img');
				$.preload( $links, { 
					onFinish:function(){
						$("#overview").fadeTo("fast", 1);
						$("#overview").css("display","inline");
						$("#load").hide();
					}
				});
				m++;
			});	
		} else {
			$("#overview").css("display","inline");
			$("#overview").fadeTo("fast", 1);
		}
	}); 
}
