var LAM = LAM || {};
$(function() {
  if(LAM.Components.CopyToClipboard) {
    LAM.CopyToClipboard = new LAM.Components.CopyToClipboard();
  }

  LAM.AbuseForm = {
    init: function(options) {
      var resource_id = options.resource_id,
          abuse_path = options.abuse_path,
          resource_type = options.resource_type,
          textInput = $('#abuse_body'),
          textInputLabel = $('#abuse_body_label'),
          typeInput = $('[name=abuse_type]'),
          abuseStatus = $('#abuseStatus'),
          disabled = true,

          button = new Button('abuseSend', {disabled: true, onPress: function(){
            $.ajax({
              type: 'post',
              datatype: 'text',
              url: abuse_path,
              data: {
                'abuse[resource_type]': resource_type,
                'abuse[resource_id]': resource_id,
                'abuse[abuse_type_id]': typeInput.filter(':checked').val(),
                'abuse[body]': textInput.val()
              },
              success: function(data){
                },
              complete: function() {
                abuseStatus.html('Ваша&nbsp;жалоба&nbsp;принята');
                setTimeout(function(){abuseStatus.html('')}, 5000);
                textInput.blur().val('');
                button.disable();
              }
            });
            return false;
           }});

          textInput.bind('focus',function(){
            textInputLabel.hide();
          })
          .bind('blur', function(){
            if(isBlank(textInput.val())) {
              textInputLabel.show();
              button.disable();
            } else {
              enable_button();
            }
          })
          .bind('keyup', function(){
            if(isBlank(textInput.val())) {
              button.disable();
            } else {
              enable_button();
            }
          });

          typeInput.bind('click', enable_button);

          if(!isBlank(textInput.val())) {
              textInputLabel.hide();
          }

          enable_button();

          function enable_button() {
  // TODO: в панели поста выводятся радиобаттоны, в панели вики - хидден инпут, проверяем, что не пустое поле с текстом жалобы и существует хидден инпут или выбран один из радиобаттонов
            if(!isBlank(textInput.val()) && (typeInput.is('input:hidden') || typeInput.filter(':checked').length)) {
              button.enable();
            }
          };
          function isBlank(str) {
            return /^\s*$/.test(str);
          };
    }
  };

});

