	function childrenOptions(number) {
		var options = "";
		for (var i=0; i < number+1; i++) {
			options += "<option>" + i + "</option>";
		};
		return options;
	}
  function childrenAgeOptions() {
    var childrenCount = parseInt($('#children').val());
    if (childrenCount < 1) {
       $('#childrenAge').overlay().close(); 
    } else {
       var ages = '<option value="0"><1</option><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option><option value="5">5</option><option value="6">6</option><option value="7">7</option><option value="8">8</option><option value="9">9</option><option value="10">10</option><option value="11">11</option><option value="12">12</option></select>';

      $('#childrenAge').find('select').remove().end().find('.newListSelected').remove();

      for (var i=0; i < childrenCount; i++) {
        $('<select class="childrenAgeTemp" ' + 'id="prs' + i + '">' + ages).insertAfter('#childrenAge br');
      };
      
      $('#childrenAge').find('select').sSelect()
        .end()
          .find('ul').css({
            height : '100px',
            top : '20px' 
          })
        .end()
        .overlay().load();
    };
  }
  
  $(document).ready(function(){
    // overlay for extra children ages
    $('#childrenAge').overlay({
      // custom  position 
      top: 100,  
      left: 160,
      closeOnClick: false
   	});
	  
  	$('#adults')
  	  .sSelect() // grafische select boxen mit jquery.stylish-select.min.js
  	  .change(function(){ 
        $('#childrenAge')
			.find('select').remove() //alle selects entfernen
			.end()
			.find('.newListSelected').remove() //alle stylisch selects entfernen
			.end()
			.overlay().close(); // overlay ausblenden 
			
  			var childrenAvailable = 4 - parseInt($(this).val()); // calc the remaining children
  			var options = childrenOptions(childrenAvailable);
			
  			$('#children')
  			  .children().remove()
  			.end()
  			  .append(options).resetSS();
  			  
  			// put the changefunction again, because resetSS() removes it
  			$('#children').change(function(){
  			  childrenAgeOptions();   // adds number of children into ovelay 
        });
		}); 
		
  	$('#children')
  	  .sSelect()
  	  .change(function(){
        childrenAgeOptions();// adds number of children into ovelay
     });

		
		// jquery datepicker 
		Date.format = 'yyyy-mm-dd';
 		$('.date-pick').datePicker();

		// abreise autmatisch 1 tag später als anreise möglich
		$('#calArrivalDateField').bind(
			'dpClosed',
			function(e, selectedDates)
			{
			  $("#bform").validate().form();
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#calDepartureDateField').dpSetStartDate(d.addDays(1).asString());
				}
			}
		);
		$('#calDepartureDateField').bind(
			'dpClosed',
			function(e, selectedDates)
			{
			  $("#bform").validate().form();
				var d = selectedDates[0];
				if (d) {
					d = new Date(d);
					$('#calArrivalDateField').dpSetEndDate(d.addDays(-1).asString());
				}
			}
		);
	// formular overlay ein - und ausblenden
	$("a#closeButton").click().toggle(function() {
		$(this).find('img').attr({ src: 'fileadmin/designmaster/images/add.gif' });
	  $('#bookingForm')
	    .find('form')
	      .hide()
	    .end()
	    .find('.opa')
	     .animate({ height: '22px' }, 100)
	    .end()
	    .find('.header')
	     .show()
	     .animate({
     			opacity: 1.0,
     			filter: 'alpha(opacity=100)'
   			}, 300)
	}, function() {
		$(this).find('img').attr({ src: 'fileadmin/designmaster/images/close.gif' });
	    $('#bookingForm')
	      .find('.opa')
	        .animate({ height: '263px'},100)
	      .end()
	      .find('.header')
	       .css({
       			opacity: 0,
       			filter: 'alpha(opacity=0)' 
     		  })
     		.end() 
     		.find('form')
     		  .show()
	});
			
	// validation (in class vom inputfeld)
	$("#bform").validate();
	
	// show and hide default values
	$(".defaultText").focus(function(srcc)
	    {
	        if ($(this).val() == $(this)[0].title)
	        {
	            $(this).removeClass("defaultTextActive");
	            $(this).val("");
	        }
	    });

	    $(".defaultText").blur(function()
	    {
	        if ($(this).val() == "")
	        {
	            $(this).addClass("defaultTextActive");
	            $(this).val($(this)[0].title);
	        }
	    });
		$(".defaultText").focus();
	    $(".defaultText").blur();
	
	
	$("#bform").submit(function() { 
	  // set childrenage 
		var childrenAgeTemp = []; 
		var selchildrenAgeTemp = $("select.childrenAgeTemp"); 
		if (selchildrenAgeTemp.length > 0) {
			$("select.childrenAgeTemp").each(function(index){
				childrenAgeTemp[index] = $(this).val();
			});
			$('input[name=childrenAges1]').val(childrenAgeTemp.join(","));
		}; 

	  // adpixel laden
	  if ($(this).valid()) {
	     // load ad pixel
       var img = new Image();
       $(img).attr('src','http://ad.yieldmanager.com/pixel?id=726269&t=2');
 	     // on submit clear default values 
	     $(".defaultText").each(function() {
  			if($(this).val() == $(this)[0].title) {
  				$(this).val("");
  			}
  		});
	  };
		
	});
	
});