$(window).load(function(){ pagina_instellen();});
$(window).resize(function(){ pagina_instellen(); });

//Variabelen voor de homeheader
var fotoheader_huidige_slide;
var fotoheader_aantal_slides;
var fotoalbum_huidige_slide;
var fotoalbum_aantal_slides;

$(document).ready(function(){		
	pagina_instellen();	
	
	//Divs doorlinken naar een item
	$("#producten_pagina .content .inhoud .productgroep").click(function(){ window.location=$("#basepath").attr("value")+$(this).attr("data-url"); });
	
	//Menu opvrolijken met een border aan de rechterkant van het laatste hoofdmenuitem
	$("#menu>ul>li").last().addClass("laatste");
	
	//Om en om kleuren van tabellen
	$("#producten_pagina table tr:odd").css("background-color", "#e6e6e6");
	$("#producten_pagina table tr:even").css("background-color", "#f6f6f6");
	
	//Print deze pagina knop werking
	$(".print_link").click(function(){
		window.print();
		return false;
	});
	
	//Twitterfeed op de homepagina innerfaden
	$("#home_pagina .content .twitter .items").innerfade({
		speed: 1500,
		timeout: 5000,
		type: 'sequence',
		containerheight: '18px'
	});
	
	//Zijkant foto's
	$(".zijkant .fotoslideshow").innerfade({
		speed: 1500,
		timeout: 5000,
		type: 'sequence',
		containerheight: '160px'
	});
	
	//Pagina headers
	$("#normale_pagina .header, #nieuws_pagina .header, #producten_pagina .header").innerfade({
		speed: 1500,
		timeout: 5000,
		type: 'sequence',
		containerheight: '110px'
	});
	
	//////////////////////////////////////////////////////////
	//	HOMEHEADER SLIDER									//			
	//////////////////////////////////////////////////////////
	fotoheader_huidige_slide=1;
	fotoheader_aantal_slides=$("#home_pagina .header .slider img").length;
	//Automatische roulatie
	if(fotoheader_aantal_slides>1){	start_header_fotoslider(); }
	 //stoppen en starten
	$("#home_pagina .header").mouseover(function() { 
		if($("#home_pagina .header .slider img[rel=\""+fotoheader_huidige_slide+"\"]").attr("data-url")==""){ $(this).css("cursor","default"); } else { $(this).css("cursor","pointer"); }	
		if(fotoheader_aantal_slides>1){ stop_header_fotoslider(); } 
	}).mouseout(function(){
		if(fotoheader_aantal_slides>1){ start_header_fotoslider(); } 
	});
	//Koppelingen via de klik op de header als de link gevuld is
	$("#home_pagina .header").click(function(){ if($("#home_pagina .header .slider img[rel=\""+fotoheader_huidige_slide+"\"]").attr("data-url")!=""){ window.location=$("input#basepath").attr("value")+$("#home_pagina .header .slider img[rel=\""+fotoheader_huidige_slide+"\"]").attr("data-url"); } });
	//Werking van de navigatieknoppen
	$("#home_pagina .header .navigatie a").click(function(){
		if(($(this).index()+1)!=fotoheader_huidige_slide){
			fotoheader_huidige_slide=($(this).index()+1);
			beweeg_fotoheader_naar_slide(fotoheader_huidige_slide);
		}
		return false;
	});
	
	
	//////////////////////////////////////////////////////////
	//	HOMEBLOKKEN WERKING BIJ KLIK						//			
	//////////////////////////////////////////////////////////
	//Pointers als cursur geven als er te linken valt
	$("#home_pagina .content .blokken .blok").each(function(){
		if($(this).attr("data-url")!=""){ $(this).addClass("klikbaar") }
	});	
	$("#home_pagina .content .blokken .klikbaar").click(function(ev){
		window.location=$("#basepath").attr("value")+$(this).attr("data-url");
	});
	
	//////////////////////////////////////////////////////////
	//	PRODUCTENGROEPEN FOTO WERKING						//			
	//////////////////////////////////////////////////////////
	//Pointers als cursur geven als er te linken valt
	$("#producten_pagina .content .inhoud .productgroep").mouseover(function(){
		$(".foto",this).attr("src",$(".foto",this).attr("src").replace("_zwartwit.jpg", ".jpg"));
		$("a",this).addClass("hover");
	}).mouseout(function(){
		$(".foto",this).attr("src",$(".foto",this).attr("src").replace(".jpg","_zwartwit.jpg"));
		$("a",this).removeClass("hover");
	});
	
	
	//////////////////////////////////////////////////////////
	//	PRODUCTEN UITKLAPPEN WERKING						//			
	//////////////////////////////////////////////////////////
	//Handmatig naar actieve product springen als er eentje actief is en geselecteerd door de furl
	if($("#geselecteerd_product").attr("value")!=""){
		if($("#producten_pagina .content .inhoud .product").hasClass("actief")){
			new_position = $("#producten_pagina .content .inhoud .product.actief").offset();
			window.scrollTo(0,new_position.top);
		}
	}
	$("#producten_pagina .content .inhoud .product .titel").click(function(){
		//Als de geklikte actief is dan alleen deze inklappen, anders ook de overige dichtklappen
		if($(this).parent(".product").hasClass("actief")){
			$("#producten_pagina .content .inhoud .product.actief .tekst").slideUp(300, function(){
				pagina_instellen();
			});
			$("#producten_pagina .content .inhoud .product.actief").removeClass("actief");
		} else {
			$("#producten_pagina .content .inhoud .product.actief .tekst").slideUp(300, function(){
				pagina_instellen();
			});
			$("#producten_pagina .content .inhoud .product.actief").removeClass("actief");
			$(this).parent().addClass("actief");
			$(this).next().slideDown(300, function(){
				pagina_instellen();	
				if($("#producten_pagina .content .inhoud .product").hasClass("actief")){
					new_position = $("#producten_pagina .content .inhoud .product.actief").offset();
					window.scrollTo(0,new_position.top);
				}	
			});
		}	
			
	});
	
	//////////////////////////////////////////////////////////
	//	WISSELEN TUSSEN ACTUEEL EN ARCHIEF NIEUWS			//			
	//////////////////////////////////////////////////////////
	$("#nieuws_pagina .content .zijkant .nieuwswisseling").click(function(ev){
		//Omwisselen van actieve lijst aan nieuwsberichten 
		if($("#nieuws_pagina .content .zijkant .laatste_nieuws").hasClass("actief")){
			$("#nieuws_pagina .content .zijkant .laatste_nieuws").removeClass("actief");			
			$("#nieuws_pagina .content .zijkant .archief_nieuws").addClass("actief");
			$("#nieuws_pagina .content .nieuwsblok.recent").slideUp(250);
			$("#nieuws_pagina .content .nieuwsblok.archief").slideDown(250, function(){
					pagina_instellen();	
			});
		} else {
			if($("#nieuws_pagina .content .zijkant .archief_nieuws").hasClass("actief")){
				$("#nieuws_pagina .content .zijkant .archief_nieuws").removeClass("actief");			
				$("#nieuws_pagina .content .zijkant .laatste_nieuws").addClass("actief");
				$("#nieuws_pagina .content .nieuwsblok.archief").slideUp(250);
				$("#nieuws_pagina .content .nieuwsblok.recent").slideDown(250, function(){
					pagina_instellen();	
				});
			} 
		}
		return false;
	});
	
	//////////////////////////////////////////////////////////
	//	TEAMPAGINA, WISSELING VAN ACTIEVE PERSOON			//			
	//////////////////////////////////////////////////////////
	$(".teamwidget .titel").click(function(){
		//Alleen wisselen als de persoon wordt aangeklikt die nog niet actief is
		if(!($(this).hasClass("actief"))){
			$(".teamwidget .titel.actief").next().slideUp(200);
			$(".teamwidget .titel.actief").removeClass("actief");
			$(this).next().slideDown(200,function(){				
				pagina_instellen();	
			});
			$(this).addClass("actief");
			//Naar de juiste foto navigeren
			mederwerker=$(this).attr("data-nummer");
			$(".zijkant .teamfotos .slider").animate({left:"-"+((mederwerker-1)*300)+"px"},{queue:false,duration:350,easing:'easeInOutExpo'});
		}
	});
	
	
	//////////////////////////////////////////////////////////
	//	ROUTEBESCHRIJVING									//			
	//////////////////////////////////////////////////////////
	
	$('a[href="#getdirections"]').click(function(e) {
		e.preventDefault();
		var adres = $('#maps-adres').val();
		var plaats = $('#maps-plaats').val();
		if(adres != '' && plaats != ''){
			getDirections(adres+', '+ plaats);
		} else {
			alert('Please fill in all the fields');
		}
	});
	$('#maps-plaats').keyup(function(e){
		if(e.keyCode == 13) {
			$('a[href="#getdirections"]').click();
		}
	});
	$(".routewidget #directions").resize(function(){ pagina_instellen(); });
	
	//////////////////////////////////////////////////////////
	//	AUTOMATISCH INGEVULDE INPUTBOXEN WERKING			//			
	//////////////////////////////////////////////////////////
	//Alle input text boxen afgaan of er een default waarde moet worden ingevuld als deze leeg is en data-default is ingevuld
	$("input[type=text],textarea").each(function(){
		if(typeof  $(this).attr("data-default")!="undefined"){
			if($(this).attr("value")==""){ 
				$(this).attr("value", $(this).attr("data-default"));
				$(this).css("color", "#787878");
			}	
		}			
	});	
	//Werking van de inputboxen bij een klik
	$("input[type=text],textarea").focus(function(){
		if(typeof $(this).attr("data-default")!="undefined"){
			if($(this).attr("value")==$(this).attr("data-default")){
				$(this).attr("value", "");
				$(this).css({"color":"#000000"});
			}
		}
	}).focusout(function(){
		if(typeof  $(this).attr("data-default")!="undefined"){
			if($(this).attr("value")==""){
				$(this).attr("value", $(this).attr("data-default"));
				$(this).css({"color":"#787878"});
			}
		}
	});

	//On submit van een formulier alle tekstvelden met nog de default waarde in leeg maken
	$("form").submit(function(){
		$("input[type=text]").each(function(){
			if($(this).attr("value")==$(this).attr("data-default")){ $(this).attr("value", ""); }		
		});
		$("textarea").each(function(){
			if($(this).attr("value")==$(this).attr("data-default")){ $(this).attr("value", ""); }		
		});
	});	
});

