$.fn.trip_initBRevMessages = function(){
    /// Run many times
    $('a.open-send-message').click( function() {
        if ( $.browser.msie == true )
            $(this).parent('.actions').siblings('.send-message').toggle('fast');
        else 
            $(this).parent('.actions').siblings('.send-message').slideToggle('fast');
    });

    /// Run many times
    $('.send-message h1').prepend(
        $('<div />').addClass('close').click( function() {
            if ( $.browser.msie == true )
                $(this).parents('.send-message').hide('fast');
        else 
                $(this).parents('.send-message').slideUp('fast');
        })
    );

    /// Run many times
    $('.send-message form').submit( function() {
        $(this).slideUp('slow', function(){
            $('div.error-message',this).remove();

            var q = $(this);
            $.post( $(this).attr('action'), $(this).serialize(), function(json) {
               if (! json.success ) {
                    q.slideDown('fast').siblings('h1').prepend( $('<div />').addClass('close')).find('div.loading').remove();

                    $.each( json, function(field,message) {
                        $('#'+field,q).after( $('<div />').addClass('error-message').text( message ) );
                    });
                } else {
                    q.siblings('h1').prepend(
                        $('<div />').addClass('success').addClass('close').text('Success!').click( function() {
                            $(this).parents('.send-message').slideUp('slow', function(){
                                $(this).find('form').show();
                            });
                        })
                    ).children('.loading').remove();
                }
            }, 'json' );
        }).siblings('h1').prepend(
            $('<div />').addClass('loading')
        ).children('.close').remove();
    });

    return this;
}

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

