	var open = false;					/* subContainer open? */
 	var t;								/* timeout van menu + logo */
 	var s;
 	var id_geopend = "leeg";			/* vorig id van subcontainer */
	var dubbel = false;					/* sprake van 2 open venstertjes? */
	var clickAllowed = true;			/* klikken Toegelaten */
	var footerOpened = false;
	var werkPresKnipperAllowed = false;
	var galleryClicked = false;
	var werk_presentatie_open = false;
	var homeOpened = true;
	var footerKnipperBezig = false;
	
	// Ga uit timeout
 	function stopcheck() {
            clearTimeout(t);
    } 
    
    // Ga uit timeout
 	function stopcheck_werk() {
            clearTimeout(s);
    } 
    
    	
	function openContainer(id) {
						
				
						// Wanneer catvenster niet opnieuwgeopend moet worden, werkfooter weg			
						if ((id != 'cat') && (id_geopend != 'cat')) sluitFooter();
						
						if (id_geopend == "leeg") {
							setTimeout(function(){
								changeBgImage("images/" + id + "_achtergrond.jpg",300 );
								homeOpened = false;
							},400);
						}
						
										
						// Wanneer geen venster geopend OF het cat venster is pas gesloten en wilt opnieuw geopend worden
						if ((id_geopend == "leeg") || ((id == 'cat') && (id_geopend == 'cat'))) {	
								
									
									// Als werk_pres_pijlke staat, sluit werk en open werkcontainer
									if (werk_presentatie_open == true) { 
										$("#werk_presentatie").animate({height: "hide"}, 300, function() {
												openContainerDefaultAction(id);												
								    	});
								    }
								    else {	
								    			openContainerDefaultAction(id);								    			
								    }
					    }
					    
					    // Wanneer reeds een venster geopend, em
					    else if (id_geopend != id) {
					    						    			
						    		// cat=work
						    		if (id == 'cat') id = 'work';
						    		
						    		// sluit huidig venster
						    		
						    		
						    		sluitContainer();		
						    		
						    		
						    		// binnen 0,x seconden open nieuw venster    		
						    		setTimeout(function() {							
												$("#"+ id).click();								
									}, 700); 
									 
					    }
	}						    		
	
	
	
	
	
	function sluitContainer() {
			
			sluitFooter();
			
			
				
			if ((id_geopend != "leeg") && (werk_presentatie_open == false)) {	

				clickAllowed = false;				
				
				//changeBgImage("images/home_achtergrond.jpg",300);
									
				$("#sub_container_" + id_geopend).animate({height: "hide"}, 600, function () {		
		        		clickAllowed = true;
		        		id_geopend = 'leeg';
		      	});
	      	}
	      	else if ((id_geopend != "leeg") && (werk_presentatie_open == true)) {
	      		clickAllowed = false;
	      		
	      		//changeBgImage("images/home_achtergrond.jpg",300);
	      		
	      		$("#werk_presentatie").animate({height: "hide"}, 300, function() {			      		
			      		$("#sub_container_" + id_geopend).animate({height: "hide"}, 600, function () {		
				        		clickAllowed = true;
				        		id_geopend = 'leeg';
				      	});
				});
	      	
	      	}
	      	
	}
	
	
	function openContainerDefaultAction(id) {
			// Geen kliks toegelaten tijdens animatie
			clickAllowed = false;
			
			// Laat scrollbar verdwijnen				
			$("body").css("overflow", "hidden");
			
			$("#sub_container_" + id).animate({height: "show"}, 600, function() {							
								
								// show scrollbar
								$("body").css("overflow", "auto");	
								
								// Animatie is klaar, klik maar
								clickAllowed = true;										
		  	});								    
		    
	
		    
		    
		    // Save venster wat nu open staat
		    id_geopend = id;	
	}
	
	
	function sluitFooter() {			
			if (footerOpened == true) {					
									
					//hide footerwerkbalk
					$("#footer_werkpres").hide(function() {
							footerOpened = false;
							
							//show foottekst
							setTimeout(function() {
								$("#foot_tekst").css('display','');
							}, 300);
					});
			}
	}
	
	
	function reopenWork() {
			galleryClicked = true;
			clickAllowed = false;
			// Laat scrollbar verdwijnen				
			$("body").css("overflow", "hidden");
			
			$("#werk_presentatie").animate({height: "hide"}, 1, function() {
						
						$("#sub_container_cat").animate({height: "show"}, 600, function() {							
												
												// show scrollbar
												$("body").css("overflow", "auto");	
												
												// Animatie is klaar, klik maar
												clickAllowed = true;										
						});
						// werkpres_pijl is weg
						werk_presentatie_open = false;
			});
	}
	

 	$(document).ready(function(){ 	
 	
 		
 		
 		/** AchtergrondVuller **/ 		
		$(document).bgStretcher({
				images: ['images/blank.jpg'], 			
				nextSlideDelay: 20000,
				resizeAnimate: false,
				resizeProportionally: true,
				imageWidth: 1229, 
				imageHeight: 768				
		});
		
 		
 		/** Logo + Menu  **/ 	
 		$("#main_wrapper").mouseenter(function(){
 				$('#blinkblink').remove();
 				$("#main_wrapper").animate({"height": "294"}, { duration: 500 });
	  			$("#menu_wrapper").animate({"height": "220"}, { duration: 500 });
	  			$("#main_wrapper").clearQueue();
	  			$("#menu_wrapper").clearQueue();
	  	});
		
		
		//Terugtrekking menu na x sec
		$("#main_wrapper").mouseleave(function(){
												
						t = setTimeout(function(){
									$("#main_wrapper").animate({"height": "80"}, { duration: 500 });
	  			
				  					$("#menu_wrapper").hide({ duration: 350 });
				  					$("#menu_wrapper").animate({"height": "1"}, { duration: 500 });	  			
				  			
				  					$("#main_wrapper").clearQueue();	  			
				  					$("#menu_wrapper").clearQueue();			  					
									
							},2000);  				
	  	});
	  		
	  	
	  	
	  		
		/**  Menu  **/ 		
		$("#home").click(function() {	
			if (clickAllowed) {
			
					if (homeOpened == false) {
										
							setTimeout(function(){
										changeBgImage("images/home_achtergrond.jpg",300);
										homeOpened = true;
							},700);
					}
					
					sluitContainer();
					
			}								
		});
		
		
		$("#about").click(function() {	
			if (clickAllowed) {				
					openContainer("about");
			}						
		});
		
		
		$("#work").click(function() {
			if (clickAllowed) {				
					openContainer("cat");
			}
		});
		
		
		$("#links").click(function() {	
			if (clickAllowed) {				
					openContainer("links");
			}
		});		
		/** End Menu  **/ 	
		
		
		
		
		/** Werk Presentatie **/
		$("#werk_presentatie").mouseenter(function() {	
			if (clickAllowed) {				
					reopenWork();
			}
		});
			
 
		
		$('#pijl_rechts > a').click(function() {			
				verander_foto_pijl('next');	
		});
		
		$('#pijl_links > a').click(function() {			
				verander_foto_pijl('prev');					
		}); 
		
		
				
		// Terugtrekking menu_werk na 0,x sec
		$(".sub_wrapper_cat").mouseleave(function(){	
		
				if (galleryClicked == true) {
						
						
						setTimeout(function(){
										
										$("#sub_container_cat").animate({height: "hide"}, 600, function () {				
					
											   // galleryLastOpenedInAction = true;
												  id_geopend = 'cat';					
									     	      galleryClicked = false;
									     	      
									     	      $("#werk_presentatie").animate({height: "show"}, 300, function() {									     	      	
									     	      			werk_presentatie_open = true;									     	      			
															
															
													if (werkPresKnipperAllowed && werk_presentatie_open) {		
																    
															knipperWerkpres();															
															werkPresKnipperAllowed = false;
													}

									     	      });
									     	      					
										});
										
						}, 1);
						
				}		  	
	  	
	  	});
	  	
	  	
	  	$('.categorie').click(function() {						
				galleryClicked = true;				
		}); 
		
		
		
		
		//LOADER
		$.loadImages('images/home_achtergrond.jpg',callback());
				// $('#loading').css('display','block'); 
		
		
		
		
	});
	
	function createLinks() {
			$("a").filter(function() {
	    		return this.hostname && this.hostname !== location.hostname;
			}).attr('target', '_blank');
	}
	
	function callback() {	
						
			setTimeout(function() {
				changeBgImage("images/home_achtergrond.jpg",300);
				knipperLogo();														
			},400);
							
	}
	
	function knipperLogo() {				
							
				setTimeout(function() {					
						/*
$('#blinkblink').css('display','');	
						setTimeout(function() {					
								$('#blinkblink').css('display','none');									
						},1000);
						
						
*/				
				$("#main_wrapper").mouseenter();
				},6500);
			
	}
	
	//Haal de gallerij op voor het gekozen project, ajax
	function haal_gallerij(id) {
		
		if (id != 'statisch') {
		
			$.ajax({						    
			    url: "get_gallery.php",
			    type: "POST",
			    data: "id_gal="+id,
			    beforeSend: function(x) {
  					if(x && x.overrideMimeType) {
   						x.overrideMimeType("application/j-son;charset=UTF-8");
  					}
 				},
			    dataType: 'json',
			    success: function(data){
			    	$('#footer_content').html(data.links);
			    	$('#preload').append(data.afbeeldingen);
			    	changeBgImage($('.picture').attr('alt'), 300);
			    	createLinks();
			    	//console.log(data.links);
			    },
			    error: function(){
			    	console.log('failure');
			    }//,
			    //beforeSend: function() { $('.loading').show(); },
        		//complete:   function() { $('.loading').hide(); }	
			});
		}
		
		//test voor Rob
		else {
			$('#footer_content').html('<span href="#" alt="images/achtergrond_rs2.jpg" class="picture selected">corporate identity</span><span href="#" alt="images/achtergrond_rs1.jpg"   style="display:none;" class="picture">precedence process architecture</span><span href="#" alt="images/achtergrond_rs3.jpg"   style="display:none;" class="picture">business cards</span><span href="#" alt="images/achtergrond_rs4.jpg"  style="display:none;" class="picture">launch site &gt;&gt;</span>');
			
			changeBgImage($('.picture').attr('alt'), 300);
		}
		
		
		// Toon passende werkfooter
		if (clickAllowed) {
				
				// foot tekst arrivederci
				$("#foot_tekst").css('display','none');
				
				// Toon footer  met hyperlink img's
				$("#footer_werkpres").show(function() {
						footerOpened = true;
						knipper(3);						
				});	
		
		}
		
		
	}
	
	function knipper(aantal) {
		var speed = 400;
		var speedKnipper = 800;
		werkPresKnipperAllowed = false;
		
		if (footerKnipperBezig == false) { 
			footerKnipperBezig = true;
			setTimeout(function(){
					$("#pijl_rechts_img").attr("src","images/pijl_rechts_knipper1.png");	
					$("#pijl_links_img").attr("src","images/pijl_links_knipper1.png");	
							setTimeout(function(){
								$("#pijl_rechts_img").attr("src","images/pijl_rechts1.png");	
								$("#pijl_links_img").attr("src","images/pijl_links1.png");
											setTimeout(function(){
												$("#pijl_rechts_img").attr("src","images/pijl_rechts_knipper1.png");	
												$("#pijl_links_img").attr("src","images/pijl_links_knipper1.png");
																setTimeout(function(){
																	$("#pijl_rechts_img").attr("src","images/pijl_rechts1.png");	
																	$("#pijl_links_img").attr("src","images/pijl_links1.png");																			
																					setTimeout(function(){
																						$("#pijl_rechts_img").attr("src","images/pijl_rechts_knipper1.png");	
																						$("#pijl_links_img").attr("src","images/pijl_links_knipper1.png");
																										setTimeout(function(){
																											$("#pijl_rechts_img").attr("src","images/pijl_rechts1.png");	
																											$("#pijl_links_img").attr("src","images/pijl_links1.png");	
																											werkPresKnipperAllowed = true;
																											//console.log('klaar met knipperen');	
																											knipperWerkpres();	
																											footerKnipperBezig = false;							
																										},speed);											
																					},speedKnipper);										
																},speed);											
											},speedKnipper);									
							},speed);										
			},speedKnipper);
		}
			
	}
	
	function knipperWerkpres() {
		var speed = 400;
		var speedKnipper = 800;
		
			setTimeout(function(){
					$('#werk_presentatie').css('background-image', 'url(../images/werkpres_pijl_knipper1.png)');							
							setTimeout(function(){
									$('#werk_presentatie').css('background-image', 'url(../images/werkpres_pijl1.png)');	
									setTimeout(function(){
											$('#werk_presentatie').css('background-image', 'url(../images/werkpres_pijl_knipper1.png)');				
											setTimeout(function(){
													$('#werk_presentatie').css('background-image', 'url(../images/werkpres_pijl1.png)');	
													setTimeout(function(){
															$('#werk_presentatie').css('background-image', 'url(../images/werkpres_pijl_knipper1.png)');				
															setTimeout(function(){
																	$('#werk_presentatie').css('background-image', 'url(../images/werkpres_pijl1.png)');
																	werkPresKnipperAllowed = true;				
															},speed);
													},speedKnipper);				
											},speed);
									},speedKnipper);											
							},speed);

			},speedKnipper);
	}
	
	
	function changeBgImage(newImageSrc, speed) {
			$(".bgs-current > img").fadeOut(speed,function() {
					$('#loading').css('display','none');
				    $(this).attr('src',newImageSrc).fadeIn(speed);
			});
	}
	
	
	//Verander foto met pijltjes
	function verander_foto_pijl(richting) {
			var $oldImage = $(".selected");				
			
			//Kies volgende/vorige in rij
			if (richting == 'prev') var $newImage = $oldImage.prev();
			if (richting == 'next') var $newImage = $oldImage.next();
			
			//Zolang vorige mogelijk is;
			if ($newImage.html()) {
				$oldImage.removeClass('selected');
				$oldImage.css('display','none');
				$newImage.addClass('selected');
				$newImage.css('display','');
				
				changeBgImage($newImage.attr('alt'), 300);
				//$(".bgs-current > img").attr('src',$newImage.attr('alt'));
				
				
			}
			
			//Aan einde doorgaan met laatste;
			else {
				if (richting == 'prev') $newImage = $('#footer_content span:last-child');	
				if (richting == 'next') $newImage = $('#footer_content span:first-child'); 
							
				$oldImage.removeClass('selected');
				$oldImage.css('display','none');
				$newImage.addClass('selected');
				$newImage.css('display','');
				changeBgImage($newImage.attr('alt'), 300);
			}		
	}

