$(document).ready(function(){
	
	
	//init: Hide & show stuff for people WITH js enabled
	$("form[name='event-booking'] div.cr-row.factuur").addClass('hidden');
	$("form[name='event-booking'] input[name='factuur']").parent().removeClass('hidden');
	$("form[name='event-booking'] input[name='btw']").parent().removeClass('hidden');
	if($("form[name='event-booking'] input[name='btwnr']").attr('value') == ""){
		$("form[name='event-booking'] input[name='btwnr']").parent().addClass('hidden');
	}
	$("form[name='event-booking'] div.cr-left.hidden").removeClass("hidden");
	
	
	
	var unitPrice = $("form[name='event-booking'] span.unitPrice").text();
	var participant = "<tr class='participant'>"+$("form[name='event-booking'] .participant:last").html()+"</tr>";
	$("form[name='event-booking'] .hidden .participants tbody tr").remove();
	$("form[name='event-booking'] select[name='aantal']").val($("form[name='event-booking'] .participants tbody tr").size() + 1);
	
	
	//for all the fields in a container with class 'double', look for field that starts with same
	//name and copy its value in it. except when the value of the duplicate field is (not empty and different).
	 $("form[name='event-booking'] .double input, form[name='event-booking'] .double select").change(function(){
	 	var name = $(this).attr('name');
	 	if(!$("form[name='event-booking'] input[name^='factuur']:checked").val()){
	 		$("form[name='event-booking'] input[name^='"+name+"'], form[name='event-booking'] select[name^='"+name+"']").attr('value',$(this).attr('value'));
	 	}
	 });
	
	
	//make fields hidden / visible if checkbox with same class as fields is changed
	$("form[name='event-booking'] input[type='checkbox']").click(function(i){
		var name = $(this).attr('name');
		
		$('.'+name).toggleClass('hidden');
		
		if(name == "btw"){
			$("form[name='event-booking'] input[name='btwnr']").toggleClass("required");
		}
	});
	
	
	//Add participants fields when number of tickets is higher then one. remove if other way around
	function manageParticipants(number){
		if(number == 1){
			$("form[name='event-booking'] .participants tbody tr").remove();
			$("form[name='event-booking'] .participants").parent().addClass("hidden");
		}else{
			$("form[name='event-booking'] .participants").parent().removeClass("hidden");
			var counter = $("form[name='event-booking'] .participants tbody tr").size() + 1;
			
			
			if (number < counter) {
				while (number < counter) {
					$("form[name='event-booking'] .participants tbody tr:last").remove();
					counter--;
				}
			}
			else {
				while (counter < number) {
					$("form[name='event-booking'] .participants tbody").append(participant);
					$("form[name='event-booking'] .participants tbody tr:last input, form[name='event-booking'] .participants tbody tr:last select").each(function(i){
						var name = $(this).attr('name');
						name = name.substring(0,name.length - 1) + counter;
						$(this).attr('name',name);
						$(this).val("");
					});
					counter++;
				}
			}
		}
	}
	
	
	//add participants fields and an extra ticket when button 'add participant' is clicked
	$("form[name='event-booking'] input[name='addParticipant']").click(function(i){
		$("form[name='event-booking'] select[name='aantal']").val(parseInt($("form[name='event-booking'] select[name='aantal']").val())+1);
		
		changeTickets($("form[name='event-booking'] select[name='aantal']").val());
	});
	
	//auto-calculate total fee when number of tickets is changed
	$("form[name='event-booking'] select[name='aantal']").change(function(i){
		$("span.totalPrice").text(($(this).val() * unitPrice));
		$("span.totalPriceInc").text(($(this).val() * unitPrice) * 1.21);
		
		manageParticipants($(this).val());

	});
	
	function changeTickets(val){
		
		$("span.totalPrice").text((val * unitPrice));
		$("span.totalPriceInc").text((val * unitPrice) * 1.21);
		
		manageParticipants(val);
	}

	
});
