/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		"left": windowWidth/2-popupWidth/2
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}

//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
		
		fadeOutBanner();
		
		function fadeInBanner(){
			$("#banner").fadeIn(1200, function(){
				fadeOutBanner();
				});
		}
		
		function fadeOutBanner(){
			$("#banner").fadeOut(1200, function(){
				fadeInBanner();			
				});
			}
	
	$("#form").bind("submit", function(){
		if ($("input[name='company']:checked").val() != 0){
			$("input[name='company_name']").addClass("required");
			$("input[name='kvk']").addClass("required");
		}
		if ($("input[name='company']:checked").val() == 0){
			$("input[name='company_name']").removeClass("required");
			$("input[name='kvk']").removeClass("required");
		}
	});
	
	$("#form").validate({
			debug: false,
			rules: {
				last_name: "required",
				city: "required",
				title: "required",
				initials: "required",
				birth_date: "required",
				mobile: "required",
				address: "required",
				address_number: "required",
				postal_code: "required",
				country: "required",
				email: {
					required: true,
					email: true
				},
			},
			messages: {
				last_name: "Dit is een verplicht veld.",
				company_name: "Dit is een verplicht veld.", 
				city: "Dit is een verplicht veld.",
				title: "Dit is een verplicht veld.",
				initials: "Dit is een verplicht veld.",
				birth_date: "Dit is een verplicht veld.",
				mobile: "Dit is een verplicht veld.",
				address: "Dit is een verplicht veld.",
				address_number: "Dit is een verplicht veld.",
				postal_code: "Dit is een verplicht veld.",
				country: "Dit is een verplicht veld.",
				kvk: "Dit is een verplicht veld.",
				email: "Dit is geen geldig Email-adres.",
			},
		});
	
	//LOADING POPUP
	//Click the button event!
	$("a#order").click(function(e){		
		
		e.preventDefault();
		
		centerPopup();
		
		loadPopup();
		
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});
