window.log = function(){
   log.history = log.history || [];
   log.history.push(arguments);
   arguments.callee = arguments.callee.caller;
   if(this.console) console.log( Array.prototype.slice.call(arguments));
}
 
function resize(){
	stageh=$(window).height();
	stagew=$(window).width();
	var fotoleft=(stagew -990)/2;
	$("#fotoscroll").css("marginLeft",fotoleft+"px");
	//log("fotoleft:"+fotoleft);
}
var actief_foto=0 // 
var click_foto=-1
$(document).ready(function(){ 
	log("ready");
	// centreren header nav
	var headerw=0
	$("#headernav li").each(function(){
		headerw += $(this).width();
	})
	var marginleft=(990 - headerw-0)/2;
 	$("#headernav").css("marginLeft",marginleft+"px");

 
 // centreren subnav
 var subw=0;
 $("#headersubnav li").each(function(){
		subw += $(this).width();
	})
	var marginleft=(990 - subw-0)/2;
 	$("#headersubnav").css("marginLeft",marginleft+"px");
 
 // centreren taalnav
 var subw=0;
 $("#taalnav li").each(function(){
		subw += $(this).width();
	})
	var marginleft=(990 - subw-0)/2;
 	$("#taalnav").css("marginLeft",marginleft+"px");
 
 	// $("#frulrechts").css("marginLeft",(headerw-9)+"px");
 marginleft=0;
 
	 $("#fotoholder .foto").each(function(i){ 
			$(this).css("marginLeft",marginleft+"px");
			marginleft+=240+10; 
			$(this).data("num",i);
			var myimg=$(this).find(".flou");
			 
			$(myimg).bind("click",function(){
				// if($(this).css("opacity") > 0){  
				click_image($(this).parent().data("num"));
				//}
				
			})
			$(this).find(".infokader .close").bind("click",function(event){
				 log("click infokader");
				   declick_images();
				   click_foto=-1;
	 		 fade_out_black();
			  drag_bar($("#slidebar").position().left); 
			 stop_drag($("#fotoholder").css("marginLeft") );
			  event.stopPropagation();
			})
	})
	 
	 /* zetten van de contacthoogtes op gelijke hoogtes ****/
	 var contacth=$("#contact2").height();
	 contacth > $("#contact3").height()?null:contacth=$("#contact3").height();
	  
	 $("#contact1").height(contacth);
	 $("#contact2").height(contacth);
	 $("#contact3").height(contacth);
	  
	  
	  
  $("#slidebar").draggable({containment: "parent"});
  $("#slidebar").bind('drag',function (event,ui){
	 /*log("drag me"+$(this).position().left);*/
	 $("#slidebar").addClass("active");
	 drag_bar($(this).position().left); 
  });
    $("#slidebar").bind('dragstop',function (event,ui){
	 /*log("drag me"+$(this).position().left);*/
	 var marginleft=$("#fotoholder").css("marginLeft");
	 stop_drag(marginleft);
  });
  $("#slidebar").bind('dragstart',function (event,ui){
	  declick_images()
	 fade_out_black();
	 
  });
  
  $("#fotopijl_left").bind("click",function(){
	 //log("click pijlleft");	 
	 if(actief_foto>0){
		 var oldclick=click_foto;
	
		 if(oldclick >0){ oldclick--;click_image(oldclick);return}
		 	 fade_out_black();
			 declick_images();		 
		 
			log("newclick"+click_foto);
			var left=$("#fotoholder").css("marginLeft") 
			left= Number(replacePx(left,"")); 
			stop_drag(left+1*240+"px");
	 }		 
 })
  
   $("#fotopijl_right").bind("click",function(){
	 //log("click pijlleft");
	 var aantfotos=getAantalFotos()// $("#fotoholder .foto").length-4;
	 if(click_foto > -1){
		 aantfotos-=2;
		 log(actief_foto+" / "+ aantfotos);
		 if(aantfotos -actief_foto == 2){ 
			  if(click_foto == aantfotos-2){
				  click_image(aantfotos-1);
				 return
			 }
		 return
		 }
	 }else{
		aantfotos-=4; 
	 }
	
	
	 if(actief_foto<aantfotos){
		var oldclick=click_foto;
		
			
		if(oldclick >-1){ oldclick++;click_image(oldclick);return}
		 fade_out_black();
		declick_images();
		var left=$("#fotoholder").css("marginLeft") 
		left= Number(replacePx(left,"")); 
		stop_drag(left-1*240+"px");
	 }		 
 })
  
  
 fade_to_black();
 resize();
  
  $(".meerfotos").click(function(e) {
				log("click"+$(this).attr("id"));	
				var mijnid=$(this).attr("id");
								
								
								
	$.ajax({
	   type: "POST",
	   /*url: "getElement.cfm",*/
	   url: "ajax/getAfbeeldingen.cfm", 
	    data: "id="+mijnid,
	  
	   success: function(data){
		    
		      arr_images=data.split(",") //["27-12.jpg","27-13.jpg","27-14.jpg"];
			 
			 if(data==""){
				 return
			 }
			$.fancybox(arr_images, {            
            'transitionIn'      : 'elastic',
            'transitionOut'     : 'none',
            'type'              : 'image',
			'overlayShow': true,
        });
	   }
	   })
		e.stopPropagation();
						 
								 
								 
	 
	})
								 
								 
								 
			 
   
 /*
  $(".meerfotos").click(function(e) {
								
        $.fancybox([
            'http://farm5.static.flickr.com/4044/4286199901_33844563eb.jpg',
            'http://farm3.static.flickr.com/2687/4220681515_cc4f42d6b9.jpg',
            {
                'href'  : 'http://farm5.static.flickr.com/4005/4213562882_851e92f326.jpg',
                'title' : 'Lorem ipsum dolor sit amet, consectetur adipiscing elit'
            }
        ], {
            'padding'           : 0,
            'transitionIn'      : 'none',
            'transitionOut'     : 'none',
            'type'              : 'image',
            'changeFade'        : 0
        });
		e.stopPropagation();
    });
   
 
 */
 
 
 /*
 $("a[rel=lightbox]").bind.fancybox({
	'opacity'		: true,
	'overlayShow'	: true,
	'transitionIn'	: 'elastic',
	'transitionOut'	: 'none'
 });
 */
	 
	  
	 
	 
	 setTimeout( setFrulRechts, 100 );
	 
	 if($("#fotoholder .foto").length<=4){
		 $("#slider").css("visibility","hidden"); 
		 
	 }else if($("#fotoholder .foto").length<=2){
	 	   $("#fotopijl_right").css("visibility","hidden");
		    $("#fotopijl_left").css("visibility","hidden");
	 }
	 
	// $("#headernav").css("visibility","hidden");
	/*
	$("#centerhome .homeimg").load(function(){
	log("loaded");	
	$(this).fadeTo(400,1);
	})
	*/
});// end ready

