/*functie bij referentie fotoalbum*/
function fadeEffect(kind,to){
	var left1 = '+=860';
	var left2 = '-860px';
	if(kind == 'previous'){
		left1 = '-=860';
		left2 = '860px';
	}
	
	$('.imageblock .foto.active').animate({
		"left": left1
	}, 800, function(){
		$(this).hide();
				
		if(to=="next"){
			if($('.imageblock .foto.active').next().length>0){
				$('.imageblock .foto.active').next().addClass('active');
			}else{
				$(".imageblock .foto").first().addClass('active');
			}
		}
		else if(to=="previous"){
			if($('.imageblock .foto.active').prev().length>0){
				$('.imageblock .foto.active').prev().addClass('active');
			}else{
				$(".imageblock .foto").last().addClass('active');
			}
		}else{
			$(".imageblock .foto."+to).addClass('active');
		}
		
		$(this).removeClass('active');		
		$(".fotoalbum .bubble.active").removeClass('active');
		
		
		
		var classes = $('.imageblock .foto.active').attr('class').split(' ');
		$(".fotoalbum .bubble."+classes[1]).addClass('active');		
		$('.imageblock .foto.active').show().css('left',left2);
		$('.imageblock .foto.active').animate({
			"left": left1
		}, 800, function(){});
	});
}

/*functie bij referentie op homepagina*/
function fadeEffectHome(kind,to){
	if(typeof(timeReferentieHome)!="undefined" && timeReferentieHome){
		clearTimeout(timeReferentieHome);
	}
	var left1 = '+=860';
	var left2 = '-860px';
	if(kind == 'previous'){
		left1 = '-=860';
		left2 = '860px';
	}
	
	//verwijderen van next klik bubbles
	//$('.bubbleblok').animate({"opacity":0},100,function(){$('.bubbleblok').delay(400).animate({"opacity":1});});
	
	$('.usecase.active').animate({
		"left": left1,
		"opacity": 0
	}, 800, function(){
		$(this).hide();
				
		if(to=="next"){
			if($('.usecase.active').next().length>0){
				$('.usecase.active').next().addClass('active');
			}else{
				$(".usecase").first().addClass('active');
			}
		}
		else if(to=="previous"){
			if($('.usecase.active').prev().length>0){
				$('.usecase.active').prev().addClass('active');
			}else{
				$(".usecase").last().addClass('active');
			}
		}else{
			$(".usecase."+to).addClass('active');
		}
		
		$(this).removeClass('active');		
		$("#usecases .bubble.active").removeClass('active');
		
		
		
		var classes = $('.usecase.active').attr('class').split(' ');
		$("#usecases .bubble."+classes[2]).addClass('active');		
		$('.usecase.active').show().css('left',left2).css('opacity',0);
		$('.usecase.active').animate({
			"left": left1,
			"opacity": 1
		}, 800, function(){timeReferentieHome = setTimeout("fadeEffectHome('previous','next')",5000);});
	});
}

function fadeEffect_inline(kind,to){
	var left1 = '-=642';
	var left2 = '+=642';
	var left3 = '0';	
	/*if(kind == 'previous'){
		left1 = '+=642';
		left2 = '-=642';
		left3 = '0';		
	}*/
	var durationVal = 2000;
	var top = -465;
	if(to=="next"){
		if($('.imageblock .foto.active').next().length>0){
			$('.imageblock .foto.active').next().addClass('active2');
		}else{
			$(".imageblock .foto").first().addClass('active2');
		}
	}
	else if(to=="previous"){
		if($('.imageblock .foto.active').prev().length>0){
			$('.imageblock .foto.active').prev().addClass('active2');
		}else{
			$(".imageblock .foto").last().addClass('active2');
		}
	}else{
		$(".imageblock .foto."+to).addClass('active2');
	}
	
	var classes = $('.imageblock .foto.active2').attr('class').split(' ');
	$(".fotoalbum_inline .bubble."+classes[1]).addClass('active2');
	
	
	$('.imageblock .foto.active').animate({
		"left": -642,
		duration: durationVal,
		queue: false
	}, function(){
		$(this).hide();		
		$(this).removeClass('active').css("left","");		
		$(".fotoalbum_inline .bubble.active").removeClass('active');
	});
	$('.imageblock .foto.active2').show();
	$('.imageblock .foto.active2').animate({
		"left": "-=642",
		duration: durationVal,
		queue: false
	}, function(){
		$(this).addClass("active").removeClass("active2").css("left","");
		$(".fotoalbum_inline .bubble."+classes[1]).removeClass("active2").addClass('active');
	});
}

