$(function() {

    $(document).on('click', '.artbox_comment_delete', function() {
        var container = $(this).parents('.artbox_comment_container');
        var comment_id = $(container).data('comment_id');
        var form_name = $(container).data('form_name');
        if(confirm("Уверены, что хотите удалить комментарий?")) {
            $.post(
                '/artbox-comment/delete',
                {
                    Comment: {
                        comment_id: comment_id
                    }
                },
                function(data, textStatus, jqXHR) {
                    if(!data.error) {
                        $(container).after('<p class="removeable">'+data.text+'</p>');
                        $(container).remove();
                    } else {
                        $(container).prepend('<p class="removeable error_message">'+data.error+'</p>')
                    }
                }
            );
        }
    });

    $(document).on('click', '.artbox_comment_reply', function() {
        var container = $(this).parents('.artbox_comment_container').first();
        var comment_id = $(container).data('comment_id');
        var form_name = $(container).data('form_name');
        var author = $(container).find('.artbox_comment_author').first().text();
        var comment_form = $('.artbox_comment_form').first();
        var offset = $(comment_form).offset();
        var reply_block = $(comment_form).find('.artbox_comment_reply_block').first();
        $(reply_block).empty();
        $(reply_block).append('<input type="hidden" name="'+form_name+'[comment_pid]" value="'+comment_id+'">');
        $(reply_block).append('<p class="artbox_comment_reply_author">'+author+'</p>');
        $('html, body').animate({
            scrollTop: offset.top - 50,
        });
    });

    $(document).on('click', '.artbox_comment_reply_author', function() {
        $(this).parents('.artbox_comment_reply_block').first().empty();
    });

    $(document).on('click', '.artbox_comment_update', function() {
        $(this).removeClass('artbox_comment_update');
        $(this).text('Сохранить');
        $(this).addClass('artbox_comment_update_submit');
        var container = $(this).parents('.artbox_comment_container').first();
        var comment_id = $(container).data('comment_id');
        var form_name = $(container).data('form_name');
        var text = $(container).find('.artbox_comment_text');
        var object = {};
        object[form_name] = {comment_id: comment_id};
        $.post(
            '/artbox-comment/form',
            object,
            function(data, textStatus, jqXHR) {
                $(text).hide();
                $(text).after(
                    '<div class="artbox_comment_text_edit new-portf-answer">'
                    + data
                    + '</div>'
                );
            }
        );
    });

    $(document).on('click', '.artbox_comment_update_reply', function() {
        $(this).remove();
    });

    $(document).on('click', '.artbox_comment_update_submit', function(e) {
        e.preventDefault();
        var container = $(this).parents('.artbox_comment_container').first();
        var edit = $(container).find('.artbox_comment_text_edit').first();
        $.post(
            '/artbox-comment/update',
            $(edit).find('form').serialize(),
            function(data) {
                if(!data.error) {
                    location.reload(true);
                }
            }
        )
    });
});