

var Utils = {
                live : false,
		notificationTimer : 3000,
                
		activeTableRows : function(cssSelector){
			
			if (cssSelector) {
				$(document).delegate(cssSelector + ':not(thead tr)', 'click', function(e){
                                        
					$(this).siblings().removeClass('active');
					$(this).addClass('active');

                                        if (e.target.nodeName.toLowerCase() != 'a') {
                                                $(this).find('a.default').triggerHandler('click');
                                        }
                                       
				});
                                $(cssSelector + ':not(thead tr)').css('cursor','pointer');
			} else {
				$(document).delegate('#Content table tr:not(thead tr)', 'click', function(e){
                                   
					$(this).siblings().removeClass('active');
					$(this).addClass('active');
                                  
                                        if (e.target.nodeName.toLowerCase() != 'a') {
                                                $(this).find('a.default').triggerHandler('click');
                                        }
				})
                                $('#Content table tr:not(thead tr)').css('cursor','pointer');
			}
		},
                
                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="/images/ajax-loader.gif" 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();
                },
                
                processJSONResponse : function(response) {
                        if (response.status == 1) {
                                Utils.notify(response.message);
                        }
                        else {
                                Utils.notify(response.message, 'bad');
                        }
                },
                
                confirm : function(message, callback, title) {
                        if (!title) {
                                title = 'Confirm';
                        }
                        
                        if (typeof(callback) == 'function') {
                                $('<div><p>' + message + '</p></div>').dialog({
                                        autoOpen : true,
                                        modal : true,
                                        title : title,
                                        buttons : {
                                                'Confirm' : function() {
                                                        $(this).dialog('close');
                                                        callback.call();
                                                },
                                                'Cancel' : function() {
                                                        $(this).dialog('close');
                                                }
                                        }
                                });
                        }
                }
};

$(document).ajaxError(function(event, response, settings, error){
        var message = '';
        
        if (response.status == 401 || response.status == 400) {

                message = 'You are not authorised to perform this action.';
        }
        else if (response.status == 403) {
                message = 'Your session has expired, please login again.';
                setTimeout(function(){
                       window.location = '/site/login'; 
                }, 4000);
        }
        else 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 if (error == 'abort') {
                Utils.removeLoading();
                return;
        }
        else {
                message = 'An error has occurred.';
        }
       
        Utils.notify(message, 'bad');
        Utils.removeLoading();
     
});
