$(document).ready(function () {
  OFACatalogItem.init(OFA_OFFERS_MATRIX);

  function showSimplyPopup(id_size) {
    $('body').find('.simply_popup_frame').remove();
    $('body').append('<div class="simply_popup_frame popup"></div>');

    $('.simply_popup_frame').jqm({
      trigger: '.simply_popup_frame.popup',
      onLoad: function (hash) {
        onLoadjqm(name, hash);
      },
      ajax: arOptimusOptions["SITE_DIR"] + 'ajax/size_info.php?id_size=' + id_size
    });

    $('.simply_popup_frame').jqmShow();
  }

  $(document).on('click', '.ofa-size-info', function (event) {
    var id_info = $(this).attr('data-id-size-info');
    showSimplyPopup(id_info);
  });

  $(document).on('click', '.ofa-prop1-block .items > SPAN', function (event) {
    var prop1 = $(this).attr('data-prop1');
    OFACatalogItem.setCurrentProp1(prop1);
  });

  $(document).on('click', '.ofa-prop2-block .items > SPAN', function (event) {
    var prop2 = $(this).attr('data-prop2');
    OFACatalogItem.setCurrentProp2(prop2);
  });

  $(document).on('click', '.ofa-detail-to-cart-block .minus', function (event) {
    var
      $input = $(this).parent().find('.count_items'),
      offer = $input.attr('data-offer'),
      value = parseInt($input.val());

    if (value > 1) {
      value = value - 1;
    }

    $input.val(value);
    $('.ofa-button-buy-block .to-cart[data-item="' + offer + '"]').attr('data-quantity', value);

  });

  $(document).on('click', '.ofa-detail-to-cart-block .plus', function (event) {
    var
      $input = $(this).parent().find('.count_items'),
      offer = $input.attr('data-offer'),
      value = parseInt($input.val());

    if (value < OFACatalogItem.getCurrentOfferQuantity()) {
      value = value + 1;
    }

    $input.val(value);
    $('.ofa-button-buy-block .to-cart[data-item="' + offer + '"]').attr('data-quantity', value);
  });

  $(document).on('click', '.ofa_right_info SPAN.to-cart', function (event) {
    $(this).parent().find('SPAN.to-cart').hide();
  });

});

OFACatalogItem = {
  currentProp1: false,
  currentProp2: false,
  currentOffer: false,
  offerMatrix: [],

  init: function (offerMatrix) {
    this.offerMatrix = offerMatrix;
    this.setDefaultOffer();
  },

  getCurrentOfferQuantity: function () {
    return parseInt(this.currentOffer['QUANTITY']);
  },

  getCurrentProductYear: function () {
    return parseInt(this.currentOffer['IS_CURRENT_YEAR']);
  },

  setDefaultOffer: function () {
    if (OFA_OFFERS_IS_MATRIX) {
      //Если свойств 2
      //Проверим и затемним первое свойство, у которых нет вообще вариантов
      for (var key in this.offerMatrix) {
        var
          is_have_offers = false,
          prop2Values = this.offerMatrix[key];

        for (var key2 in prop2Values) {
          var
            prop2Value = prop2Values[key2],
            quantity = parseInt(prop2Value.QUANTITY);

          if (quantity > 0) {
            is_have_offers = true;
          }
        }

        if (!is_have_offers) {
          $('#prop1_' + key).addClass('no-variants');
        }
      }


      //Ищем первый товар, который есть в наличии
      for (var key in this.offerMatrix) {
        var prop2Values = this.offerMatrix[key];
        for (var key2 in prop2Values) {
          var
            prop2Value = prop2Values[key2],
            quantity = parseInt(prop2Value.QUANTITY);

          if (quantity > 0) {
            this.setCurrentProp1(key);
            this.setCurrentProp2(key2);
            return true;
          }
        }
      }

      //Товара нет, показываем просто первое предложение
      for (var key in this.offerMatrix) {
        this.setCurrentProp1(key);
        return true;
      }
    } else {
      //Если свойств 1
      //Ищем первый товар, который есть в наличии
      for (var key in this.offerMatrix) {
        var
          propValue = this.offerMatrix[key],
          quantity = parseInt(propValue.QUANTITY);

        if (quantity > 0) {
          this.setCurrentProp1(key);
          return true;
        }

      }

      //Товара нет, показываем просто первое предложение
      for (var key in this.offerMatrix) {
        this.setCurrentProp1(key);
        return true;
      }
    }
  },

  setCurrentOffer: function (prop1, prop2) {

    if (OFA_OFFERS_IS_MATRIX) this.currentOffer = this.offerMatrix[prop1][prop2];
    else this.currentOffer = this.offerMatrix[prop1];
   
    
    if(parseInt(this.currentOffer['QUANTITY']) < 1){
	    $('.ofa-detail-price').html('&nbsp;');
    }else{
		$('.ofa-detail-price').html('<i>' + this.currentOffer['PRICE'] + '</i> руб.');    
    }

    $('.ofa-detail-icons').hide();
    $('#offer_like_compare_' + this.currentOffer['ID']).show();

    $('.ofa-articul-block I').html(this.currentOffer['ARTICUL']);
    $('.ofa-available-block I').html(this.currentOffer['AVAILABLE']);
    $('.ofa-delivery-block I').html(this.currentOffer['DELIVERY']);

    $('.count_items').attr('value', 1);

    $('.ofa-detail-to-cart-block').hide();
    $('.ofa-detail-on-order').hide();

    var isCurrentYear = this.getCurrentProductYear();

    if (this.getCurrentOfferQuantity() < 1) {
      $('.item-stock').show();
      if (isCurrentYear) $('#offer_on_order_' + this.currentOffer['ID']).show();
    } else {
      $('#offer_to_cart_' + this.currentOffer['ID']).show();
      $('.item-stock').hide();
    }
  },

  setCurrentProp2: function (prop2) {
    this.currentProp2 = prop2;

    $('.ofa-prop2-block .items > SPAN')
      .removeClass('active');

    $('.ofa-prop2-block .items > SPAN#prop2_' + this.currentProp2).addClass('active');

    this.setCurrentOffer(this.currentProp1, this.currentProp2);
  },

  setCurrentProp1: function (prop1) {
    this.currentProp1 = prop1;

    $('.ofa-prop1-block .items > SPAN')
      .removeClass('no-available')
      .removeClass('active');

    $('.ofa-prop1-block .items > SPAN#prop1_' + this.currentProp1).addClass('active');

    if (OFA_OFFERS_IS_MATRIX) {
      var prop2Values = this.offerMatrix[this.currentProp1];

      $('.ofa-prop2-block .items > SPAN')
        .removeClass('no-available')
        .removeClass('active')
        .hide();

      var counter = 0;

      for (var key in prop2Values) {

        counter++;

        var
          offer = this.offerMatrix[this.currentProp1][key],
          $prop2 = $('.ofa-prop2-block .items > SPAN#prop2_' + key);

        $prop2.show();

        if (parseInt(offer['QUANTITY']) < 1) $prop2.addClass('no-available');

        if (counter == 1) {
          this.setCurrentProp2(key);
        }
      }
    }
    else {
      this.setCurrentOffer(this.currentProp1, this.currentProp2);
    }
  }
};