var CommentManager = {
  s: {class_comment: '', suffix_action: '', commented_title: ''},

  init: function(class_comment, suffix_action, commented_title)
  {
    var manager = CommentManager;
    manager.s.class_comment = class_comment;
    manager.s.suffix_action = suffix_action;
    manager.s.comment_deleted = commented_title;

    if(typeof(i18n) == 'undefined')
    {
      window.i18n = {};
      i18n['comment_successfully_edited'] = 'Комментарий успешно отредактирован';
      i18n['comment_assured_remove'] = 'Вы уверены что хотите удалить комментарий?';
      i18n['comment_deleted'] = 'Комментарий удален.';
      i18n['comment'] = 'Комментарий';
    };
    manager.list = jQuery('#comments_list');

    manager.form = jQuery('#comment_form');
    manager.form.ajaxForm({
      beforeSubmit: CommentManager.showSendRequest,
      success: CommentManager.showSendResponse,
      type: 'post',
      dataType: 'json',
      timeout: 30000,
      url: '/comment/ajax_add_comment/' + CommentManager.s.suffix_action,
    });
  },
  
  showSendRequest: function(formData, jqForm, options)
  {
    if(CommentManager.form.attr('disabled'))
      return false;
    ammo.ajaxLoader().show();
    CommentManager.form.attr('disabled', true);
    return true;
  },

  showSendResponse: function(data)
  {
    ammo.ajaxLoader().hide();
    CommentManager.form.attr('disabled', '');
    if(data.error)
    {
      modalWindow.messageBlock(data.error, '');
      return;
    }
    CommentManager.form.find('#comment').val("");
    CommentManager.targetClear();
    if(data.html)
    {
      CommentManager.list.html(data.html);
    }
    if(data.comment_id)
      document.location.hash = '#c_anchor_' + data.comment_id;
  },

  showEditForm: function(id) 
  { 
    var blok = jQuery("#comment_anchor_" + id);
    blok.html(jQuery('#template_edit_comment').html());
    blok.find('#comment').val(document.getElementById("comment_body_" + id).textContent);
    blok.find('#comment_id').val(id);
  },

  closeEditForm: function(id)
  {
    jQuery("#comment_anchor_" + id).empty();
  },

  sendEditedComment: function(form)
  {
    form = jQuery(form);

    var comment = form.find('#comment').val();
    var id = form.find('#comment_id').val();
    var input = form.find('input');
    
    if(input.attr("disabled"))
      return;
    input.attr("disabled", true);

    ammo.ajaxLoader().show();

    jQuery.ajax({
      type: 'POST',
      url: '/comment/ajax_edit_comment/' + CommentManager.s.suffix_action,
      data: {'id' : id, 'comment' : comment},
      dataType: 'json',
      success: function(data){
        ammo.ajaxLoader().hide();
        if(data.error)
        {
          input.attr("disabled", false);
          modalWindow.messageBlock(data.error,'');
        }
        else
        {
          CommentManager.closeEditForm(id);
          modalWindow.messageBlock(i18n['comment_successfully_edited'],'');
          jQuery('#comment_body_' + id).html(data.comment);
        }
      },
      error: baseErrorAjaxSend 
    });
  },

  remove: function(item_id)
  {
    modalWindow.confirm(i18n['comment_assured_remove'], 
      function() {
        jQuery.ajax({
          type: 'POST',
          url: '/comment/ajax_remove_comment/' + CommentManager.s.suffix_action,
          data: {'id' : item_id},
          success: function(msg){
            if(msg == 'success')
            {
              modalWindow.messageBlock(i18n['comment_deleted'],'');
              jQuery('#comment_body_' + item_id).html('<i>' + i18n['comment_deleted'] + '</i>');
              jQuery('#links_comment_' + item_id).hide();
            }
            else
              modalWindow.messageBlock(msg, '');
            return;
          }, 
          error: baseErrorAjaxSend
        })
        return;
      }
    );
  },

  setTargetLinkProps: function(num, item_id, nick, date)
  {
    var form = CommentManager.form;
    form.find('#response_to_id').val(item_id);
    form.find('#target_link').html(i18n['comment'] + ' ' + nick + ' | ' + date);
    document.getElementById('send_comment').scrollIntoView(false);
  },

  targetClear: function()
  {
    this.form.find('#response_to_id').val('');
    this.form.find('#target_link').html(this.s.commented_title);
  },

  scrollPageToTargetResponse: function() 
  {
    window.scroll(0,0);
    if(document.getElementById('response_to_id').value != '')
    {
      document.getElementById('comment_anchor_' + document.getElementById('response_to_id').value).scrollIntoView(false);
    }
  },

  ctrEnter: function(e, form)
  {
    if ((((e.keyCode == 13) || (e.keyCode == 10)) && (e.ctrlKey == true)) && !CommentManager.form.attr('disabled'))
      CommentManager.form.submit();
  },

  send_estimations: {},

  estimate: function(item_id, sing)
  {
    if(CommentManager.send_estimations[CommentManager.s.class_comment + item_id])
      return;
    send_comment_estimations[CommentManager.s.class_comment + item_id] = 1;
    jQuery.ajax({
      type: 'POST',
      dataType: 'json',
      url: '/comment/ajax_estimate/' + CommentManager.s.suffix_action,
      data: {'id' : item_id, 'sign' : sing},
      success: function(data) {
        if(data.errors)
        {
          modalWindow.messageBlock(data.errors,'');
        }
        if(data.total_estimate)
        {
          var el = jQuery('#comment_total_estimate_' + item_id);
          el.html(data.total_estimate);
          if (data.total_estimate < 0)
            el.addClass('negative').removeClass('positive');
          else
            el.addClass('positive').removeClass('negative');
        }
      }
    })
  }
};


