$(function(){

  // слой с номером скидки
  $('#discount-link').click(function(e){
    e.preventDefault();
    $('#diskont').slideToggle(function(){
      $('#order_discount_id').focus();
    });
  });

  // вкладки с шагами
  var default_margin = {
    1: parseInt($('#list1').css('margin-left')),
    2: parseInt($('#list2').css('margin-left')),
    3: parseInt($('#list3').css('margin-left'))
  };
  $('.show').click(function(e){
    e.preventDefault();
    var $parent = $(this).parent(),
        id = parseInt($parent.attr('id').substr(4));
    // прячем ссылку по которой кликнули
    $(this).fadeOut();
    $('.leftblock .close').fadeOut();
    // выдвигаем
    $('.close', $parent).fadeIn();
    $parent.animate({ marginLeft: default_margin[id]+280 });
    // выдвигаем все что правее
    $parent.nextAll().each(function(){
      var id = parseInt(this.id.substr(4));
      $('.show', this).fadeIn();
      $(this).animate({ marginLeft: default_margin[id]+280 });
    });
    // задвигаем все что левее
    $parent.prevAll().each(function(){
      var id = parseInt(this.id.substr(4));
      $('.show', this).fadeIn();
      $(this).animate({ marginLeft: default_margin[id] });
    });
  });
  $('.leftblock .close').click(function(e){
    e.preventDefault();
    $(this)./*hide*/fadeOut();
    $('.leftblock > div').each(function(){
      var id = parseInt(this.id.substr(4));
      $('.show', this)./*show*/fadeIn();
      $(this).animate({ marginLeft: default_margin[id] }, function(){

      });
    });
  });

  // отмечаем опции
  $('.sorting_list :checkbox').click(function(){
    var input = $(this).parent().next().children('input');
    if ($(this).is(':checked')) {
      input.removeAttr('disabled');
    } else {
      input.attr('disabled', 'disabled');
    }
  });

  // текстовые блоки в оформлении заказа
  $('#left-info dd').hide();
  $('#left-info dt a').click(function(e){
    e.preventDefault();
    $(this).parent().next().slideToggle();
  });

  // показываем фильтр
  $('.dop-options .show-filter').click(function(e){
    e.preventDefault();
    $('.dop-options .filter').show();
    $('.dop-options .description').hide();
  });

  // сам фильтр
  var filter_morph = [1, 1, 0]; // это типа морфологии, 1 - женский род, 0 - мужской
  $('#links2 select').each(function(i){
    $(this).after('<a href="#" id="dop-options'+i+'">'+(this.selectedIndex ? this[this.selectedIndex].text : any_value[filter_morph[i]])+'</a>');
    $('.dop-options').append('<div class="options" id="options'+i+'"><div><ul></ul></div><div class="lt"></div><div class="rt"></div><div class="lb"></div><div class="rb"></div><img src="/images/blank.gif" height="18" width="18" class="close" /></div>');
    $('option', this).each(function(){
      $('#options'+i+' ul').append('<li><a href="#" rel="'+this.value+'">'+(this.text ? this.text : any_value[filter_morph[i]])+'</a></li>');
    });
    $(this).hide();
  });
  $('#links2 a').live('click', function(e){
    e.preventDefault();
    var id = this.id.substr(11);
    $('.options').hide();
    $('#options'+id).show();
  });
  $('.options .close').live('click', function(){
    $(this).parent().hide();
  });
  $('.options a').live('click', function(){
    var id = $(this).parents('.options').hide().attr('id').substr(7);
    $('.dop-options select').eq(id).val(this.rel).next().text($(this).text());
  });

  // сбросить фильтр
  /*$('#dop-options-reset').click(function(e){
    e.preventDefault();
    $('#links2 select').val('');
    $('#links2 a').text(any_value);
  });*/

  // пересчитываем опции
  var max_count = 2;
  var calc_option = function() {
    var total = parseInt($('.auto-price').text());
    $('.list_position').each(function(){
      var count = $('.count:enabled', this);
      count = (count.length && count.val() && parseInt(count.val()) > 0) ?
                (max_count && parseInt(count.val()) > max_count ?
                  max_count :
                  parseInt(count.val())
                ) :
                0;
      var sum = count * parseInt($('.price', this).text());
      $('.sum', $(this).prev()).text(sum);
      total += sum;
    });
    $('.total').text(total);
  };
  $('.list_position .buttons :checkbox').click(calc_option);
  $('.list_position .count').keyup(calc_option).change(function(){ this.value = this.value && parseInt(this.value) > 0 ? (max_count && parseInt(this.value) > max_count ? max_count : parseInt(this.value)) : 0; calc_option(); });

  // открываем оплату во фрейме
  $('#payment-form').submit(function(e){
    $('#payment-frame').load(function(){
                          $('#payment-frame-shadow').hide();
                          $('#payment-frame-loader').hide();
                        })
                       .show();
    $('#payment-frame-shadow').css({ opacity: 0.7 })
                              .show();
    $('#payment-frame-loader').show();
    $('#reservation-payment-translate').show();
    $(':submit', this).hide();
  });
});
