var zindex = 0;
var submitted = false;

	$(function(){

		//CONTACT A COACH//		
		var overlay = $('<div class="overlay"></div>').prependTo("body");		
		overlay.wrap('<div class="overlaycontainer"></div>');
		$('.vacaturecontactform').insertAfter(overlay);
		
		windowResize();
		$(window).bind("resize", windowResize);
		$('.overlay').animate({opacity:.8}, {duration:500});
		var opmerkingenHeight = $('#opmerkingen').height();
		$('#opmerkingen').keyup(function(){
									 
			if(opmerkingenHeight != $(this).height()){
				windowResize();
				opmerkingenHeight = $(this).height();
			 }
			 
		});
		
		$('.overlay, .sluiten').click(function(){
			closeForm();						 
		})
		$('.verstuur').click(function(){
			validateForm();						 
		})
		$('.openform').click(function(){
			openForm();						 
		})
	});
	
	function windowResize(){
		$('.overlay').height($(document).height());
		$('.overlay').width($(window).width());
		var coachcontactform = $(".vacaturecontactform");
		var coachcontactformTop = ($(window).height() - coachcontactform.height()) / 2;
		var coachcontactformLeft = ($(window).width() - coachcontactform.width()) / 2;
		coachcontactform.css("top", coachcontactformTop+ "px");
		coachcontactform.css("left", coachcontactformLeft+ "px");
		
	}
	
	function closeForm(){
		$('.vacaturecontactform').fadeOut();
		$('.overlay').fadeOut();
	}
	function openForm(){		
		$('.vacaturecontactform').fadeIn();
		$('.overlay').fadeIn();
	}

//VALIDATE FORM ////

function reset(){	
	$("input, textarea").val("");
	$(".field").removeClass("error");
	$('.form.block h2, .form.block .required, .form.block .fields, .form.block .verstuur').show();
	$('.thankscreen').hide();
	$(".form.vacatures").css("width", "").css("height", "");
	$(".vacaturecontactform").css("width", "");
	windowResize()
}

function validateForm(){
	
	var fieldsToCheck = new Array();

	fieldsToCheck.push({field:$("input[name='naam']"), validation:"isEmpty"});	
	fieldsToCheck.push({field:$("input[name='adres']"), validation:"isEmpty"});
	fieldsToCheck.push({field:$("input[name='postcode']"), validation:"isEmpty"});
	fieldsToCheck.push({field:$("input[name='plaats']"), validation:"isEmpty"});
	fieldsToCheck.push({field:$("input[name='telefoon']"), validation:"isPhone"});
	fieldsToCheck.push({field:$("input[name='email']"), validation:"isEmail"});
	
	if(!validate(fieldsToCheck)){
		$('.verstuur').unbind('click');		
		$('.verstuur').animate({opacity:.5}, {duration:500});
		
		if($('#uploadify').uploadifySettings('queueSize') == 0){
			sendEmail();	
		}else{
			$('.wait').show();
			submitted = true;
			
		}
	}
}


function sendEmail(){

	var vacaturetitle = $(".nieuwsheader h2").html();
	var naam = $("input[name='naam']").val();
	var adres = $("input[name='adres']").val();
	var postcode = $("input[name='postcode']").val();
	var plaats = $("input[name='plaats']").val();
	var telefoon = $("input[name='telefoon']").val();
	var email = $("input[name='email']").val();
	var opmerkingen = $("textarea[name='opmerkingen']").val();
	var file1 = $('.file:eq(0)').attr('name');
	var file2 = $('.file:eq(1)').attr('name');
	var file3 = $('.file:eq(2)').attr('name');
	var file4 = $('.file:eq(3)').attr('name');
	
	
	$.ajax({
	  type: "POST",
	  url: BASE_URL+'vacatures/sendmail',
	  data: ({
		vacaturetitle:vacaturetitle, 
		naam:naam,
		adres:adres,
		postcode:postcode,
		plaats:plaats,
		telefoon:telefoon,
		email:email,
		opmerkingen:opmerkingen,
		foldername: FOLDERNAME,
		file1:file1,
		file2:file2,
		file3:file3,
		file4:file4
	   }),

	  success: function(data) {		  		
		openThanksScreen();
	  }
	});
}


function openThanksScreen(){
	
	$('.form.block h2, .form.block .required, .form.block .fields, .form.block .verstuur').fadeOut(600);

	setTimeout(function(){
		var thanksWidth = 450;
		var thanksHeight = 120;
		var coachcontactformTop = ($(window).height() - thanksHeight) / 2;
		var coachcontactformLeft = ($(window).width() - thanksWidth) / 2;
		$(".form.vacatures").animate({width:thanksWidth+"px", height:thanksHeight + "px"}, {duration:600});
		$(".vacaturecontactform").animate({left:coachcontactformLeft+"px", top: coachcontactformTop+ "px", width:(thanksWidth+60)+"px"}, {duration:600 });
		setTimeout(function(){
				$('.thankscreen').fadeIn();
				$('.vacaturecontactform').attr("id","");
				
		},500);
	}, 500);
}