//Algemene instellingen instellen voor de pagina
function pagina_instellen(override){
	//Hoogte onderdelen website gelijk trekken
	if(typeof $("#normale_pagina .content .zijkant").css("padding-bottom")!="undefined"){
		$("#normale_pagina .content .zijkant").css("padding-bottom","75px");
		$("#normale_pagina .content .inhoud").css("padding-bottom","15px");
		hoogte_zijkant=$("#normale_pagina .content .zijkant").outerHeight();
		hoogte_content=$("#normale_pagina .content .inhoud").outerHeight();
		if(hoogte_zijkant<hoogte_content){
			padding=$("#normale_pagina .content .zijkant").css("padding-bottom").replace("px","");
			verschil=hoogte_content-hoogte_zijkant;
			$("#normale_pagina .content .zijkant").css("padding-bottom",(Number(padding)+verschil)+"px");
		} else {
			padding=$("#normale_pagina .content .inhoud").css("padding-bottom").replace("px","");
			verschil=hoogte_zijkant-hoogte_content;
			$("#normale_pagina .content .inhoud").css("padding-bottom",(Number(padding)+verschil)+"px");
		}
	}	
	if(typeof $("#nieuws_pagina .content .zijkant").css("padding-bottom")!="undefined"){
		$("#nieuws_pagina .content .zijkant").css("padding-bottom","75px");
		$("#nieuws_pagina .content .inhoud").css("padding-bottom","15px");
		hoogte_zijkant=$("#nieuws_pagina .content .zijkant").outerHeight();
		hoogte_content=$("#nieuws_pagina .content .inhoud").outerHeight();
		if(hoogte_zijkant<hoogte_content){
			padding=$("#nieuws_pagina .content .zijkant").css("padding-bottom").replace("px","");
			verschil=hoogte_content-hoogte_zijkant;
			$("#nieuws_pagina .content .zijkant").css("padding-bottom",(Number(padding)+verschil)+"px");
		} else {
			padding=$("#nieuws_pagina .content .inhoud").css("padding-bottom").replace("px","");
			verschil=hoogte_zijkant-hoogte_content;
			$("#nieuws_pagina .content .inhoud").css("padding-bottom",(Number(padding)+verschil)+"px");
		}
	}		
	if(typeof $("#producten_pagina .content .zijkant").css("padding-bottom")!="undefined"){
		$("#producten_pagina .content .zijkant").css("padding-bottom","75px");
		$("#producten_pagina .content .inhoud").css("padding-bottom","15px");
		hoogte_zijkant=$("#producten_pagina .content .zijkant").outerHeight();
		hoogte_content=$("#producten_pagina .content .inhoud").outerHeight();
		if(hoogte_zijkant<hoogte_content){
			padding=$("#producten_pagina .content .zijkant").css("padding-bottom").replace("px","");
			verschil=hoogte_content-hoogte_zijkant;
			$("#producten_pagina .content .zijkant").css("padding-bottom",(Number(padding)+verschil)+"px");
		} else {
			padding=$("#producten_pagina .content .inhoud").css("padding-bottom").replace("px","");
			verschil=hoogte_zijkant-hoogte_content;
			$("#producten_pagina .content .inhoud").css("padding-bottom",(Number(padding)+verschil)+"px");
		}
	}	
	//Hoogte pagina instellen
	$("#sitecontainer").attr("style","");
	hoogte_venster=$(window).height();
	hoogte_website=$("#sitecontainer").outerHeight();
	if(hoogte_website<hoogte_venster){
		$("#sitecontainer").css("height",hoogte_venster+"px");
	}
	
}


