$(document).ready( function() {
	$.backstretch("img/bg_pagina_home.jpg", {speed:700});
	adjustToWidth();
	$(window).resize( function() {
	 	adjustToWidth();
    });
	
	
	$('.menuLink').click(function(){
		$('.menuContainer').slideToggle().toggleClass('up');
		if($('.menuContainer').hasClass('up')){
			$(this).find('.right').html('&ndash;');
		} else {
			$(this).find('.right').html('+');
		}
	});
	
	$('#contactform .required').blur(function(){
		if($(this).val() !== ''){
			var inputVal = $(this).val();
			var inputId = $(this).attr('id');
			$(this).removeClass('correct').addClass('error');
			if(inputId==='email'){
				var regExMail = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
				if(!regExMail.test(inputVal)){ return false; }
			} else if(inputId==='phone' || inputId==='fax'){
				var regExPhone = /^((((0031)|(\+31))(\-)?6(\-)?[0-9]{8})|(06(\-)?[0-9]{8})|(((0031)|(\+31))(\-)?[1-9]{1}(([0-9](\-)?[0-9]{7})|([0-9]{2}(\-)?[0-9]{6})))|([0]{1}[1-9]{1}(([0-9](\-)?[0-9]{7})|([0-9]{2}(\-)?[0-9]{6}))))$/;
				if(!regExPhone.test(inputVal)){ return false; }
			} else if(inputId==='postcode'){
				var regExPostcode = /^[1-9][0-9]{3}\s?[a-zA-Z]{2}$/;
				if(!regExPostcode.test(inputVal)){ return false; }
			} else if(inputId==='huisnummer'){
				var regExHuisnummer = /^([0-9]){1,}([A-Z]){0,3}/;
				if(!regExHuisnummer.test(inputVal)){ return false; }
			} else {}
			$(this).removeClass('error').addClass('correct');
		} else {
			$(this).removeClass('correct').addClass('error');
		}
	});
	
});

//Contact submit
function contactSubmit() {
	var allRequiredFilled = 'check';
	$('#contactform .required').each( function() {
		if($(this).val()==='') {
			$(this).removeClass('correct').addClass('error');
			allRequiredFilled = 'error';
		}
	});
	if($('#contactform .error').length > 0 || allRequiredFilled === 'error' || $('#contactform #checkbox.verplicht').length > 0 && $('#contactform #checkbox.verplicht').attr('checked')!=='checked') {
		if($('#contactform #checkbox.verplicht').length > 0 && $('#contactform #checkbox.verplicht').attr('checked')!=='checked'){
			$('label[for="checkbox"]').addClass('error');
			$('#contactform #checkbox.verplicht').click(function(){
				if ($(this).is(':checked')){
					$('label[for="checkbox"]').removeClass('error');
					$(this).removeClass('error').addClass('correct');
				}
			});
		} else {
			$('label[for="checkbox"]').removeClass('error');
		}
	} else {
		$('#contactform .loading').show();
		var values = $("#contactformulier").serialize();
		$.ajax({
			url: '/formprocess.php?submit=true',
			type: 'POST',
			data: values,

			success: function(result) {
				$('#contactform').append('<p id="response">' + result + '</p>');
				$('#response').hide();
				$('#contactformulier').fadeOut('normal', function() {
					$('#response').fadeIn('normal');
				});
			}
		});
	}
	return false;
}

/*function viewImpressieFoto(elem){
	var imgSrc = $(elem).attr('src');
	$('#impressieOverlay').fadeIn();
	$('#impressieFoto').attr("src",imgSrc).show();
}*/

/*function viewImpressieFoto(elem){
	var imgSrc = $(elem).attr('src');
	$.backstretch(imgSrc, {speed:700});
}*/

function changePage(page,elem,knopClass){
	$('#impressieOverlay').hide();
	$('#impressieFoto').hide();
	if(page!=='lunchkaart' && page!=='dinerkaart'){
		if($('.menuContainer').is(':visible')){
			$('.menuContainer').slideUp('normal');
			$('.menuLink .right').html('+');
		}
		$('#nav li,.menuContainer a').removeClass('current');
		$(elem).parent().addClass('current');
	} else {
		$('#nav li,.menuContainer a').removeClass('current');
		$(elem).addClass('current');
	}
	if(page!== 'route'){
		var imgSrc = 'img/bg_pagina_' + page + '.jpg';
		$.backstretch(imgSrc, {speed:700});
		$('#map-canvas:visible').fadeTo('normal',0.01,function(){$(this).hide();});
		if($('.content').is(':visible')){
			$('.content').load(page + '.php',function(){
				$("a.impressieImageLink").fancybox({
					overlayColor: '#000',
					overlayOpacity: 0.9
				});
			});
		} else {
			$('.content').fadeIn().load(page + '.php',function(){
				$("a.impressieImageLink").fancybox({
					overlayColor: '#000',
					overlayOpacity: 0.8
				});
			});
		}
		if(!$('#footer').is(':visible')){
			$('#footer').fadeIn();
		}
		if($('.logoRoute').is(':visible')){
			$('.logoRoute').fadeOut();
			$('.logo').fadeIn();
		}
	} else if(page==='route'){
		if ( $.browser.msie && $.browser.version < 9 ) {
			$('.content').fadeOut('normal',function(){
				$('.content').load('ie8map.php').fadeIn();
			});
		} else {
			if($('#map-canvas').length === 0){
				$('body').append('<div id="map-canvas"></div>');
				initializeMap();
				$('.content').fadeOut('normal',function(){
					$('#map-canvas').show().fadeTo('normal',1);
					$('.gmnoprint').css("z-index",99);
				});
			} else {
				$('.content').fadeOut('normal',function(){
					$('#map-canvas').show().fadeTo('normal',1);
				});
			}
		}
		if($('#footer').is(':visible')){
			$('#footer').fadeOut();
		}
		if($('.logo').is(':visible')){
			$('.logo').fadeOut();
			$('.logoRoute').fadeIn();
		}
	}
}

function initializeMap() {
	var mapDiv = document.getElementById('map-canvas');
	var map = new google.maps.Map(mapDiv, {
		center : new google.maps.LatLng(51.3277195, 5.9814998),
		zoom : 15,
		mapTypeId : google.maps.MapTypeId.ROADMAP,
		mapTypeControl : true,
		mapTypeControlOptions : {
			style : google.maps.MapTypeControlStyle.DROPDOWN_MENU
		},
		navigationControl : true,
		navigationControlOptions : {
			style : google.maps.NavigationControlStyle.SMALL,
			position : google.maps.ControlPosition.TOP_RIGHT
		}
	});
	var infoWindow = new google.maps.InfoWindow({
		position : map.getCenter(),
		content : '<h1>Restaurant de Tafelaer</h1>Raadhuisstraat 11<br/>5981 BA Panningen<br/>077 3747155'
	});
	infoWindow.open(map);
}

function adjustToWidth(){
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	if(windowWidth > 1150){
		$('.content').css('left','29%');
	} else if(windowWidth < 1151){
		$('.content').css('left','328px');
	}
	$('.impressieContainer').css('height',windowHeight);
}

function goBackContact(){
	$("#response").remove();
	$("#contactformulier").show();
}
