$(window).load(function() {
	$('#slider').nivoSlider({
	        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
	        animSpeed:500, // Slide transition speed
	        pauseTime:3000 // How long each slide will show
	    });
});


$(document).ready( function() {
	
	$('#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
	$("#contactsubmit").click( function() {
		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 goBackContact(){
	$("#response").remove();
	$("#contactformulier").show();
}

function validate(){
	var chain = "";
	$(".verplicht").each(function(){
		if(this.type == "text" || this.type == "password" || this.type == "select-multiple" || this.type == "select" || this.type == "file"){
			if ((this.value.length==0) || (this.value==null) || (this.value=="http://")) {
				$(this).css("border","2px dashed #f33").css("background-color","#FF8585");
				chain += this.name;
			} else {
				$(this).css("border","1px solid #ABADB3").css("background-color","#fff");
			}
		} else if (this.type == "checkbox"){
			if ((this.checked == false)) {
				$(this).css("outline","2px dashed #f33");
				chain += this.name;
			} else {
				$(this).css("outline","0");
			}
		}
		else if (this.type == "radio"){
			var name = this.name;
			var path = eval('document.getElementById("bestelform").' + name);
			checked = false;
			for(i=0; i<path.length; i++){
			    if(path[i].checked){
			        checked=true;
			    }
			}
			if ((checked != true)) {
				$(this).css("outline","2px dashed #f33");
				chain += this.name;
			} else {
				$(this).css("outline","0");
			}
		}
	});	
	if(chain == ""){
		document.getElementById('bestelform').submit();
	}
	if(chain != ""){
		alert("Vul a.u.b. de in rood gemarkeerde velden in.");
	}
}

// Placeholder-support
(function($) {
	// Return if native support is available.
	if ("placeholder" in document.createElement("input")) return;

	$(document).ready(function(){
		$(':input[placeholder]').not(':password').each(function() {
			setupPlaceholder($(this));
		});

		$(':password[placeholder]').each(function() {
			setupPasswords($(this));
		});

		$('form').submit(function(e) {
			clearPlaceholdersBeforeSubmit($(this));
		});
	});

	function setupPlaceholder(input) {

		var placeholderText = input.attr('placeholder');

		setPlaceholderOrFlagChanged(input, placeholderText);
		input.focus(function(e) {
			if (input.data('changed') === true) return;
			if (input.val() === placeholderText) input.val('');
		}).blur(function(e) {
			if (input.val() === '') input.val(placeholderText); 
		}).change(function(e) {
			input.data('changed', input.val() !== '');
		});
	}

	function setPlaceholderOrFlagChanged(input, text) {
		(input.val() === '') ? input.val(text) : input.data('changed', true);
	}

	function setupPasswords(input) {
		var passwordPlaceholder = createPasswordPlaceholder(input);
		input.after(passwordPlaceholder);

		(input.val() === '') ? input.hide() : passwordPlaceholder.hide();

		$(input).blur(function(e) {
			if (input.val() !== '') return;
			input.hide();
			passwordPlaceholder.show();
		});

		$(passwordPlaceholder).focus(function(e) {
			input.show().focus();
			passwordPlaceholder.hide();
		});
	}

	function createPasswordPlaceholder(input) {
		return $('<input>').attr({
			placeholder: input.attr('placeholder'),
			value: input.attr('placeholder'),
			id: input.attr('id'),
			readonly: true
		}).addClass(input.attr('class'));
	}

	function clearPlaceholdersBeforeSubmit(form) {
		form.find(':input[placeholder]').each(function() {
			if ($(this).data('changed') === true) return;
			if ($(this).val() === $(this).attr('placeholder')) $(this).val('');
		});
	}
})(jQuery);

function introLogo(){
	$('.intrologo img').delay(500).fadeIn('slow');
}

