$(document).ready(function(){
    /*COMMENTS*/
    var register_block = $('#js_register_block');
    
    $('.js_comment_to_answer').click(function(){
        var element = $(this);
        var user_name = element.parent().parent().children(".comment_header").children(".user_name").children(".user_name").html();
        $('#id_comment').attr('value','to '+user_name+': ').select();
        $('#id_comment').focus();
        return false;
    });
    
    function bind_comment_form(){
        var $comment_form_container = $('.js_comment_form');
        var $form = $comment_form_container.find('form');
        $form.ajaxForm({
            beforeSend: function(){
                if(!$form.find('#id_comment').attr('value')){
                    return false;
                }
                if($form.find('#id_name').length && !$form.find('#id_name').attr('value')){
                    return false;
                }
            },
            success: function(data){
                if(!data.length){
                    window.location.replace('./#c_end');
                    window.location.reload(); // случай странных браузеров
                } else {
                    $comment_form_container.hide();
                    $comment_form_container.after(data);
                    $comment_form_container.detach();
                    bind_comment_form();
                }
            }
        });
    }
    bind_comment_form();

    $('.js_delete_comment_form').each(function(){
        var that = $(this);
        that.ajaxForm({
            success: function(){
                that.parents('.js_comment').remove();
        }});
    });
    /*END OF COMMENTS*/ 
});

