
$(document).ready(function() {

   /**
    * Event Handlers
    */
   function id_meal_change()
   {
        if ($("#id_meal").val() == 'other'){
            $('#meals').fadeIn('slow');
        } else {
            $('#meals').hide();
        } 
   }

   function air_fare_quote_change()
   {
        if ($("input[name='air_fare_quote']:checked").val() == 'yes') {
            $('#departure_info').fadeIn('slow');
        } else {
            $('#departure_info').hide();
        }        
   }

   function modify_itinerary_change()
   {
        if ($("input[name='modify_itinerary']:checked").val() == 'yes') {
            $('#itinerary_changes').fadeIn('slow');
            $('#itinerary_flexible').fadeIn('slow');
        } else {
            $('#itinerary_changes').hide();
            $('#itinerary_flexible').hide();
        } 
   }
   
   function modify_tour_change()
   {
        if ($("input[name='modify_tour']:checked").val() == 'yes') {
            $('#tour_changes').fadeIn('slow');
            $('#private_tours').hide();
        } else {
            $('#private_tours').fadeIn('slow');
            $('#tour_changes').hide();
        }        
   }
   
   function custom_before_change()
   {
      if ($("input[name='custom_before']:checked").val() == 'yes') {
            $('#agent').fadeIn('slow');
      } else {
            $('#agent').hide();
      }         
   }

   // run the change events once on each page load to set
   // the initial state and/or restore the state after 
   // clicking 'back' or failing validation
   air_fare_quote_change();
   modify_tour_change(); 
   modify_itinerary_change();
   custom_before_change();
   id_meal_change();
   
   /**
    * Event binding
    */
   $("input[name='custom_before']").change(function(e){
        e.preventDefault();
        custom_before_change();
   });   
         
   $("input[name='modify_tour']").change(function(e){
        e.preventDefault();
        modify_tour_change();
   });
   
   $("input[name='modify_itinerary']").change(function(e){
        e.preventDefault();
        modify_itinerary_change();
   });

   $("input[name='air_fare_quote']").change(function(e){
        e.preventDefault();
        air_fare_quote_change();
   });

   $('#id_meal').change(function(e){
        e.preventDefault();
        id_meal_change();
   });
 
   // scroll to the quote form because layout changes after
   // document loads causes confusing jump upwards on page
   if(location.href.indexOf('#request-quote') > -1)
   {
       var targetOffset = $('#request-quote').offset().top;
       $('html,body').animate({scrollTop: targetOffset}, 500);
   }

});
