$(function () {
  // var location_text = "Введите место...";
  
  // $("#search_start_date, #search_end_date").datepick({
  //   // beforeShow:      customRange,
  //   showOn:          'both', 
  //   buttonImageOnly: true, 
  //   buttonImage:     '/images/calendar.gif', 
  //   buttonText:      'Календарь', 
  //   prevText:        '<<', 
  //   nextText:        '>>', 
  //   dateFormat:      'yy-mm-dd',
  //   yearRange:       '2009:2015',
  //   regional:        ['ru']
  // });
  // 
  $('#gallery a.lightbox').colorbox({
    slideshow: true,
    slideshowStart: (locale == 'ru' ? 'Старт' : 'Start'), 
    slideshowStop:  (locale == 'ru' ? 'Стоп' : 'Stop'), 
    current: '{current} ' + (locale == 'ru' ? 'из' : 'from') + ' {total}', 
    previous: 'предыдущее',
    next:     'следующее',
    close:    'закрыть'
  });
  // 
  // function customRange(input) {  
  //   return {minDate: (input.id == "search_end_date" ? 
  //     $("#search_start_date").datepick("getDate") : null),  
  //     maxDate: (input.id == "search_start_date" ? 
  //     $("#search_end_date").datepick("getDate") : null)};  
  // }

  var customRange = function(input) {
    var d1 = $('#search_start_date').datepick('getDate');
    var d2 = $('#search_end_date').datepick('getDate');
    var today = (new Date());

    if (d1) d1.setDate(d1.getDate() + 1);
    if (d2) d2.setDate(d2.getDate() - 1);
    
    // alert('Element: ' + input.id);

    return {
      minDate: (input.id == 'search_end_date' ? d1 : today)
      // maxDate: (input.id == 'search_start_date' ? d2 : null)
    }
  }
  
  var dateSelected = function(input) {
    var d1 = $('#search_start_date').datepick('getDate');
    var d2 = $('#search_end_date').datepick('getDate');

    if (d1) d1.setDate(d1.getDate() + 1);
    if (!d2 || d2 < d1) $('#search_end_date').datepick('setDate', d1);
  }

  $.datepick.setDefaults({showOn: 'both', buttonImageOnly: true, buttonImage: 'http://i.expoua.com/images/calendar-icon.gif', buttonText: 'Calendar', beforeShow:customRange, onSelect:dateSelected, prevText: '<<', nextText: '>>', dateFormat: 'dd-mm-yy'});

  $(".datefield").datepick({
    showOn:          'both', 
    buttonImageOnly: true, 
    buttonImage:     '/images/calendar.gif', 
    // buttonText:      'Календарь', 
    // closeText: 'Закрыть',
    closeStatus: '',
    prevText: '&lt;&lt;',  prevStatus: '',
    nextText: '&gt;&gt;', nextStatus: '',
    // currentText: 'Сегодня',
    currentStatus: '',
    // monthNames: [
    //   'Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'
    // ],
    // monthNamesShort: ['Янв', 'Фев', 'Мар', 'Апр', 'Май', 'Июн', 'Июл', 'Авг', 'Сен', 'Окт', 'Ноя', 'Дек'],
    monthStatus: '', yearStatus: '',
    // weekHeader: 'Не',
    weekStatus: '',
    // dayNames: ['воскресенье', 'понедельник', 'вторник', 'среда', 'четверг', 'пятница', 'суббота'],
    // dayNamesShort: ['вск', 'пнд', 'втр', 'срд', 'чтв', 'птн', 'сбт'],
    // dayNamesMin: ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'],
    dayStatus: 'DD', dateStatus: 'D, M d',
    dateFormat: 'dd/mm/yy', yearRange: '2009:2012',
    initStatus: '', isRTL: false        
  });

  $("#_sort-mode").change(function() {
    if ($(this).val() == 'location') {
      $("#_location").val('').show().focus();
    } else {
      $("#_location").blur().hide();
    }
  });
  
  // $("#_location").focus(function() {
  //   if ($(this).val() == location_text) {
  //     $(this).val("");
  //   }
  // });
  // 
  // $("#_location").blur(function() {
  //   if (!$(this).val()) {
  //     $(this).val(location_text);
  //   }
  // });
  
  $("#_selector").submit(function() {
    $("#_location").focus();
  });
  
  $("#_sort-mode").change();

  $(".entry a.closed").live("click", function() {
    $(this).removeClass('closed').addClass('opened'); // $(".entry a.opened").click(closedHandler);
    $(this).parents("tr").next(".block-closed").removeClass("block-closed").addClass("block-opened");
    return false;
  });
  
  $(".entry a.opened").live("click", function() {
    $(this).removeClass('opened').addClass('closed'); // $(".entry a.closed").click(openedHandler);
    $(this).parents("tr").next(".block-opened").removeClass("block-opened").addClass("block-closed");
    return false;
  });
  
  // $("#search_country_id").change(loadRegionsList);
  $("#search_region_id").change(loadCitiesList);
  
  loadCitiesList();
});

// function loadRegionsList() {
//   $.getJSON('/' + locale + '/partner/profiles/regions', { country_id: $('#search_country_id').val(), region_id: $('#region_id').val() }, function(data){
//     buildList('search_region_id', $('#region_id').val(), data)
//     loadCitiesList();
//   });
// }

function loadCitiesList() {
  $.getJSON('/' + locale + '/booking/hotels/cities', { region_id: $('#search_region_id').val(), city_id: $('#city_id').val() }, function(data){
    buildList('search_city_id', $('#city_id').val(), data);
  });
}

function buildList(id, selected, data) {
  var select = $('#' + id);
  select.html('');
  // var option = $("<option/>").attr("value", '').text('Выберите, пожалуйста, город').appendTo("#" + id);
  $.each(data, function(i, item){
    option = $("<option/>");
    option.attr("value", item.id).text(item.name);
    if (item.id == selected) {
      option.attr('selected', 'selected');
    }
    option.appendTo("#" + id);
  });
}