$(document).ready(function($){

	$('#overviewAndPaymentForm button[type=submit]').click(function() {
		$('#overviewAndPaymentForm #formaction').val($(this).attr('rel'));
	});

	$('#form-sort').change(function(){
		this.form.submit();
	});

	$('.children_age li').each(function(i){
		var elem = $(this);
		elem.hide();
		elem.find('select').attr('disabled', 'disabled');
	});
	$('.childage_label').hide();

	$('.person_selection').each(function(i){
		//bookingform.html --> class="StuckOnHP"
		if( !$(this).hasClass('StuckOnHP') ){
			if(i > 0 ){
				$(this).hide();
				$(this).find('select').attr('disabled', 'disabled');
			}
		}
	});

	$('.children_amount').change(function(e) {
		var amount = parseInt($(e.target)[0].value);
		if(amount == 0){
			$(e.target).parents('.person_selection').find('.childage_label').hide();
		}else{
			$(e.target).parents('.person_selection').find('.childage_label').show();
		}

		$(e.target).parents('.person_selection').find('ul.children_age li').each(function(i){
			var elem = $(this);

			if(i > amount){
				elem.find('select').attr('disabled','disabled');
				elem.hide();
			}else{
				elem.find('select').removeAttr('disabled');
				elem.show();
			}
		});

	});

	$('#hotel_room_amount').change(function(e){
		var amount = parseInt($(e.target)[0].value);

		$('.person_selection').each(function(i){
			var elem = $(this);

		//bookingform.html --> class="StuckOnHP"
		if( !elem.hasClass('StuckOnHP') ){
			if(i >= amount ){
				elem.find('select').attr('disabled', 'disabled');
				elem.hide();
			}else{
				elem.find('select').removeAttr('disabled');
				elem.find('select.children_amount').change();
				elem.show();
			}
		}//endif
		});

	});


	$('.children_amount').change();
	$('#hotel_room_amount').change();

	//$('#searchfilter').hide();
	$('#detaillist_link').click(function(){
		$('#searchfilter').slideToggle('fast', function(){
			var link = $('#detaillist_link');
			if(link.hasClass('detaillist_link')){
				link.removeClass('detaillist_link');
				link.addClass('detaillist_link_minus');
			}else{
				link.removeClass('detaillist_link_minus');
				link.addClass('detaillist_link');
			}

		});

		return false;
	});

	$('#rueckfahrt').change(function(e){
		if($(this).attr('checked')){
			$('#rueckfahrt_form').find('input').removeAttr('disabled');
			$('#rueckfahrt_form').show();
		}else{
			$('#rueckfahrt_form').find('input').attr('disabled', 'disabled');
			$('#rueckfahrt_form').hide();
		}
	});

	$('#rueckfahrt').change();

	// validation method "group_required"
	// requires one or more inputs of a group to be filled
	// enclose group of inputs with class "require_group"
	jQuery.validator.addMethod('group_required', function(val, el) {
		var module = $(el).closest('.require_group');
		return module.find('.group_required:filled').length;
	}, 'Please fill out at least one of these fields');


	// initialize general form validation
	$('form.validate').each(function() {
		var form = this;
		
		var validator = $(form).validate({ 
			errorElement: "span",
			messages:{
				"tx_wagbooking_pi1[accommodationSearchFilter][dateFrom]": "",
				"tx_wagbooking_pi2[vacancyFilter][dateFrom]": "",
				"tx_wagbooking_pi2[vacancyFilter][dateTo]": "",
				"tx_wagbooking_pi13[filter][dateArrival]": "",
				"tx_wagbooking_pi14[collectionFilter][startDate]": "",
				"tx_wagbooking_pi14[collectionFilter][endDate]": "",
				"tx_wagbooking_pi15[filter][startDate]": "",
				"tx_wagbooking_pi17[collectionFilter][startDate]": "",
				/* Contact form, selectdate fields */
				/* DE */
				"tx_powermail_pi1[uid182]": "",
				"tx_powermail_pi1[uid198]": "",
				"tx_powermail_pi1[uid157]": "",
				"tx_powermail_pi1[uid159]": "",
				/* EN */
				"tx_powermail_pi1[uid253]": "",
				"tx_powermail_pi1[uid255]": "",
				"tx_powermail_pi1[uid305]": "",
				"tx_powermail_pi1[uid307]": "",
				/* FR */
				"tx_powermail_pi1[uid278]": "",
				"tx_powermail_pi1[uid280]": "",
				"tx_powermail_pi1[uid580]": "",
				"tx_powermail_pi1[uid582]": "",
				/* IT */
				"tx_powermail_pi1[uid383]": "",
				"tx_powermail_pi1[uid385]": "",
				"tx_powermail_pi1[uid410]": "",
				"tx_powermail_pi1[uid412]": "",
				/* SV */
				"tx_powermail_pi1[uid513]": "",
				"tx_powermail_pi1[uid515]": "",
				"tx_powermail_pi1[uid540]": "",
				"tx_powermail_pi1[uid542]": "",
				/* NL */
				"tx_powermail_pi1[uid448]": "",
				"tx_powermail_pi1[uid450]": "",
				"tx_powermail_pi1[uid475]": "",
				"tx_powermail_pi1[uid477]": ""
			}
		});
		$(form).data('validator', validator);
	});

	// initialize serviceAvailability links
	$('a.availabilityLink').click(function(event) {

		if( typeof availabilityCalendarClose != 'undefined' ){
			if( $(this).html() == availabilityCalendarOpen ){
				$(this).html( availabilityCalendarClose );
			}else{
				$(this).html( availabilityCalendarOpen );
			}
		}


			$(this).closest("span").next('p').next(".availabilityDatePicker").toggle();
			return false;

	});

});

