/// class PD.ajax

PD.ajax = function (options) {
    
    var ajaxEvents = {
        beforeSend: function (jqXHR, settings) {
            if (_.isFunction(options.beforeSend)) {
                options.beforeSend.apply(this, arguments);
            }
        },
        success: function (data, textStatus, jqXHR) {
            
            if (_.isFunction(options.success)) {
                options.success.apply(this, arguments);
            }

        },
        error: function (jqXHR, textStatus, errorThrown) {

            jqXHR.errorHandled = false;

            if (_.isFunction(options.error)) {
                options.error.apply(this, arguments);
            }

            if (!jqXHR.errorHandled) {
                new PD.Web.AjaxErrorHandler({
                    config: this,
                    jqXHR: jqXHR,
                    contextData: options.contextData
                });
            }

        },
        complete: function (jqXHR, textStatus) {

            if (_.isFunction(options.complete)) {
                options.complete.apply(this, arguments);
            }
            
            Jade.Application
                .instance
                .contexts
                .global
                .events
                .publish("Ajax:Request:Complete");

        }
    };

    return $.ajax($.extend({}, options, ajaxEvents));

};