/*******************************************************************************
*  Вопросы (консультации)  *****************************************************
*******************************************************************************/



// показываем форму нового ответа

function show_new_answer_form (a)
{
  $('#new-question-answer-form').removeClass('hidden');
  $(a).attr('onclick', '');
}



// отправляем новый ответ на вопрос

function publish_new_answer (str_example)
{
  if ($('#new-question-answer').val() == '' || $('#new-question-answer').val() == str_example)
  {
    $('#new-question-answer').focus();
    return false;
  }
  else
  {
    $('#new-question-answer-form').submit();
  }
}



// показываем форму ответа на ответ

function show_answer_form (a, parent)
{
  $('.answer-forms').remove();
  $(a).parent().after('<form method="post" action="" id="answer-to-answer-form" class="answer-forms"><fieldset><textarea name="question_answer_to_answer" id="question-answer-to-answer"></textarea><input type="hidden" name="parent" value="'+parent+'"/></fieldset><a href="javascript:;" onclick="publish_answer()">Отправить</a></form>');
  $('#question-answer-to-answer').focus();
}



// отправляем ответ на ответ

function publish_answer (str_example)
{
  if ($('#question-answer-to-answer').val() == '')
  {
    $('#question-answer-to-answer').focus();
    return false;
  }
  else
  {
    $('#answer-to-answer-form').submit();
  }
}


function delete_answer (id, m_item_id)
{
  $.post('/ajax/delete_question_answer', { comment_id: id, m_item_id: m_item_id }, function (data) {
    if (data.status)
    {
      $('#comment-wrap-'+parseInt(id)+' .content').addClass('hidden').after('<div class="deleted">удален</div>');
    }
  }, "json");
}



// добавляем вопрос в избранное

function change_question_in_favorites (question_id)
{
  $('#blog-post-info-'+question_id+' .favorites').addClass('fav-load');
  if ($('#blog-post-info-'+question_id+' .favorites').hasClass('fav-in'))
  {
    $.post('/ajax/del_question_from_favorites', { question_id: question_id }, function(data) {
      if (data.status)
      {
        $('#blog-post-info-'+question_id+' .favorites').removeClass('fav-load').removeClass('fav-in').find('a').attr('title', 'Добавить в избранное');
        Alert.show('Уведомление', 'Вопрос удален из избранного', 'message');
      }
    }, "json");
  }
  else
  {
    $.post('/ajax/add_question_to_favorites', { question_id: question_id }, function(data) {
      if (data.status)
      {
        $('#blog-post-info-'+question_id+' .favorites').removeClass('fav-load').addClass('fav-in').find('a').attr('title', 'Удалить из избранного');
        Alert.show('Уведомление', 'Вопрос добавлен в избранное', 'message');
      }
    }, "json");
  }  
}

// ставим оценку вопросу

function estimate_question (question_id, rating)
{
  $('.blog-item-estimate .ajax-loader').removeClass('hidden');
  $.post('/ajax/estimate_question', { question_id: question_id, rating: rating }, function (data) {
    $('.blog-item-estimate-result .inner').html(data.result_str);
    $('.blog-item-estimate').addClass('hidden');
    $('.blog-item-estimate-result').removeClass('hidden');
    $('.blog-post-info .rating span').html(data.new_rating);
    if (data.new_rating > 0) $('.blog-post-info .rating').addClass('rating-plus');
    else if (data.new_rating < 0) $('.blog-post-info .rating').addClass('rating-minus');
    else { $('.blog-post-info .rating').removeClass('rating-minus').removeClass('rating-plus') }
  }, "json" );
}


// показываем форму ответа на вопрос

function show_new_answer_form (where)
{
  if ($('#new-answer-form-here').html() == '')
  {
    $('#new-answer-form-here').html( $('#new-comment-form-wrap').html() );
    $('#new-answer-form-here form').addClass('comment-form-to-send');
    $('#new-answer-form-here').find('textarea').TextAreaResizer().attr('id', 'new-question-comment-textarea-tmp');
    activate_textarea_buttons ( document.getElementById('new-question-comment-textarea-tmp') );
    $('#new-answer-form-here textarea').focus();
  }
  else
  {
    $('#new-answer-form-here').html( '' );
  }
}


// отсылаем новый ответ на вопрос

function send_question_answer ()
{
  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();
  }  
}


// удаляем ответ на вопрос

function delete_question_answer (comment_id, question_id)
{
  $('#post_comment_'+comment_id).append('<div class="ajax-load"><img src="/images/interface/ajax_load_mini.gif" alt=""/></div>');
  $.post('/ajax/delete_question_comment', { comment_id: comment_id, question_id: question_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");  
}


// оценка ответа

var rate_answer_in_progress = false;
function rate_answer (a, comment_id, rating)
{
  if (rate_answer_in_progress) return false;
  rate_answer_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_answer_in_progress = false;
  }, "json" );
}