function start_header_fotoslider(){
	header_fotoslider="";
	window.clearInterval(header_fotoslider);
	header_fotoslider=setInterval(function(){
		if(fotoheader_huidige_slide==fotoheader_aantal_slides){ fotoheader_huidige_slide=1; } else { fotoheader_huidige_slide++; }
		beweeg_fotoheader_naar_slide(fotoheader_huidige_slide);		
	},7500);
}

function stop_header_fotoslider(){
	window.clearInterval(header_fotoslider);	
}

function beweeg_fotoheader_naar_slide(slide){
	$("#home_pagina .header .slider").animate({marginLeft:-(980*(slide-1))+"px"},{queue:false,duration:750,easing:'easeInOutExpo'});
	$("#home_pagina .header .info").slideUp(250,function(){
		$(".scroll .dummy",this).html("<h1>"+$("#home_pagina .header .slider img[rel=\""+slide+"\"]").attr("data-titel")+"</h1>"+$("#home_pagina .header .slider img[rel=\""+slide+"\"]").attr("data-tekst"));		
		$(this).slideDown(250);	
		//Navigatie aanpassen
		$("#home_pagina .header .navigatie a").html("<img src=\"images/icoon.bol.wit.png\" alt=\"navigation\" title=\"navigation\">");
		$("#home_pagina .header .navigatie a:eq("+(slide-1)+")").html("<img src=\"images/icoon.bol.rood.png\" alt=\"navigation\" title=\"navigation\">");
		$("#home_pagina .header .navigatie .huidige").html(slide);
	});
	//Aanpassen van de huidige slider	
	fotoheader_huidige_slide=slide; 	
}

