$(function() {

    $(document).bind('SHOW_PROFILE',function(e,sn) {
        //$('body').append("Showing: "+sn);
        $(document).trigger('SHOW_LOADING');
        $.ajax({
            type:   'GET',
            url :   '/get-profile.php?sn='+sn,
            dataType: 'html',
            error: function (X, textStatus, errorThrown) {
                switch (X.status) {
                    case 401: 
                        $(document).trigger('sys.message.error',["Can not check. User's updates are likely protected"]);
                        break; 
                    default:
                        $(document).trigger('sys.message.error',[X.responseText]);        
                }                
            },
            success: function(d,textStatus) {
                $('#profile').html(d); // show it ...mmm
            },
            complete: function() {
                $(document).trigger('HIDE_LOADING');            
            }
        }
        );         
    });
    
    $(document).bind('SHOW_LOADING',function(e) {
        $('#loading').show();
    });
    $(document).bind('HIDE_LOADING',function(e) {
        $('#loading').hide();
    });

    $('#btnCheck').click(function(e) {
        var sn = $('#screen_name').val().trim(); 
            if (sn.trim() == '') {
            $(document).trigger('sys.message.error',["Screen name is required"]);
            return false; 
        }
        
        window.location = "/"+encodeURI(sn);

    });
    
    // System Actions (sys) Event Handlers
    $(document).bind('sys.message.error',function(e,msg) {
        $('<p></p>')
            .addClass('error')
            .html(msg)
            .appendTo('#messages')
            .animate({opacity:1.0},2500)
            .fadeOut(function() { $(this).remove()});
    }); 

}); 

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,""); 
}
String.prototype.stripNewlines = function() {
    return this.replace(/[\n\r]/g,"");
}
String.prototype.squashSpaces = function() {
    return this.replace(/\s+/g," "); 
}