function setFrulRechts(){
	var headerw=0
	$("#headernav li").each(function(){
		headerw += $(this).width();
	})
	$("#frulrechts").css("marginLeft",(headerw-30)+"px");	
	// $("#headernav").css("visibility","visible");
	$("#headernav").fadeTo(200,1);
	$("#headersubnav").delay(10).fadeTo(200,1);
	$("#centerhome .homeimg").fadeTo(400,1);
}

$(window).resize(resize)

function getAantalFotos(){
	var aantfotos= $("#fotoholder .foto").length;
	if(click_foto >-1){
	 	aantfotos+=2;
	}
	return aantfotos
}

function replacePx(str,toreplace){	
	if(typeof(str) == "undefined"){return ""}
	//console.log("func replaceTekst "+str);
	str= str.replace("px",toreplace);	 
	return str 
}

function drag_bar(left){ // dragfunctie van de slidebar onderaan 
	var w=740; // breedte van de kader waarin gedragt wordt
	var bar_br=30; // breedte van het dragbalkje
	var aantfotos=getAantalFotos();// $("#fotoholder .foto").length;
	var scrollperc=left/(w-bar_br);	
	//
	var totallen= aantfotos *240 + (aantfotos-1)*10;
	var overschot=totallen - (4*240+3*10);
	$("#fotoholder").css("marginLeft",-overschot*scrollperc+"px") 		 
}

function stop_drag(sliderleft){ 
	var sliderleft= Number(replacePx(sliderleft,""));
	// eerste actief zichtbare; 
	var act= Math.round(Math.abs(sliderleft) /(240+10) ); 

	var tox=-(act*(240+10)) ; 
	$("#fotoholder").stop(true,true).animate({ marginLeft:tox},"slow")
	//
	 
	actief_foto=act;
	
	var aantfotos=getAantalFotos();// $("#fotoholder .foto").length;
	var w=740; // breedte van de kader waarin gedragt wordt
	var bar_br=30; // breedte van het dragbalkje
 
	$("#slidebar").removeClass("active");
	var sliderx=((w - bar_br)/(aantfotos-4))*act;
	$("#slidebar").stop(true,true).animate({ left:sliderx},"slow",function(){
													if(click_foto > -1){
														check_if_foto_open();
													}else{
													   fade_to_black();
													};
											});
	
}

