$(document).ready(function(){
							
	if(($.browser.msie && parseInt($.browser.version, 10) < 7)){
		$("*:last-child").addClass("last");
		$("*:first-child").addClass("first");
	}   
	
	$('#bureau .scroll').jScrollPane({scrollbarWidth:9, showArrows:true, scrollbarMargin:20});
	var width = $('#bureau').width();
	$('#bureau').css({'width':'0px','overflow':'hidden'});
	$('#bureau').animate({'width':width+'px'},500);
	
			var width = $('#contact').width();
	$('#contact').css({'width':'0px','overflow':'hidden'});
	$('#contact').animate({'width':width+'px'},500);
	
	var currentpage = 1;
	var totalpages = 1;
	
	$("#projects_next").click(function(){ 
		currentpage++; 
		if(currentpage > totalpages) currentpage = totalpages;
		moveSlider(); 
		return false;
	})
	
	$("#projects_prev").click(function(){ 
		currentpage--;
		if(currentpage < 1) currentpage = 1;
		moveSlider(); 
		return false; 
	})
	

	
	
	function moveSlider(){
		var newpos = ((currentpage-1)*-$("#projectencontainer").width());
		$("#projectenslider").stop().animate({'left':newpos+'px'},500,"easeOutQuad");
		pagecounter();
	}
	
	$('#projectencontainer li').hover(
		function(){
			$(this).find("span").stop().animate({'top':'0px'},300);										 
		},
		function(){
			$(this).find("span").stop().animate({'top':'-23px'},200);										 
		}
	);
	
	$('#projectencontainer li').click(function(){ showProject($(this).attr('id')) });
	
	
	
	$('#projectencontainer li').addClass('show').each(function(i){
		var top = i%5;
		$(this).css({'position':'absolute','left':'0px', 'top':(top*($(this).height()+1))+'px'})
	});
	
	
	$("input[type=checkbox][name=filter]").click(function(){
		$('#projectencontainer li').stop().removeClass('moving').removeClass('doHide').addClass('doShow');
		$(this).parent().parent().find("input[type=checkbox][name=filter]").each(function(){
			if($(this).attr('checked')){
				$(this).parent().find("label").addClass('activeMenu');
				$("#projectencontainer li:not([rel*=';"+$(this).val()+";'])").removeClass('doShow').addClass('doHide')
			}else{
				$(this).parent().find("label").removeClass('activeMenu');
			}
		});
		
		var number = -1;
		
		$('#projectencontainer li.doShow:not(.hiding)').addClass('moving');
		
		$('#projectencontainer li').addClass('show').each(function(){
			if($(this).hasClass('doShow')){
				number++;
				
				$(this).attr('currentcolumn',$(this).parent().parent().children().index($(this).parent()));
				$(this).attr('targetcolumn',Math.floor(number/5));
				$(this).attr('targettop',(number%5) * ($(this).height()+1));
				
				if($(this).hasClass('hiding')){
					$(this).removeClass('moving');
				}else{
					$(this).removeClass('doShow');
					moveblock($(this))						
				}
			}else{				
				$(this).removeClass('moving doShow doHide show').addClass("hiding").fadeTo(200,0,false);				
			}
		});	

		if($(".moving").length == 0){
			showblocks();
		}
		pagecounter();
		if(currentpage > totalpages){
			currentpage = totalpages;
			moveSlider();
		}
	});
	
	function pagecounter(){
		var projects = $('#projectencontainer li.show').length;
		totalpages 	= Math.ceil(projects/25);
		if(totalpages < 1) totalpages = 1;
		$('#pagecounter').html(currentpage+' / '+ totalpages)
	}
	function showblocks(){
		$(".doShow").each(function(){
			$(this).appendTo(".column:eq("+$(this).attr('targetcolumn')+")").css('top',$(this).attr('targettop')+'px').fadeTo(200,1).removeClass('doShow moving hiding doHide');
		})
	}
	
	function showInfo(){
		$("#projectinfo").stop().animate({'left':'0px'},'500',"easeOutQuad");
		$("#projectinfocontent").stop().animate({'width':'624px'},'500',"easeOutQuad");
		$("#projectinfobuttonclose").stop().show("fast");
		$("#projectinfobuttonopen").stop().hide("fast");
	};
	function hideInfo(){
		$("#projectinfo").stop().animate({'left': '-675px'},'500',"easeOutQuad");
		$("#projectinfocontent").stop().animate({'width':'655px'},'500',"easeOutQuad");
		$("#projectinfobuttonclose").stop().hide("fast");
		$("#projectinfobuttonopen").stop().show("fast");
	}
	var projectid = null;
	function showProject(id){
		projectid = id;
		$.ajax({
			type: 	'GET',
			url: 	'modules/loadproject.inc.php',
			data: 	'id='+id,
			success: function(msg){
				$("#projectenzoom").html(msg);
				$("#projectfotosslide img:not(:first)").fadeTo(10,0.1);
				$("#filter").hide();
				$("#projects_next").hide();			
				$("#projects_prev").hide();	
				$("#project_next").show();			
				$("#project_prev").show();			
				$("#sluitproject").show();				
				$("#fotograaf").show();		
				activatePicture($("#projectfotosslide img:first"));
				$("#projectenzoom").animate({'left':'0px'},'500',"easeOutQuad",function(){});
	
				$("#projectinfobuttonopen").click(function(){
					showInfo();
				});
				$("#projectinfobuttonclose").click(function(){
					hideInfo();
				});
				
			},
			error: function(xhr, ajaxOptions, thrownError){
				alert(xhr.status);
				alert(thrownError);
			}
		});
	}
	
	function hideProject(){
		$("#filter").show();
		$("#projects_next").show();			
		$("#projects_prev").show();	
		$("#project_next").hide();			
		$("#project_prev").hide();			
		$("#sluitproject").hide();				
		$("#fotograaf").hide();		
		pagecounter();
		$("#projectenzoom").animate({'left':$("#projectenzoom").parent().width() + 'px'},'500',"easeOutQuad",function(){
			$('#projectinfo').html('');
		});
	}
	
	$("#sluitproject").click(function(){
		$("#titelproject").html('');
		hideProject();
		return false;
	});
	
	$("#project_next").click(function(){ 
		var next = $(".activePic").next();
		if(next.length > 0){
			activatePicture(next);
		}
		return false;
	})
	$("#project_prev").click(function(){ 
		var prev = $(".activePic").prev();
		if(prev.length > 0){
			activatePicture(prev);
		}
		return false; 
	})		
	
	function activatePicture(picture){
		if($(".activePic").length > 0){
			$(".activePic").removeClass('activePic').stop().fadeTo(500,0.1);
		}
		picture.addClass('activePic').stop().fadeTo(500,1);
		var newpos = 0;
		var count = true;
		$(".activePic").parent().find('img').each(function(){
			if($(this).hasClass('activePic')){
				count = false;	
			}
			if(count) newpos -= $(this).width(); 
		});
		var currentpic = ($(".activePic").parent().children().index($(".activePic"))+1);
		$("#projectfotosslide").stop().animate({'left':newpos+'px'},500);
		$("#fotograaf").load('fotograaf.php?id='+projectid+'&nr='+currentpic);	
		$("#pagecounter").html(currentpic + ' / ' + $("#projectfotosslide img").length);	
	}
	
	function moveblock(project){
		project.attr('currentcolumn',project.parent().parent().children().index(project.parent()));
		
		if(project.attr('currentcolumn') == project.attr('targetcolumn')){
			
			speed = Math.abs(parseInt(project.css('top'))-project.attr('targettop')) * 2;
			if(speed < 20) speed = 20;
			project.animate({'top':project.attr('targettop')+'px'},speed,"easeOutQuad",function(){
				$(this).removeClass('moving')
				if($(".moving").length == 0){
					showblocks();
				}
			});
		}else{
			
			if(project.attr('currentcolumn') > project.attr('targetcolumn')){
				newtarget = -(project.height()+1);
			}else{
				newtarget = 419;
			}
			speed = Math.abs(parseInt(project.css('top'))-newtarget) * 1;
			project.animate({'top':newtarget+'px'},speed,"linear",function(){
				if($(this).attr('currentcolumn') > $(this).attr('targetcolumn')){
					//$(this).appendTo($(this).parent().prev());
					$(this).appendTo(".column:eq("+$(this).attr('targetcolumn')+")");
					$(this).css('top','419px');
				}else{
					//$(this).prependTo($(this).parent().next());
					$(this).appendTo(".column:eq("+$(this).attr('targetcolumn')+")");
					$(this).css('top',-(project.height()+1)+'px');
				}
				moveblock($(this))
			});			
		}
	}
	pagecounter();
});
