window.addEvent('domready', function(){
  $extend(Element.NativeEvents, {
      'paste': 2, 'input': 2
  });
  Element.Events.paste = {
      base : (Browser.Engine.presto || (Browser.Engine.gecko && Browser.Engine.version < 19)) ? 'input' : 'paste',
      condition: function(e){
          this.fireEvent('paste', e, 1);
          return false;
      }
  };
  var bCutIsNeeded = false;
  /**
   * Comments counting
   */
  var countChars = function() {
    var textarea = this.getElement('textarea');
    this.getFirst('div.panel_form').getElement('span.curtextnumber').set('html', textarea.value.length);
  }
  $$('div.reply').addEvent('keyup', countChars);
  $$('div.reply').addEvent('paste', countChars);
  /**
   * Topics counting
   */
  var confirmNoCut = function (event) {
    if (!confirm('Размер топика настолько большой, что просто необходимо поставить <CUT>. Сделать это автоматически? Можете нажать "Отмена" для того, чтобы сделать это самостоятельно.')) {
      event.preventDefault();
    }
  }
  var topicFormCharCounting = function(obj) {
    $$('span.curtextnumber').set('html', obj.value.length);
    if(obj.value.length >= CHARCNTR_TOPIC_CUT && obj.value.search(/<cut[^>]*>/i) === -1) {
      if (!bCutIsNeeded) {
        $('span_for_cut_notify').set('html', aLang['charcntr_put_cut']);
        $$('input[name=submit_topic_publish]').addEvent('click', confirmNoCut);
        bCutIsNeeded = true;
      }
    } else {
      if (bCutIsNeeded) {
        $('span_for_cut_notify').set('html', '');
        $$('input[name=submit_topic_publish]').removeProperty('disabled');
        bCutIsNeeded = false;
      }
    }
  }
  var editTopicEvent = function(){
    topicFormCharCounting(this);
  }
  var insertCutForTopicEvent = function(){
    topicFormCharCounting($('topic_text'));
  }
  $$('div.profile-user textarea#topic_text').addEvent('keyup', editTopicEvent);
  $$('div.profile-user textarea#topic_text').addEvent('paste', editTopicEvent);
  $$('div.profile-user div.panel_form').addEvent('click', insertCutForTopicEvent);
  /**
   * Counting char number in textfield on load time
   */
  if ($('topic_text')) {
    topicFormCharCounting($('topic_text'));
  }
});

