$(document).ready(function(){
  document.getElementById('search_form').reset();
  $(function()  {
    $("#date_from").datepicker({  dateFormat: 'dd.mm.y', minDate: '+1', showAnim: ''});
    $("#date_till").datepicker({  dateFormat: 'dd.mm.y', minDate: '+1', showAnim: ''});
  });
  
  if($('#hotel_list').length) {
    $('ul.star input').click(function(){
      hotel_refresh();
    });
    $('#region_list').change(function(){
      hotel_refresh();
    });
  }
  
  if($('#region_list').length)
    var region_name = '#region_list';
  else
    var region_name = '#region';
  $('#country').change(function(){
    $(region_name).hide();
    $('#all_region_block').hide();
    $('#ajax_loader_region').show();
    hotel_refresh();
    $.post(location.href, { 'action':'get_region', 'country_id':  $('#country').val()}, function(data) {
      $(region_name).html(data);
      $('#ajax_loader_region').hide();
      $(region_name).show();
      $('#all_region_block').show();
    });
    return false;
  });
  
  $('#children').change(function(){
    var child_count = $('#children').val();
    if(child_count == 0) {
      $('div.child_age-select').hide();
      $(':input.age_small[id^="child"]').attr('disabled', 'disabled');
    } else {                           
      $(':input.age_small[id^="child"]').attr('disabled', '').hide();
      if(child_count == 1) {
        $('#child1_age').attr('disabled', '').show();
      } 
      if(child_count == 2) {
        $('#child1_age').attr('disabled', '').show();
        $('#child2_age').attr('disabled', '').show();
      }
      if(child_count == 3) {
        $('#child1_age').attr('disabled', '').show();
        $('#child2_age').attr('disabled', '').show();
        $('#child3_age').attr('disabled', '').show();
      }
      $('div.child_age-select').show();
    }
  });

});

function search_form_submit(form_type) {
  $('div.btn-search :input').attr('disabled', 'disabled');
  var hotel_rating_str  = '';
  $('ul.star :checkbox:checked').each(function(index){
    hotel_rating_str  += $(this).val()+' ';
  });
  $('#hotel_rating').val($.trim(hotel_rating_str));

  var food_str = '';
  $('ul.food :checkbox:checked').each(function(index){
    food_str += $(this).val()+' ';
  });
  $('#food').val($.trim(food_str));

  if(form_type == 'extended') {
    var region_list = '';
    $('#region_list  option:selected').each(function(index){
      region_list  += $(this).val()+' ';
    });
    $('#region').val($.trim(region_list));
    var hotel_list = '';
    $('#hotel_list option:selected').each(function(index){
      hotel_list  += $(this).val()+' ';
    });
    $('#hotel').val($.trim(hotel_list));
    $('#hotel_list').val('');
    $('#region_list').val('');
  }

  $('ul.star input').attr('checked', '');
  $('ul.food input').attr('checked', '');
  $('#search_form').submit();
  return false;
}

function hotel_refresh(){
  $('#hotel_list').hide();
  $('#ajax_loader_hotel').show();
  var region_list = '';
  $('#region_list option:selected').each(function(index){
    region_list  += $(this).val()+' ';
  });
  var hotel_rating_list  = '';
  $('ul.star :checkbox:checked').each(function(index){
    hotel_rating_list  += $(this).val()+' ';
  });
  $.post(location.href, { 'action':'get_hotel', 'region_id':  $.trim(region_list), 'country_id':  $('#country').val(), 'hotel_rating_id': $.trim(hotel_rating_list)}, function(data) {
    $('#hotel_list').html(data);
    $('#ajax_loader_hotel').hide();
    $('#hotel_list').show();
  });
}

function all_region_click(state){
  if(state){                                     
    $('#region_list').val('');
    $('#region_list').css('opacity', '0.4');
  } else {
    $('#region_list').css('opacity', '1');
  }
}

function all_hotel_click(state){
  if(state){                                     
    $('#hotel_list').val('');
    $('#hotel_list').css('opacity', '0.4');    
  } else {
    $('#hotel_list').css('opacity', '1');
  }
}

function hotel_list_click(){
  $('#hotel_list').css('opacity', '1');
  $('#all_hotel').attr('checked', false);
}

function region_list_click(){
  $('#region_list').css('opacity', '1');
  $('#all_region').attr('checked', false);
}
