(function($){
    $.fn.ajaxMe = function ( options ){

        var defaults = {
            container: '#content',
            notLoggedMessage: 'Ops! Você não está logado. <a href="/login">Entre aqui</a>.'
        }

        options = $.extend(defaults, options);

        $(this).live('click',function(){
            var url = $(this).parents("form:first").attr('action');
            $(this).parents("form:first").attr('action','#');
            $(this).parents("form:first").ajaxSubmit({
                dataType: 'json',
                beforeSubmit: function(arr, $form, opts) {
                    opts.url = url;
                    $(options.container).message( '<img src="/img/layout/loader.gif" />' , '' , 'loader');
                },
                success: function(responseText, responseCode) {
                    $('.loader').hide();
                    options.success(responseText);
                },
                error: function(responseText, responseCode) {
                    $('.loader').hide();
                    if ( responseText.status == 403)
                        $(options.container).message( options.notLoggedMessage , 'error' , 'login' );
                    else
                        $(options.container).message( responseText.statusText , 'error');
                }
            });
            $(this).parents("form:first").attr('action',url);
            return false;
        });
    };
})(jQuery);

(function($){
    $.fn.message = function ( message , type , randonn ){
        if ( randonn == undefined) {
            var randomnumber=Math.floor(Math.random()*11)
            $(this).before('<div class="msg '+type+' '+randomnumber+'">'+message+'</div>');
            setTimeout(function(){
                $('.'+randomnumber).fadeOut();
            },3000);
        } else {
            if( $('.'+randonn)[0] ) {
                $('.' + randonn).html(message);
            } else {
                $(this).before('<div class="msg '+type+' '+randonn+'">'+message+'</div>');
            }
        }
    };
})(jQuery);

