$(document).ready( function() {
	
	$('#afhalenop').datepicker({  minDate: 0, dateFormat: 'dd-mm-yy', nextText: 'Volgende maand', prevText: 'Vorige maand', dayNamesMin: ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'], monthNames: ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'] });
	
	$('#banner').nivoSlider({
        effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
        boxRows: 3,
        boxCols: 10,
        animSpeed: 500, // Slide transition speed
        pauseTime: 4000, // How long each slide will show
        directionNav: false, // Next & Prev navigation
        controlNav: false, // 1,2,3... navigation
        keyboardNav: true, // Use left & right arrows
        pauseOnHover: false, // Stop animation while hovering
    });
	
	$('#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');
		}
	});

	$('.productWithImage').hover(function(){
		var elem = $(this);
		$(this).data('timeout', setTimeout( function () {
			$(elem).parent().parent().find('img.productImage').fadeIn(300);
		}, 300));
	},function(){
		clearTimeout($(this).data('timeout'));
		$(this).parent().parent().find('img.productImage').fadeOut(300);
	});
	
// 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;
	});
	
	$(".cartVeld").blur(function(){
		$('.updateButton').css("outline","2px dashed #f33");
	});
	
	$(".bestelTypeVeld").blur(function(){
		$('.updateButton').css("outline","2px dashed #f33");
	});
});

function showRond(elem){
	if($(elem).val() === 'in de winkel'){
		$('.rondSpan').show();
		$('.rondSpan').find('input').addClass('verplicht');
	}
	else {
		$('.rondSpan').hide();
		$('.rondSpan').find('input').removeClass('verplicht');
	}
}

function bestelProduct(elem){
	var aantal = $(elem).parent().parent().find('.bestelAantalVeld').val();
	var type = $(elem).parent().parent().find('.bestelTypeVeld').val();
	var id = $(elem).parent().parent().attr('id');
	isAlpha = /[(A-Z)|(a-z)]/ ;
	if(isAlpha.test(aantal) || aantal.length < 1) {
	  alert('Geef a.u.b. een geldig aantal op.') ;
	} else {
		$.ajax({
		   type: 'POST',
		   url: '/cart/add/',
		   data: 'quantity=' + aantal + '&productid=' + id + '&type=' + type,
		   success: function(){
				$('#cart').show();
				var totaal = $('.winkelMandItems').text();
				totaal = parseFloat((totaal * 1) + 1); 
				$('.winkelMandItems').html(totaal);
				$(elem).parent().parent().find('td:first .confirmerPlaceholder').append('<span class="confirmer">Dit product is in de winkelwagen geplaatst.</span>');
				var element = $(elem).parent().parent().find('.confirmer');
				var timeout = setTimeout(function(){hideElement(element)},2000);
		   }
		 });
	}
}

function hideElement(elem){
	$(elem).fadeOut();
}

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.");
	}
}

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

// 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);
