// Комментарии


function show_new_comment_form (where)
{
  if ($('#new-comment-form-here').html() == '')
  {
    $('.answer-comment-wrap').html('');
    $('#new-comment-form-here').html( $('#new-comment-form-wrap').html() );
    $('#new-comment-form-here form').addClass('comment-form-to-send');
    $('#new-comment-form-here').find('textarea').TextAreaResizer().attr('id', 'new-post-comment-textarea-tmp');
    activate_textarea_buttons ( document.getElementById('new-post-comment-textarea-tmp') );
  }
  else
  {
    $('#new-comment-form-here').html( '' );
  }
}


function send_post_comment ()
{
  if ( $('form.comment-form-to-send').size() > 0 )
  {
    if ($('form.comment-form-to-send textarea').val() == '')
    {
      $('form.comment-form-to-send textarea').focus();
    }
    else $('form.comment-form-to-send').submit();
  }
  
  //$.post('/ajax/post_comment', { comment_text: $('#new-comment-form-here textarea').val(), post_id: post_id }, function (data) {
    /*
    $('#popup-frm-admin-letter .ajax-load').addClass('hidden');
    if (data.status)
    {
      hide_popup_form('admin-letter');
      Alert.show('Уведомление', 'Письмо отправлено', 'message');
      clear_admin_letter();
    }
    else
    {
      $('#admin-letter-btn-submit').attr('disabled', '');
      Alert.show('Уведомление', 'Произошла ошибка', 'error');
    }
    */
  //}, "json");
}


function show_answer_comment_form (comment_id)
{
  if ($('#answer-comment-'+comment_id+'-here').html() == '')
  {
    $('.answer-comment-wrap').html('');
    $('#new-comment-form-here').html('');
    $('#answer-comment-'+comment_id+'-here').html( $('#new-comment-form-wrap').html() );
    $('#answer-comment-'+comment_id+'-here form').addClass('comment-form-to-send');
    $('#answer-comment-'+comment_id+'-here fieldset').append('<input type="hidden" name="answer_to" value="'+comment_id+'"/>');
    $('#answer-comment-'+comment_id+'-here').find('textarea').TextAreaResizer().attr('id', 'new-post-comment-textarea-tmp');
    activate_textarea_buttons ( document.getElementById('new-post-comment-textarea-tmp') );
  }
  else
  {
    $('#answer-comment-'+comment_id+'-here').html( '' );
  }  
}


function delete_post_comment(comment_id, post_id)
{
  $('#post_comment_'+comment_id).append('<div class="ajax-load"><img src="/images/interface/ajax_load_mini.gif" alt=""/></div>');
  $.post('/ajax/delete_post_comment', { comment_id: comment_id, post_id: post_id }, function (data) {
    if (data.status)
    {
      $('#post_comment_'+comment_id+' .ajax-load').remove();
      $('#post_comment_'+comment_id).append('<div class="deleted">удален</div>');
      Alert.show('Уведомление', 'Комментарий удален', 'message');
    }
    else
    {
      $('#post_comment_'+comment_id+' .ajax-load').remove();
      Alert.show('Уведомление', 'Произошла ошибка', 'error');
    }
  }, "json");  
}


// предпросмотр комментария

function preview_comment ()
{
  $.post('/ajax/preview_comment', { comment_text: $('form.comment-form-to-send textarea').val() }, function(data) {
    if (data.status)
    {
      window.open('/preview/comment/'+data.preview_id, 'Предпросмотр комментария', 'height=400,width=800,toolbar=no,location=no,status=no'); 
    }
  }, "json");  
}


// оценка комментария

var rate_comment_in_progress = false;
function rate_comment (a, comment_id, rating)
{
  if (rate_comment_in_progress) return false;
  rate_comment_in_progress = true;
  $(a).find('img').addClass('load');
  $.post('/ajax/rate_comment', { comment_id: comment_id, rating: rating }, function (data) {
    $(a).find('img').removeClass('load');
    $('#post_comment_'+comment_id+' .rate .plus').html(data.new_plus+'<img src="/images/interface/spacer.gif" alt=""/>');
    $('#post_comment_'+comment_id+' .rate .minus').html(data.new_minus+'<img src="/images/interface/spacer.gif" alt=""/>');
    if (rating == '+')
      $('#post_comment_'+comment_id+' .rate .plus').addClass('plus-active');
    else if (rating == '-')
      $('#post_comment_'+comment_id+' .rate .minus').addClass('minus-active');
    rate_comment_in_progress = false;
  }, "json" );
}


function highlight_comment_rate (who, str_class, highlight)
{
  if (highlight) $(who).parent().addClass(str_class+'-active');
  else $(who).parent().removeClass(str_class+'-active');
}
