$.fn.trip_initBRevComments = function(){
    /// Run many times
    $('.comments:has(div.one-comment)').each( function(i, item) {
        $(item).children('div.one-comment').wrapAll( $('<div />') ).filter(':last').css('border-bottom-width', '0');
    });

    /// Run many times
    $('.comments').not(':has(div.one-comment)').each( function(i, item) {
        $(item).append( $('<div />') );
    });

    /// Run many times
    $('a.open-comments').unbind('click').click( function() {
        $(this).parent('.actions').siblings('.comments').toggle('fast');
    });

    /// Run many times
    $('a.open-add-comment').unbind('click').click( function() {
        $(this).parent('.actions').siblings('.add-comment').toggle('fast');
    });

    /// Run many times
    $('.comments h1').prepend(
        $('<div />').addClass('close').unbind('click').click( function() {
            $(this).parents('.comments').hide('fast');
        })
    );

    /// Run many times
    $('.add-comment h1').prepend(
        $('<div />').addClass('close').unbind('click').click( function() {
            $(this).parents('.add-comment').hide('fast');
        })
    );

    // Add-comment submit handler
    /// Run many times
    $('.add-comment form').unbind('submit').submit( function() {
        $(this).slideUp('slow').siblings('h1').prepend(
            $('<div />').addClass('loading')
        ).children('.close').remove();

        var q = $(this);
        $.post( $(this).attr('action'), $(this).serialize(), function(html) {

            q.parents('.add-comment').siblings('.comments').children('div').html( html ).each( function(i, item) {
                var c = $('div.one-comment', this).length;
                var s = c == 1 ? ' ' : 's';
                $(this).parents('.comments').siblings('.actions').children('a.open-comments').text( c + ' Comment' + s);
            }).slideDown('fast').children('div.one-comment').filter(':last').css('border-bottom-width', '0');

            q.siblings('h1').prepend(
                $('<div />').addClass('success').addClass('close').text('Success!')
            ).parents('div.add-comment').fadeOut('slow', function() {
                $(this).siblings('.actions').children('a.open-add-comment').unbind('click').addClass('disabled');
            }).children('.loading').remove();

        }, 'html' );
    });

    return this;
}

jQuery(function($){$(document).trip_initBRevComments();});