function fade_out_black(){
	log("fadeoutblack");
	 $("#fotoholder .foto").each(function(i){ 
		$(this).find(".flou").stop(true,true).fadeTo("fast",0);	
	})
}
function fade_to_black(){
	 var halfflou=-1
	 var aantal=$("#fotoholder .foto").length
	 $("#fotoholder .foto").each(function(i){
		var centr=3;
		if(click_foto >-1){
			centr=1;
		 	if(click_foto==aantal-1){
				//halfflou=click_foto-1
				halfflou=click_foto-1
		 	}else{
				halfflou=actief_foto+1
				//halfflou=click_foto+1
		 	}
		}
		if(i == halfflou){
			$(this).find(".flou").stop(true,true).fadeTo("slow",.4);
		}else{
			if(i<actief_foto || i > actief_foto+centr){
				$(this).find(".flou").stop(true,true).fadeTo("slow",.8);
			}else{
				$(this).find(".flou").stop(true,true).fadeTo("slow",0);
			}
		}
	})
	 
}


function click_image(fotonum){
	log("clickimage"+fotonum);
	var marginleft=0;
		 $("#fotoholder .foto").each(function(i){
											  if(i == fotonum){
												  $(this).addClass("active");
												  	$(this).find(".infokader .infocontent").css("display","block");
													$(this).find(".infokader .infocontent").css("opacity",0)
													$(this).find(".infokader .infocontent").stop(true,true).delay(200).fadeTo(1000,1);
												  	$(this).find(".infokader").delay(1000).stop(true,true).animate({width:490},500,"easeInOutCirc",function(){
																															  log("zet aan");
																															 // $(this).find(".infocontent").css("display","block");
																															  
																															  });
											  }else{
												    $(this).removeClass("active");
													//if($(this).find(".infokader").width() >0){
														log("content onzichtbaar zetten");
														$(this).find(".infokader .infocontent").css("display","none");
														$(this).find(".infokader").stop(true,true).animate({width:0},"fast");
												//	}
											  }
			if(i>fotonum){
				$(this).stop(true,true).delay(20).animate({ marginLeft:(marginleft+250+250)+"px"},"normal");
			}else{
				//$(this).css("marginLeft",marginleft+"px");
				$(this).stop(true,true).animate({ marginLeft:marginleft+"px"},"normal");			
			}
			marginleft+=240+10; 
		})
		click_foto=fotonum;
		stop_drag($("#fotoholder").css("marginLeft"));
}

function declick_images(){
	var marginleft=0;
	 $("#fotoholder .foto").each(function(i){  
		$(this).stop(true,true).animate({ marginLeft:marginleft+"px"},"normal");
		
		// $(this).find(".infokader").stop(true,true).width(0);
			$(this).find(".infokader .infocontent").css("display","none");
			$(this).find(".infokader").stop(true,true).animate({width:0},"fast");
		
		marginleft+=240+10; 
	})
	click_foto=-1;
}

function check_if_foto_open(){ 
	 
	if(click_foto > -1){	
	//log("check if open"+click_foto);
		var aantfotos=getAantalFotos();
		aantfotos-=2
		var totallen= aantfotos *240 + (aantfotos-1)*10;
		var overschot=totallen - (4*240+3*10);
		var tox=click_foto*240 + click_foto*10
		actief_foto=click_foto;
		if(actief_foto==aantfotos -1){
			actief_foto=aantfotos -2
			tox-=250;
		}
	
		$("#fotoholder").stop(true,true).animate({ marginLeft:-tox},800,"easeInOutCirc",function(){fade_to_black();})
		
		var w=740; // breedte van de kader waarin gedragt wordt
		var bar_br=30; // breedte van het dragbalkje	
		var sliderx=((w - bar_br)/(aantfotos-2))*actief_foto;
 	 
	//  $("#slidebar").stop(true,true).animate({left:sliderx},"fast");
		$("#slidebar").css("left",sliderx+"px");
	}
}
 
