var comment = {
    formHtml: '',

    add: function(commentParentId)
    {
        var commentText = $('#comment_text').val();
        var commentTitle = $('#comment_title').val();
        $('#comment_form').remove();

        $('#comment' + commentParentId).append(this.formHtml);
        $('#comment_form').show();
        $('#comment_parent_id').val(commentParentId);

        $('#comment_text').val(commentText);
        $('#comment_title').val(commentTitle);
    },

    ins: function(form)
    {
        var ajax = new leto.ajax('/blogs/ajaxcomment/insert/');
        ajax.onSuccess = function(r)
        {
            $('#comment_form').remove();
            $('#children' + r.id).append(r.html);
        }
        ajax.onError = function(e, r)
        {
            if (14 == r.ecode) {
                layout.loginNow();
            } else {
                alert(e);
            }
        }
        ajax.query({ 'form': form });
    },

    del: function(commentId, $div)
    {
        if (!confirm('Вы уверены?')) {
            return;
        }

        var ajax = new leto.ajax('/blogs/ajaxcomment/delete/');
        ajax.onSuccess = function()
        {
            $div.slideUp();
        }
        ajax.query({ id: commentId });

    },

    isReply: function()
    {
        return -1 != location.href.search('#reply');
    }
}

$(document).ready(function(){
    comment.formHtml = $('#_comment_form').html();
    $('#_comment_form').remove();

    if (comment.isReply()) {
        comment.add(0);
    }
});
