var zindex = 0;
	$(function(){

		//onhover show pep modules//
		$('a.pepmodules').hover(function(){				
				var pepmodules = $(this).parent().find('div.pepmodules');
				var container = $(this).parent().parent().parent();
				//alert(container.attr('class'));
				container.css('z-index', ++zindex);
				pepmodules.show();
				var top = pepmodules.height() / 2 -12;
				pepmodules.css('top', 0-top);
				pepmodules.find('.arrow').css('top', top);
		}, function(){
				$(this).parent().find('div.pepmodules').hide()
		} );	
		
		$('.coach .contact a.button').click(function(){
				var lastId = $('.coachcontactform').attr("id");
				var newId = $(this).attr('id');
				
				if(lastId != newId){
					reset();
				}
				
				var coachName = $(this).parent().parent().find('h2').html();	
				$('.form.block.coaches h2 span').html(coachName);
				$('.coachcontactform').attr("id",newId);
				openForm();
		});
		

		//CONTACT A COACH//		
		var overlay = $('<div class="overlay"></div>').prependTo("body");		
		overlay.wrap('<div class="overlaycontainer"></div>');
		$('.coachcontactform').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();						 
		})
	});
	
	function windowResize(){
		$('.overlay').height($(document).height());
		$('.overlay').width($(window).width());
		var coachcontactform = $(".coachcontactform");
		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(){
		$('.coachcontactform').fadeOut();
		$('.overlay').fadeOut();
	}
	function openForm(){
		
		$('.coachcontactform').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.coaches").css("width", "").css("height", "");
	$(".coachcontactform").css("width", "");
	windowResize()
}

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

	fieldsToCheck.push({field:$("input[name='bedrijfsnaam']"), validation:"isEmpty"});	
	fieldsToCheck.push({field:$("input[name='contactpersoon']"), validation:"isEmpty"});
	fieldsToCheck.push({field:$("input[name='functie']"), 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)){
		sendEmail();				
	}
}


function sendEmail(){
	var bedrijfsnaam = $("input[name='bedrijfsnaam']").val();
	var contactpersoon = $("input[name='contactpersoon']").val();
	var functie = $("input[name='functie']").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 coachid = $('.coachcontactform').attr("id");
	
	$.ajax({
	  type: "POST",
	  url: BASE_URL+'pepcoaches/sendmail',
	  data: ({
		bedrijfsnaam:bedrijfsnaam,
		contactpersoon:contactpersoon,
		functie:functie,
		adres:adres,
		postcode:postcode,
		plaats:plaats,
		telefoon:telefoon,
		email:email,
		opmerkingen:opmerkingen,
		coachid:coachid
		
	   }),

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


function openThanksScreen(){
	
	$('.form.block h2, .form.block .required, .form.block .fields, .form.block .verstuur').fadeOut(600);
	/*$('.form.block .required').fadeOut(600);
	$('.form.block .fields').fadeOut(600);
	$('.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.coaches").animate({width:thanksWidth+"px", height:thanksHeight + "px"}, {duration:600});
		$(".coachcontactform").animate({left:coachcontactformLeft+"px", top: coachcontactformTop+ "px", width:(thanksWidth+60)+"px"}, {duration:600 });
		setTimeout(function(){
				$('.thankscreen').fadeIn();
				$('.coachcontactform').attr("id","");
				
		},500);
	}, 500);
}