var directionDisplay;
var map;
function doGoogleMaps() {
  directionsDisplay = new google.maps.DirectionsRenderer({draggable:true, suppressMarkers:true});
  var geocoder = new google.maps.Geocoder();
  if(geocoder) {
    geocoder.geocode({'address':'Stationstraat 30, Nuth'}, function(results, status) {
      if(status == google.maps.GeocoderStatus.OK) {
        var gLat = results[0].geometry.location.lat();
        var gLng = results[0].geometry.location.lng();
      }
      var latlng = new google.maps.LatLng(gLat, gLng);
      var myOptions = {zoom:14, center:latlng, mapTypeId:google.maps.MapTypeId.ROADMAP};
      map = new google.maps.Map(document.getElementById('maps'), myOptions);
      new google.maps.Marker({position:latlng, map:map});
      directionsDisplay.setMap(map);
      directionsDisplay.setPanel(document.getElementById("directions"));
    });
  }

}
function getDirections(address){	
	var directionsService = new google.maps.DirectionsService();
	directionsService.route({origin:address, destination:'Stationstraat 30, Nuth', provideRouteAlternatives:false, travelMode:google.maps.DirectionsTravelMode.DRIVING, unitSystem:google.maps.DirectionsUnitSystem.METRIC}, function(result, status){
		if(status == google.maps.DirectionsStatus.OK){
			directionsDisplay.setDirections(result);
			var geocoder = new google.maps.Geocoder();
			if(geocoder){
				geocoder.geocode({'address':address}, function(results, status){
					if(status == google.maps.GeocoderStatus.OK){
						var gLat = results[0].geometry.location.lat();
						var gLng = results[0].geometry.location.lng();
					}
					var latlng2 = new google.maps.LatLng(gLat, gLng);
					new google.maps.Marker({position:latlng2, map:map});
				});
			} 
		} else {
			alert('Your adress "' + address + '" can\'t be found. Please check your adress for spelling mistakes.');
		}
	});	
}