$(document).ready(function(){	
	$('.clicksection').click(function () {
		window.location.href = $('a:first', this).attr('href');
	});

	//superfish menu
	$("ul.sf-menu").superfish();	
	$(".vijfstappen td").click(function(){window.location = $(this).find('.displaynone').attr("href");return false;}).hover(
		function(){
			var classS = $(this).attr("class");
			$(this).parent().parent().parent().addClass(classS+"sel");
		},
		function(){
			var classS = $(this).attr("class");
			$(this).parent().parent().parent().removeClass(classS+"sel");
		}
	);
	
	$('#reactiebutton').click(function() {$("#reactieform").submit();});

	//textarea resize
	$('textarea').autoResize({
		// On resize:
		onResize : function() {
			$(this).css({opacity:0.8});
		},
		// After resize:
		animateCallback : function() {
			$(this).css({opacity:1});
		},
		// Quite slow animation:
		animateDuration : 300,
		// More extra space:
		extraSpace : 40
	});
	
	//get Teampicture
	if(typeof(moduleTeampicture)!="undefined" && moduleTeampicture){
		$.ajax({
			type: "POST",
			url: "/index.php?action=ajax&type=teamPicture",
			data: "screenwidth="+$(document).width()+"&screenheight="+$(document).height(),
			async: false,
			success: function(html){
				$("#team .team-photos").append(html);
				$('#team .photohover').photohover({
					'method': 'luminosity'
				});
			}
		});
	}

	//	
	//fotoalbum_inline	
	if(typeof(moduleInlinefotoalbum)!="undefined" && moduleInlinefotoalbum){
		$(".fotoalbum_inline .next").click(function(){
			fadeEffect_inline('next','next');		
			return false;
		});
			
		$(".fotoalbum_inline .previous").click(function(){
			fadeEffect_inline('previous','previous');		
			return false;
		});
		
		$(".fotoalbum_inline .bubble").click(function(){
			if(!$(this).hasClass('active')){		
				var classes = $(this).attr('class').split(' ');
				fadeEffect_inline('next',classes[1]);
			}
			return false;
		});
	}


	//fotoalbum
	if(typeof(moduleFotoalbum)!="undefined" && moduleFotoalbum){
		$(".fotoalbum .next").click(function(){
			fadeEffect('next','next');		
			return false;
		});
			
		$(".fotoalbum .previous").click(function(){
			fadeEffect('previous','previous');		
			return false;
		});
		
		$(".fotoalbum .bubble").click(function(){
			if(!$(this).hasClass('active')){		
				var classes = $(this).attr('class').split(' ');
				fadeEffect('next',classes[1]);
			}
			return false;
		});
	}
	
	//referentieHome
	if(typeof(moduleRefentiehome)!="undefined" && moduleRefentiehome){
		var timeReferentieHome = setTimeout("fadeEffectHome('previous','next')",5000);
		$("#usecases .bubble").click(function(){
			if(!$(this).hasClass('active')){
				var classes = $(this).attr('class').split(' ');
				fadeEffectHome('previous',classes[1]);
			}
			return false;
		});	
	}
	
	//routebeschrijving
	if(typeof(moduleRoutebeschrijving)!="undefined" && moduleRoutebeschrijving){
		//niet weghalen
		if($.browser.msie && parseInt($.browser.version, 10)<8){
			$(".shadow_grijs,.mapborderbottom,.mapbordertop,.mapborderright").remove();
		}
		
		//routebeschrijving		
		var $width = screen.width;
		//calculate space left and
		$width 		 	 -= 64; //browser scrollbar size
		var $stdareawidth = 541; //standard width of image
		var $stdpagewidth = 960;
		var $widthleft	  = 0;
		if($width>$stdpagewidth){
			$widthleft = Math.ceil(($width-$stdpagewidth)/2);
		}
		
		$width = $stdareawidth + $widthleft;
		
		$('.routebeschrijving .map').css({"width":+$width+"px","height":"480px"});
		var move=true;	
		$('.routebeschrijving .onzelocatie').css({"margin-left":+$width-296+"px"});
		
		var offsetBegin	 = $('.routebeschrijving .map').offset();
		$('.controls').css({'opacity':0,"top":offsetBegin.top-60,"left":offsetBegin.left-60});
		
		//get extra coordinates to check if the mouse is 50px of the border
		var inboundBox 	   = offsetBegin;
		var border		   = 50; 
		inboundBox.right   = inboundBox.left + $('.routebeschrijving .map').width() - border;
		inboundBox.rightNoBorder = inboundBox.left + $('.routebeschrijving .map').width();
		inboundBox.bottom  		 = inboundBox.top + $('.routebeschrijving .map').height() - border;
		inboundBox.leftNoBorder  = inboundBox.left;
		inboundBox.left	  +=border;
		inboundBox.top	  +=border;
		
		var mouseCoordinates;
		
		//mousemoves on map.
		var mouseVisible   = false;
		var elementWidth   = $('.controls').width();
		var elementHeight  = $('.controls').height();
		var paddingControl = 40;
		var topMove  	   = paddingControl;
		var topMoveAlt 	   = -1*(elementHeight+paddingControl);
		var leftMove 	   = paddingControl;
		var leftMoveAlt	   = -1*(elementWidth+paddingControl);		
		$('.routebeschrijving .left,.onzelocatie').mousemove(function(e) {
			var position = $(this).position();
			var offset 	 = $(this).offset();
			var x 		 = e.pageX - (offset.left);
			var y 		 = e.pageY - (offset.top);
			var moveY    = e.pageY;
			var moveX	 = e.pageX;
		
			//check if the mouse is inbound or outbound
			if(e.pageX>=inboundBox.left && e.pageX<=inboundBox.right && e.pageY>=inboundBox.top && e.pageY<=inboundBox.bottom){
				if(move){					
					var controlOffset   = $('.controls').offset();
					controlOffset.right = controlOffset.left+elementWidth;
					
					//check where the mouse is moving to
					var move1 = topMove;
					var move2 = leftMove;
					if(typeof(mouseCoordinates)=="undefined"){mouseCoordinates = e;}
					if(mouseCoordinates.pageY<e.pageY){
						//alert(topMoveAlt);
						move1 = topMoveAlt;
					}
					if(mouseCoordinates.pageX<e.pageX){
						move2 = leftMoveAlt;
					}
					mouseCoordinates = e;
					
					
					var moveOne = moveY+move1;
					var moveTwo = moveX+move2;
					// && moveTwo>=(inboundBox.leftNoBorder+(-1*leftMoveAlt)) && moveTwo<=inboundBox.rightNoBorder
					if(!mouseVisible){
						$('.controls').fadeIn(200,function(){mouseVisible = true;});
					}
					if(mouseVisible){
						$('.controls').stop(true).delay(200,'hover').animate({opacity:1,top:(moveOne)+'px',left:(moveTwo)+'px'},500,'swing');
					}
				}else{
					$('.controls').stop(true);
				}
			}else{
				if(mouseVisible){
					mouseVisible = false;
					$('.controls').stop(true).animate({opacity:0,"top":offsetBegin.top-30,"left":offsetBegin.left-60},300,'swing');
				}
			}
		}); 
		
		$('.controls').mouseenter(function(event){
			if(mouseVisible){
				move=false;
			}
		});
		$('.controls').mouseleave(function(event){
			move=true;
		}); 
		
		$('.controls .zoomout').click(function(){
			var id = $(this).parent().attr('id');
			var mapdata = window['map'+id];
			mapdata.customZoom(-1);
			return false;
		}).hover(function(){$('.controls .zoomin').css("width","26px");$(this).css("width","54px");},function(){$('.controls .zoomin').css("width","40px");$(this).css("width","40px");});
		$('.controls .zoomin').click(function(){
			var id = $(this).parent().attr('id');
			var mapdata = window['map'+id];
			mapdata.customZoom(1);
			return false;
		}).hover(function(){$('.controls .zoomout').css("width","26px");$(this).css("width","54px");},function(){$('.controls .zoomout').css("width","40px");$(this).css("width","40px");});
	}
	//routebeschrijving
	if(typeof(moduleHeart)!="undefined" && moduleHeart){
		var position = $("#heart").offset();			
		var timeout;
		var beginPosition = new Array();
		position.top += Math.round($("#heart").height()/2)-20;			
		position.left += Math.round($("#heart").width()/2)-20;					
		//$('.icon').css({top:position.top,left:position.left});
		var nieuwePlek 		= new Array();
		nieuwePlek["icon1"] = {"top":35,"left":55};
		nieuwePlek["icon2"] = {"top":68,"left":-75};
		nieuwePlek["icon3"] = {"top":-93,"left":32};
		nieuwePlek["icon4"] = {"top":-35,"left":-61};
		$('.icon').each(function(){
			var pos    = nieuwePlek[$(this).attr("id")];
			beginPosition[$(this).attr("id")] = {top:position.top-pos.top,left:position.left-pos.left};	
		}).css({top:position.top,left:position.left}).fancybox({"overlayColor":"#fff","showCloseButton":false,content: $('#socialpopup').html(),onComplete:function(){$("#fancybox-content div").html($('#socialpopup').clone())}});
		$('#heart').mouseenter(function(){
			clearTimeout(timeout);
			var px 	= position.left-71
			var py	= position.top-70;
			var i 	= 0;
			$(".icon").each(function(){
				px+=30;
				i++;
				x = i%2 ? -1 : 1;
				
				//get current position
				var positionC = beginPosition[$(this).attr("id")];

				var bez_params = {
					start: { x:position.left, y:position.top, angle: i*20 * position.top},	
					end: {x:positionC.left,y:positionC.top, angle: i*10, length: i / 2.0}
				};				  
				$(this).stop().animate({opacity:1,path : new $.path.bezier(bez_params)},1000);				
			});
		}).fancybox({"overlayColor":"#fff","showCloseButton":false,autoDimensions:false,"width":560,"height":300,content: "f",onComplete:function(){$("#fancybox-content div").html($('#socialpopup').clone())}});	
		$('#heart,.icon,#iconbar').mouseleave(function(){				
			timeout = window.setTimeout(leave,500);
		});	
		$('.icon,#iconbar').mouseenter(function(){
			clearTimeout(timeout);	
		});
		function leave(){
			$(this).stop();
			var px 	= position.left;
			var py	= position.top;
			var i 	= 0;
			$(".icon").each(function(){
				i++;
				x = i%2 ? -1 : 1;
				var pos = $(this).offset();
				var bez_params = {
					start: { x:pos.left, y:pos.top, angle: i*20 * x},	
					end: {x:px,y:py, angle: i*10, length: i / 4.0}
				};				  
				$(this).stop().animate({opacity:0,path : new $.path.bezier(bez_params)},1000);				
			});					
		}	
	}
		
	//routebeschrijving
	if(typeof(moduleReferenties)!="undefined" && moduleReferenties){
		var seconds = 0;
		$('.referentiedots').each(function(){
			seconds = 0;
			$(this).find('.dots').each(
				function(){					
					var top  = ($(this).height()/2);
					var left = ($(this).width()/2);
					var bg	 = $(this).css('background-position-x');
					$(this).find('*').hide();
					$(this).css({'margin-top':'+='+top,'margin-left':'+='+left,'background-position-x':'-170px'});
					$(this).hide().delay(200+seconds).animate({
						opacity: 'toggle',
						height: 'toggle',
						width: 'toggle',
						'margin-top': "-="+top,
						'margin-left': "-="+left,
						'background-position-x': bg
					},500,function(){$(this).find('*').fadeIn('slow');});
					seconds = seconds+200;					
				}
			)
		})
	}
}); 



