$(document).ready(function() {
	$(document).pngFix();
	$('body').addClass('domenabled');
	$("#footer ul li:last").css("border","none");
	
	// Check Safari
	if($.browser.safari){
		$("#branding").css('background', 'none');
	}
	
	// FAQ animation
	$('.faq li a').click(function() {
		$('.faq li div').hide();
		$(this).next().show();
	});
	
	// Set height
	setFooter();
	$(window).resize(function(){ 
		setFooter();
	});
	
	// Fancybox
	if($('body').hasClass("referenties")){
		$("a.fancybox").fancybox({ 
			'zoomSpeedIn': 0,
			'zoomSpeedOut': 0,
			'overlayShow': true 
		});
	}
	
	// Validate the form
	// Form elements
	$('form input.button').after('<span class="submit button"></span>');
	$('div.extra').hide();
	
	$("select").change(function() {
		var selectedId = $("select option:selected").attr('id');
		if(selectedId == 'question'){
			$('div.extra').slideDown();
		}
		else if(selectedId == 'call'){
			$('div.extra:visible').slideUp();
		}
		else{
			$('div.extra:visible').slideUp();
		}
	});

	// Prefill form fields
	$('input.text, textarea').focus(function(){
		$(this).css('color','#000000');
	});
	
	if($('#contactForm').length == 1){
		formValidate();	
	}
	openInNewWindow();
});

function formValidate(){
	// validate contact form on keyup and submit
	$("#contactForm").validate({
		//set the rules for the fild names
		rules: {
			sirname: {
				required: true,
				minlength: 2
			},
			street: {
				required: true,
				minlength: 2
			},
			zipcode: {
				required: true,
				rangelength: [6, 6]
			},
			city: {
				required: true,
				minlength: 2
			},
			email: {
				required: true,
				email: true
			},
			phone: {
				required: true,
				number: true,
				rangelength: [10, 10]
			},
		
		//set messages to appear inline
		messages: {
				sirname: "Verplicht",
				street: "Verplicht",
				zipcode: "Verplicht",
				city: "Verplicht",
				email: "Verplicht",
				phone: "Verplicht"
			}
		}
	});
}

function setFooter() {
	var desiredHeight = $(window).height() - ($("#footer").height()) ;
	if(($("#content").height() + $("#footer").height()) < $(window).height()){
		$("#content").css("height", desiredHeight); /*-276*/
	}
}

function openInNewWindow() {
	$("a[rel='blank']").each(function() {
		$(this).attr({
			href: "javascript:window.open('" + $(this).attr("href") + "');void(0);", 
			title: "Link opent in een nieuw venster"
		});
	});
}
