$(function(){
  
 $('.scroll-holder').slider({
  orientation: 'vertical',
  value: 100,
  slide: function(event, ui) {
    var $scroll_window = $(ui.handle).parents('.drop-choser').find('.scrolling');
    var ul_h = $(ui.handle).parents('.drop-choser').find('ul').height() + 5;
    var h = ul_h - $scroll_window.height();
    var offset = (100 - ui.value) / 100;
    $scroll_window.scrollTop(h*offset);
  }
 });

 $('form.spec-search-form a.convert').click(function(){
    $(this).siblings('a.convert').removeClass('activated');
    $(this).addClass('activated');
    return false;

  });

  /*
  $('input[name=days]').keyup(function() {
    var st = $("input[name=date_from]").datepicker("getDate");
    if (st && parseInt($(this).val())) {
      var days = parseInt($(this).val());
      $("input[name=date_to]").datepicker("setDate", new Date(st.getTime()+ (days*24*60*60*1000)));
    }
   })
  */
  $('form.spec-search-form span.cal input').each(function(){
    $(this).datepicker({
      dateFormat: 'dd.mm.yy',
      prevText: '',
      nextText: ''
    /*  onSelect: function(dateText, inst) {
        var days = parseInt($('input[name=days]').val());
        var date_from = $('input[name=date_from]').datepicker("getDate");
        var date_to = $('input[name=date_to]').datepicker("getDate");

        if (!date_to && days) {
          $("input[name=date_to]").datepicker("setDate", new Date(st.getTime()+ (days*24*60*60*1000)));
          return;
        } else if(!days && !date_to) {
          return;
        }

        if (date_from && date_to) {
          var dif = date_to.getTime() - date_from.getTime();
          days = dif ? (dif/(1000*60*60*24)) : 0;
          if (days > 0) $('input[name=days]').val(days);
        }
      }*/
    });
  });


  $('form.spec-search-form span.cal').click(function(){

    $(this).find('input').datepicker('show');
    
  });

  var base_value_1 = 0;
   var base_value_2 = 0;
   $('input[name=price_from], input[name=price_to]').keypress(function() {
       base_value_1 = 0;
       base_value_2 = 0;
       $('.currencies .convert').removeClass('activated');
       $('.currencies .convert:first').addClass('activated');
       $('input[name=currency]').val('RUB');
   });



  $('.currencies .convert').click(function(){
    if (!base_value_1) base_value_1 = $('input[name=price_from]').val();
    if (!base_value_2) base_value_2 = $('input[name=price_to]').val();

    var parent = $(this).parent();
    $('.convert', parent).removeClass('activated');

    var from = $('.activated').attr('rel');
    var to = $(this).attr('rel');
    $(this).addClass('activated');

    $('input[name=price_from]').val(convertCurrency(from, to, base_value_1));
    $('input[name=price_to]').val(convertCurrency(from, to, base_value_2));
    $('input[name=currency]').val(to);
    return false;
  })

 $('#rate-s a').click(function(){
      var rate = $('#rate-s');
      var atr = $(this).attr('id');
      (rate).removeClass();
      (rate).addClass('rate-s'+atr);
      (rate).attr('rel', atr);
      var city = $('#selected_city').val();
      var city_id;
      $('#citylist li').each(function(){
        if ($(this).text() == city) {
          city_id = $(this).attr('rel');
        }
      })
      loadHotels(city_id, atr);
      return false;
    });

  initBinding();
  $("input[name=city]").attr('disabled', true);
  $("input[name=hotel]").attr('disabled', true);

});

