function CommentAnswer(id) {

    var author = $('#author-'+id).html();

    $('#addcomment').find('#answerblock').html('Ответить: <b>' + author + '</b><input type="hidden" name="parent_id" value="' + id + '">&nbsp;<a href="javascript: CommentNobody();">[X]</a>');
    $('#addcomment').find('#answerblock').css('display', 'block');

    $.scrollTo('#addcomment');
}

function CommentEdit(id) {

    var form = null;

    if (form = $('#comment-' + id).find('form').get(0)) {
        $(form).ajaxSubmit({ dataType: null, success: CommentEditResult })
    } else {
        $('#comment-' + id).html('<form method="post" action="/ajax/editcomment.php"><input type="hidden" name="id" value="' + id + '"><textarea name="content" style="width: 100%;">' + $('#comment-' + id).html() + '</textarea></form>');
        $($('#comment-' + id).siblings('.ans').children('a').get(0)).html('<b>Сохранить</b>');
    }
}

function CommentEditResult(data) {
    data = eval('(' + data + ')');
    if (data.status == "success") {
        $('#comment-' + data.id).html($('#comment-' + data.id).find('textarea').val());
        $($('#comment-' + data.id).siblings('.ans').children('a').get(0)).html('Редактировать');
    } else {
        alert(data.status);
    }
}

function CommentDelete(id) {

    if (confirm('Вы действительно хотите удалить свой комментарий?')) {
        jQuery.get('/ajax/deletecomment.php?id='+id, null, CommentDeleteResult);
    }
}

function CommentDeleteResult(data) {
    data = eval('(' + data + ')');
    if (data.status == "success") {
        $('#comment-' + data.id).parent().html('Комментарий удален');
    } else {
        alert(data.status);
    }
}

function CommentNobody() {

    $('#addcomment').find('#answerblock').html('');
    $('#addcomment').find('#answerblock').css('display', 'none');
}

function CommentAdd() {

    $('body').css('cursor', 'wait');
    $('#addcomment_button').attr('src', '/images/addcomment_h.png');
    $('#addcomment').ajaxSubmit({ dataType: null, success: CommentAddResult });
}

function CommentAddResult(data) {
    $('body').css('cursor', '');
    $('#addcomment_button').attr('src', '/images/addcomment.png');
    data = eval('(' + data + ')');
    if (data.status == "success") {
        CommentNobody();
        $('#addcomment').find('textarea').val('');
        $('#addcomment').find('input[type=text]').val('');
        window.location = window.location;
    } else {
        alert(data.status);
    }
    ReloadCaptcha();
}

function ReloadCaptcha() {
    $('#addcomment').find('input[name=captcha]').val('');
    $('#addcomment').find('#captcha_img').attr('src', '/engine/captcha.php?'+Math.random());
}

$(document).ready(function() {

   $('#addcomment').submit(function() {

        CommentAdd();
        return false;
   });
});
