$(document).ready( function() {
	$('.slideContainer').nivoSlider({
		effect : 'fade', // Specify sets like: 'fold,fade,sliceDown'
		boxCols : 8, // For box animations
		boxRows : 4, // For box animations
		animSpeed : 500, // Slide transition speed
		pauseTime : 6000, // How long each slide will show
		directionNav : true, // Next & Prev navigation
		directionNavHide : false, // Only show on hover
		controlNav : true, // 1,2,3... navigation
		controlNavThumbs : false, // Use thumbnails for Control Nav
		keyboardNav : true, // Use left & right arrows
		pauseOnHover : true, // Stop animation while hovering
		manualAdvance : false, // Force manual transitions
		captionOpacity : 1 // Universal caption opacity
	});
	
	$(".fancybox").fancybox({
		'autoScale'			: true,
		'transitionIn'		: 'fade',
		'width'				: '90%',
		'height'			: '75%',
		'padding'			: 0,
		'transitionOut'		: 'fade',
		'type'				: 'iframe'
	});
	
	$('.leesmeertekst').hide();
	
	if($(window).width() < 1060){
		$('.nivo-prevNav').css('left','-40px');
		$('.nivo-nextNav').css('right','-40px');
	}

	$('.leesmeer').click(function(){
		$(this).find('img').fadeOut('fast',function(){
    		if($(this).parent().prev('.leesmeertekst').is(':visible')){
    			$(this).parent().prev('.leesmeertekst').fadeThenSlideToggle('normal',function(elem){
    				$(this).next().find('img').css("margin-left","0px").fadeIn('fast');
    			});
    		} else {
    			$(this).parent().prev('.leesmeertekst').css("opacity","1").show('normal',function(elem){
    				$(this).next().find('img').css("margin-left","-18px").fadeIn('fast');
    			});
    		}
		});
	});
    
	$('#overlay').click(function(){
		closeOverlay();
	});
	
	$('.infiniteCarousel').infiniteCarousel();
    
	$('.triggerPortfolioTags').hover(function(){
		$(this).find('.tags').stop(true,true).animate({'bottom':0},350);
	},function(){
		$(this).find('.tags').stop(true,true).animate({'bottom':'-73px'},350);
	});
    
	
	$('#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');
		}
	});
	
	$('.homePage .blok a img').load(function(){
		var width = $(this).width();
		var offset = (199 - parseFloat(width)) / 2;
		$(this).css("margin-left",offset);
	});
	
	$('.item .imgContainer img').load(function(){
		var height = $(this).height();
		var itemHeight = $(this).parent().parent().height();
		var offset = (parseFloat(itemHeight) - parseFloat(height)) / 2;
		$(this).css("margin-top",offset);
	});
	
	$('.homePage .blok').hover(function(){
		$(this).find('span.arrowRight').css('background-position','0px -102px');
	},function(){
		$(this).find('span.arrowRight').css('background-position','0px -81px');
	});
	
	$('.project').hover(function(){
		$(this).find('span.arrowRight').css('background-position','0px -102px');
	},function(){
		$(this).find('span.arrowRight').css('background-position','0px -81px');
	});
	
	$('.moreInfo a').hover(function(){
		$(this).find('span.arrowRight').css('background-position','0px -102px');
	},function(){
		$(this).find('span.arrowRight').css('background-position','0px -81px');
	});

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

				success: function(result) {
					$('.' + formname).append('<p id="response">' + result + '</p>');
					$('#response').hide();
					$('.' + formname + ' #contactformulier').fadeOut('normal', function() {
						$('#response').fadeIn('normal');
					});
				}
			});
		}
		return false;
	});
	
	
/*	$('#netwerk .netwerkItemImgContainer').hover(function(){
		var elem = $(this).parent();
		var position = $(elem).position();
		$('.hoverItem .textPlaceholder').text(unescape($(elem).attr('rel')));
		$(elem).css('z-index',101).addClass('hovered');
		$('.hoverItem').css({'left':position.left,'top':position.top}).show();
		netwerkImgSubsSlider($(this));
	},function(){});*/
	
/*	$('.hoverItem').mouseleave(function(){
		$('.hovered .netwerkItemSubImg').hide();
		$('.hovered').css('z-index',50).removeClass('hovered');
		$('.hoverItem').hide();
	});*/
});