/*
	Deze functie controleert de invoer van formulieren. 
	
	Er wordt gecontroleerd of:
	-Alle verplichte velden zijn ingevuld
	-Een vinkje is gezet bij het vakje 'akkoord met algemene voorwaarden'
	-Een correct e-mailadres is ingevuld
*/
function check(which, verplicht, errorOld, emailerrorOld){
	var empty 	   		   	  = false;
	var veld  	   		   	  = "";
	var error 	   		   	  = false;
	var errorClass 		   	  = "error";
	var email	   		   	  = /^[_\.0-9a-z-]+\@([0-9a-z][0-9a-z-]*\.)+([a-z]{2,4})+$/i; //e-mail check
	var postcode  		   	  = /^[1-9][0-9]{3}[\s]([A-Z]|[a-z]){2}$/; // van 1000AA tot 8999ZZ - Let op, geen spatie!
	var oneSelected		   	  = false;

	var errorTekst 		   	  = Array();
	errorTekst['tekst']    	  = "- U heeft een of meerdere velden leeg gelaten\n";
	errorTekst['email']    	  = "- Het door u ingevoerde e-mailadres is niet geldig\n";
	errorTekst['postcode'] 	  = "- De door u ingevoerde postcode is niet geldig. Vul de postcode in met notatie 1234AB\n";
	errorTekst['checkbox'] 	  = "- U heeft bij één of meerdere checkboxen, geen checkbox aangevinkt.";
	errorTekst['radio'] 	  = "- U heeft bij één of meerdere radiobuttons, geen radiobutton aangevinkt.";
	
	var soortErrors 	   	  = Array();
	
	for (j=0;j<verplicht.length;j++){
		if(which[verplicht[j][0]]){
			veld  = which[verplicht[j][0]];
			error = false;
			
		    if(verplicht[j][1]=="string" || verplicht[j][1]=="textarea"){
				if(veld.className){
					className 	   = veld.className;
					
					//remove error class if exists
					veld.className = veld.className.replace(" "+errorClass,"");
				}else{
					veld.className = "";
				}
				
				if(veld.value=='' || veld.value==' '){
					error = true;
					empty = true;
					
					if(!soortErrors['tekst']){
						soortErrors['tekst'] = 'tekst';
					}
				}
				if((verplicht[j][0]=='email' || verplicht[j][0]=='e-mail' || verplicht[j][0]=='e-mailadres' || verplicht[j][0]=='emailadres') && !(email.test(veld.value))){
					error = true;
					empty = true;
					
					if(!soortErrors['email']){
						soortErrors['email'] = ['email'];
					}
				}
				/*if(verplicht[j][0]=='postcode'){
					veld.value = veld.value.toUpperCase();		
					alert(veld.value);	
					if (!postcode.exec(veld.value)) {	
						error = true;
						empty = true;
						
						if(!soortErrors['postcode']){
							soortErrors['postcode'] = 'postcode';
						}
					}
				}*/
				
				if(error){
					veld.className = veld.className+' '+errorClass;
					error = false;
				}
			}
			else if(verplicht[j][1]=="email" || verplicht[j][1]=="e-mail"){
				

				if(veld.className){
					className 	   = veld.className;
					//remove error class if exists
					veld.className = veld.className.replace(" "+errorClass,"");
				}else{
					veld.className = "";
				}

				if(veld.value=='' || veld.value==' '){
					error = true;
					empty = true;
					
					if(!soortErrors['tekst']){
						soortErrors['tekst'] = 'tekst';
					}
				}

				if(!(email.test(veld.value))){
					error = true;
					empty = true;
					
					if(!soortErrors['email']){
						soortErrors['email'] = ['email'];
					}					
				}



				if(error){
					veld.className = veld.className+' '+errorClass;
					error = false;
				}

			}				
			else if(verplicht[j][1]=="checkbox" || verplicht[j][1]=="checkboxset"){
				oneSelected = false;
				
				if(veld.length>1){	
					for(i=0; i<veld.length;i++){
						if(veld[i].checked){
							oneSelected = true;
						}
					}
				}
				else{
					if(veld.checked==true){
						oneSelected = true;	
					}
				}	
					
				if(!oneSelected){
					empty = true;		
					if(!soortErrors['checkbox']){
						soortErrors['checkbox'] = 'checkbox';
					}
				}					
			}
			else if(verplicht[j][1]=="radio"){
				oneSelected = false;	
				
				for(i=0; i<veld.length;i++){
					if(veld[i].checked){
						oneSelected = true;
					}
				}	
					
				if(!oneSelected){
					empty = true;		
					if(!soortErrors['radio']){
						soortErrors['radio'] = 'radio';
					}
				}
			}					
		}
	}
	
	if(empty){
		var message = "U heeft de volgende onderdelen van het formulier niet correct ingevoerd:\n";
		for(i in soortErrors){
			if(errorTekst[soortErrors[i]]){
				message += errorTekst[soortErrors[i]];
			}
		}
		alert(message);
		return false;
	} else {
		if(which.spamchecker){
			which.spamchecker.value = "GEEN_SPAM";
		}
		return true;
	}
}