function initBinding()
{

  $('.drop-body ul li').mouseover(function(){
      $(this).addClass('active-drop');
    }).mouseout(function(){
      $(this).removeClass('active-drop');
    });

    $('.inp-choser').click(function() {
      $(this).parents('.choser-body').find('.drop-choser').hide();
      $(this).parents('fieldset').find('.drop-choser').show();
      if($(this).parents('fieldset').find('.drop-body ul').height() < 181) {

        $(this).parents('fieldset').find('.drop-chos-scroll').hide();
      } else {
        $('.drop-body').parents('fieldset').find('.drop-chos-scroll').show();
      }
      $('#choser-close').show();
    });
 	  $('.choser-closer').click(function() {
       $('body').unbind('click.chooser');
       $(this).parents('.choser-body').find('.drop-choser').hide();
       $('#choser-close').hide();
       return false;
     });
  $('.choser-opener').click(function() {
    $(this).parents('.choser-body').find('.drop-choser').hide();
    $(this).parents('fieldset').find('.drop-choser').show();
    if($(this).parents('fieldset').find('.drop-body ul').height() < 181) {
      $(this).parents('fieldset').find('.drop-chos-scroll').hide();
    } else {
      $('.drop-body').parents('fieldset').find('.drop-chos-scroll').show();
    }
    $('#choser-close').show();
    return false;
  });
  

   $('#choser-close').click(function() {
    $(this).parents('#section-main').find('.drop-choser').hide();
    $('#choser-close').hide();
    return false;
   });

  $('.choser-closer').click(function() {
    $(this).parents('.choser-body').find('.drop-choser').hide();
    $('#choser-close').hide();
    return false;
   });

  $('.choser-opener').click(function() {
    $(this).parents('.choser-body').find('.drop-choser').hide();
    $(this).parents('fieldset').find('.drop-choser').show()
    var a = $(this).parents('fieldset').find('.drop-choser').find('.choser-close');
    $('#choser-close').show();
    $('body').bind('click.chooser',function(e){
      if ($(e.target).parents('.drop-choser').length == 0 && !$(e.target).hasClass('inp-choser')) {
        $('.drop-choser:visible').find('.choser-closer').trigger('click');
      }
    });
    return false;
  });


  $('.inp-choser').click(function() {
    $(this).parents('.choser-body').find('.drop-choser').hide();
    $(this).parents('fieldset').find('.drop-choser').show();
    $('#choser-close').show();
    $('body').bind('click.chooser',function(e){
      if ($(e.target).parents('.drop-choser').length == 0 && !$(e.target).hasClass('inp-choser')) {
        $('.drop-choser:visible').find('.choser-closer').trigger('click');
      }
    });
  });

  $('.drop-body ul li').click(function() {
    var text = $(this).text();
    $(this).parents('fieldset').find('.inp-choser').val($(this).text()).attr('alt',$(this).text()).attr('title',$(this).text());
    $(this).parents('.choser-b1').find('.drop-choser').hide();
    $('#choser-close').hide();
    $('body').unbind('click.chooser');
    return false;
  });

}



function loadCountries(rest_type_id) {
  var url = '/chooser/options/country';
  if (rest_type_id) {
    url += '?rid=' + rest_type_id;
  }

  $.ajax({
      url: url,
      success: function(data){
        if (data) $('#countrylist').html(data);
        else $('#countrylist').html('<li>Все страны</li>');
        
        initBinding();

        $("#countrylist li").click(function(){
          loadCities($(this).attr('rel'));
          $("input[name=city]").attr('disabled', false);
          $("input[name=country]").val($(this).text())
            .attr('title', $(this).attr('title'))
            .attr('alt', $(this).attr('alt'));
         
        });
        
      },
      error: function(){
        $('#countrylist').html('<li>Все страны</li>');
      }
    })
  }

  function loadCities(country_id) {
    var url = '/chooser/options/city';
    if (country_id) {
      url += '?country_id=' + country_id;
    }
    $.ajax({
        url: url,
        success: function(data){
          if (data) $('#citylist').html(data);
          else $('#citylist').html('<li>Все города</li>');
          
          initBinding();

          $("input[name=city]").val($("#citylist li:first").text());
          $("#hotellist li:first").click();
          $("#citylist li").click(function(){
            loadHotels($(this).attr('rel'), null);
            $("input[name=hotel]").attr('disabled', false);
            $("input[name=city]").val($(this).text())
              .attr('title', $(this).attr('title'))
              .attr('alt', $(this).attr('alt'));
          });
          
        },
        error: function(){
          $('#citylist').html('<li>Все города</li>');
        }
      })

  }

  function loadHotels(city_id, star) {
    var url = '/chooser/options/hotel?city_id=' + city_id;
    if (star) {
      url += '&star=' + star;
    }
    $.ajax({
        url: url,
        success: function(data){
          if (data) $('#hotellist').html(data);
          else $('#hotellist').html('<li>Все отели</li>');

          initBinding();
          
          $("input[name=hotel]").val($("#hotellist li:first").text());
          $("#hotellist li").click(function(){
            $("input[name='hotel']").val($(this).text())
              .attr('title', $(this).attr('title'))
              .attr('alt', $(this).attr('alt'));
          });
         
        },
        error: function(){
          $('#citylist').html('<li>Все города</li>');
        }
      })
  }

function loadRestTypes() {
  var url = '/chooser/options/resttypes';
  $.ajax({
      url: url,
      success: function(data){
        if (data) $('#resttypelist').html(data);
        else $('#resttypelist').html('<li>Все виды</li>');

        initBinding();

        $("input[name=rest_type]").val($("#resttypelist li:first").text());

        $("#resttypelist li").click(function(){
          loadCountries($(this).attr('rel'));
          
          $("input[name=rest_type]").val($(this).text())
            .attr('title', $(this).attr('title'))
            .attr('alt', $(this).attr('alt'));
          
        });
        
      },
      error: function(){
        $('#resttypelist').html('<li>Все виды</li>');
      }
    })
}