var i = 0;
function netwerkImgFade(){
	var nrOfItems = $('#netwerk').children('.netwerkItem').size();
	if($('.netwerkItem img[rel=' + i + ']').length > 0){
		$('.netwerkItem img[rel=' + i + ']').fadeIn('normal',function(){
			i++;
			if(i != nrOfItems){
				netwerkImgFade();
			}
		});
	} else {
		i++;
		if(i != nrOfItems){
			netwerkImgFade();
		}
	}
}

jQuery.fn.fadeThenSlideToggle = function(speed, easing, callback){
	return this.fadeTo(speed, 0, easing).slideUp(speed, easing, callback);
};

var visibleImg = 0;
var slideDelay = 0;
function netwerkImgSubsSlider(elem){
	var animationTime = 300;
	var nrOfSubImgs = $(elem).find('.netwerkItemSubImg').size();
	
	if(nrOfSubImgs == 0){}
	else {
		$(elem).find('.netwerkItemSubImg').not('.netwerkItemSubImg:eq(' + visibleImg + ')').delay(slideDelay).fadeOut(animationTime);
		$(elem).find('.netwerkItemSubImg:eq(' + visibleImg + ')').delay(slideDelay).fadeIn(animationTime,function(){
			$(elem).find('.netwerkItemSubImg').not(this).hide();
			visibleImg++;
			if(visibleImg == nrOfSubImgs){
				visibleImg = 0;
			}
			slideDelay = 2000;
			netwerkImgSubsSlider(elem);
		});
	}
}

function loadProjecten(start,amount,cats,klanten){
	$.ajax({
		url: '/models/projectenloader.php',
		type: 'GET',
		data: 'start=' + start + '&amount=' + amount + '&cats=' + cats + '&klant=' + klanten,
		success: function(result) {
			if(start === 0){
				$('.projecten').html(result);
			} else {
				$('.projecten').append(result);
			}
			$('.triggerPortfolioTags').hover(function(){
				$(this).find('.tags').stop(true,true).animate({'bottom':0},350);
			},function(){
				$(this).find('.tags').stop(true,true).animate({'bottom':'-73px'},350);
			});
			$('.project').hover(function(){
				$(this).find('span.arrowRight').css('background-position','0px -102px');
			},function(){
				$(this).find('span.arrowRight').css('background-position','0px -81px');
			});
		}
	});
}

function triggerMens(id){
	$.ajax({
		url: '/models/mensloader.php',
		type: 'GET',
		data: 'id=' + id,
		success: function(result) {
			$('#overlayContainer').html(result);
			$('#overlayContainer, #overlay').fadeIn('fast');
			$('#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 formname = $(this).attr('rel');
				var allRequiredFilled = 'check';
				$('.' + formname + ' .required').each( function() {
					if($(this).val()==='') {
						$(this).removeClass('correct').addClass('error');
						allRequiredFilled = 'error';
					}
				});
				if($('.' + formname + ' .error').length > 0 || allRequiredFilled === 'error' || $('.' + formname + ' #checkbox.verplicht').length > 0 && $('.' + formname + ' #checkbox.verplicht').attr('checked')!=='checked') {
					if($('.' + formname + ' #checkbox.verplicht').length > 0 && $('.' + formname + ' #checkbox.verplicht').attr('checked')!=='checked'){
						$('label[for="checkbox"]').addClass('error');
						$('.' + formname + ' #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 {
					$('.' + formname + ' .loading').show();
					var values = $('.' + formname + ' #contactformulier').serialize();
					$.ajax({
						url: '/formprocess.php?submit=true',
						type: 'POST',
						data: values,

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

function triggerNetwerk(id){
	$.ajax({
		url: '/models/netwerkloader.php',
		type: 'POST',
		data: 'id=' + id,
		success: function(result) {
			$('#overlayContainer').html(result);
			$('#overlayContainer, #overlay').fadeIn('fast');
			netwerkImgSubsSlider($('.hoverItem'));
		}
	});
}

function closeOverlay(){
	$('#overlay').fadeOut();
	$('#overlayContainer').fadeOut().css("overflow-y","hidden");
}

function bulletWidth(){
	var nrOfSlides = $('.slideContainer').find('img').length;
	var bulletWidth = 13;
	$('.nivo-controlNav').css('width', bulletWidth * nrOfSlides);
}

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