

var Utils = {
                live : false,
		notificationTimer : 3000,
                loadingImage : '/mysite/images/loading.gif',
                
                notify : function(message, type) {
                        //remove any previous notifications which haven't already cleared
                        $('#Notification').remove();
                        
                        if (!type) type = 'good';
                        $('body').append(
                                $('<div id="Notification"></div>').html(message)
                                        .addClass(type)
                                        .css({
                                                'position' : 'fixed',
                                                'top' : 30,
                                                //'left' : 500,
                                                'display' : 'none',
                                                'z-index' : 10000
                                        })
                        );
                                
                        var left = $(window).width()/2 - $('#Notification').width()/2;
                        $('#Notification')
                                .css('right', 50)
                                .fadeIn('slow', function(){
                                        setTimeout(function(){
                                                $('#Notification').fadeOut('slow', function(){
                                                        $('#Notification').remove();
                                                });
                                        }, Utils.notificationTimer);
                                });
                        
                        
                },
                
                log : function(message) {
                        if (!Utils.live) {
                                console.log(message);
                        } 
                },
                
                insertLoading : function(container, clear) {
                        if (clear) {
                                $(container).html('');
                        }
                        Utils.removeLoading();
                        
                        var top = $(container).height()/2 - 50;
                        if (top < 0) top = 0;
                        
                        $(container).append(
                                $('<div id="AjaxLoading"><img src="' + Utils.loadingImage + '" alt="Loading" /><p>Loading...</p></div>')
                                        .css({
                                                'position': 'absolute',
                                                'left' : $(container).width()/2 - 50,
                                                'top' : top
                                        })
                        )
                        .css({
                                'position' : 'relative',
                                'min-height' : 100
                        });
                        
                        
                },
                
                removeLoading : function() {
                        $('#AjaxLoading').remove();
                }
};

$(document).ajaxError(function(event, response, settings, error){
        var message = '';
        
        if (response.status == 404) {
                message = 'There was an error connecting to the server. Please check your internet connection.';
        }
        else if (response.status == 500) {
                message = 'An error has occurred on the server. Someone has been notified. If the problem persists, please contact an administrator.'
        }
        else {
                message = 'An error has occurred.';
        }
                
        /*$.modal('<h2>An error has occurred</h2><p class="form">' + message + '</p><p>&nbsp;</p>', {
                minWidth : 400
        });*/
        Utils.notify(message, 'bad');
     
});
