// Комментарии


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') );

    // проверяем, не открыта ли форма редактирования комментария и закрываем ее если нужно
    if (!$('#edit-comment-'+comment_id).hasClass('hidden'))
    {
        $('#edit-comment-'+comment_id).addClass('hidden');
        $('#comment-text-'+comment_id).removeClass('hidden');
    }
  }
  else
  {
    $('#answer-comment-'+comment_id+'-here').html( '' );
  }

}


function show_edit_comment_form(comment_id)
{
  if ($('#edit-comment-'+comment_id).hasClass('hidden'))
  {
    $('#comment-text-'+comment_id).addClass('hidden');
    $('#edit-comment-'+comment_id).removeClass('hidden');
    $('#answer-comment-'+comment_id+'-here').html('');
  }
  else
  {
    $('#edit-comment-'+comment_id).addClass('hidden');
    $('#comment-text-'+comment_id).removeClass('hidden');
  }
}


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 delete_comment(comment_id)
{
    $('#edit-comment-'+comment_id).addClass('hidden');
    $('#comment-edit-link-'+comment_id).addClass('hidden');
    $('#comment-reply-link-'+comment_id).addClass('hidden');
    $('#comment-text-'+comment_id).removeClass('hidden');
    $('#comment-text-'+comment_id).append('<div class="ajax-load"><img src="/images/interface/ajax_load_mini.gif" alt=""/></div>');


    $.post('/ajax2/comments_delete_comment', { comment_id: comment_id }, function (data) {
    if (data.status)
    {
        $('#comment-text-'+comment_id+' .ajax-load').remove();
        $('#comment-text-'+comment_id).html('<div class="deleted">Комментарий удален</div>');
    }
    else
    {
        $('#comment-text-'+comment_id+' .ajax-load').remove();
    }
    }, "json");

}



// удаляем комментарий
function update_comment_text(comment_id)
{
    $('#edit-comment-'+comment_id).addClass('hidden');
    $('#comment-text-'+comment_id).removeClass('hidden');
    $('#comment-text-'+comment_id).html('');
    $('#comment-text-'+comment_id).append('<div class="ajax-load"><img src="/images/interface/ajax_load_mini.gif" alt=""/></div>');


    var comment_text = $('#edit-comment-'+comment_id+' .comment-edit-text').val();

    $.post('/ajax2/comments_update_comment', { comment_id: comment_id, comment_text: comment_text  }, function (data) {
    if (data.status)
    {
        $('#comment-text-'+comment_id+' .ajax-load').remove();
        $('#comment-text-'+comment_id).html(data.comment_text);
    }
    else
    {
        $('#comment-text-'+comment_id+' .ajax-load').remove();
    }
    }, "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('<img src="/images/interface/spacer.gif" alt=""/>'+data.new_plus);
    $('#post_comment_'+comment_id+' .rate .minus').html('<img src="/images/interface/spacer.gif" alt=""/>'+data.new_minus);
    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');
}

