
$.fn.exists = function () { return $(this).length !== 0; }

$(document).ready(function () {
    creatNamespace();
    $.apr.error = {
        callback: {
            failed: function (result, userContext, methodName) {
                $.apr.error.showBusy(false);
                switch (methodName) {
                    case "GetLastError":
                        // If we get an error callings GetLastError, what are you going to do?
                        break;
                    case "SubmitIncident":
                        $.apr.error.data.incidentID = null;
                        if (!isNull($.apr.error.data.lastErrorResult)) $.apr.error.trigger("errorReceived", $.apr.error.data.lastErrorResult);
                        break;
                    default:
                        $.apr.error.data.lastErrorMethod = methodName;
                        $.apr.error.data.lastErrorResult = result;
                        $.apr.error.getLastError();
                        break;
                }
            },
            succeeded: function (result, dialog, methodName) {
                $.apr.error.showBusy(false);
                switch (methodName) {
                    case "GetLastError":
                        if (!isNull(result)) $.apr.error.data.lastErrorResult = result;
                        else result = $.apr.error.data.lastErrorResult;

                        var title = 'Error:  ' + result.Message;
                        if (!isNull($.apr.error.data.lastErrorMethod)) {
                            title = 'Error:  ' + $.apr.error.data.lastErrorMethod;
                        }
                        var description = result.Message;
                        if (isNull(description)) description = result._message;
                        if (!isNull(result.Detail)) description += '  Detail:  ' + result.Detail;
                        else if (!isNull(result._stackTrace)) description += '  Stack Trace:  ' + result._stackTrace;
                        var page = window.location.pathname;
                        $.apr.error.submitIncident(title, description, '', page);
                        break;
                    case "SubmitIncident":
                        if (!isNull(result)) $.apr.error.data.incidentID = result;
                        $.apr.error.trigger("errorReceived", $.apr.error.data.lastErrorResult);
                        break;
                    default:
                        alert(methodName + " succeeded, but wasn't handled.");
                        break;
                }
            }
        },
        eventHandlers: {
            onGoToErrorPage: function (error) {
                debugger;
                $.apr.error.data.lastErrorResult = error;
                window.location.href = $.apr.common.getUrl("/ErrorPage.aspx");
            },
            onErrorReceived: function (error) {
                if (isNull(error)) return;
                var message = 'An error has occurred.  Sorry for the inconvenience.';
                if (!isNull($.apr.error.data.incidentID)) message = $.apr.error.data.incidentID + ':  An error has occurred and the support team has been notified.  Sorry for the inconvenience.';
                var detail = error.Detail;
                if (isNull(detail)) detail = error._message + '.  ' + error._stackTrace;
                $.apr.error.ui.message.html(message);
                $.apr.error.ui.detail.html('');
                if (!isNull(detail) && !isNull($.apr.userInfo) && !isNull($.apr.userInfo.current) && $.apr.userInfo.current.IsPerformanceReportingAdmin) $.apr.error.ui.detail.html(detail);
                $.apr.error.showError();
                $.apr.error.data.incidentID = null;
            },
            onOkButton: function (e) {
                $.apr.error.ui.container.fadeOut(200, function () {
                    $.unblockUI();
                    $.apr.error.trigger("errorDialogClose");
                    if ($.apr.error.flags.isErrorPage) $.apr.error.goToLoginPage();
                });
            },
            onShowDetail: function (e) {
                if ($(this).text() == "Show Detail") {
                    $.apr.error.ui.detail.fadeIn(500);
                    $(this).text('Hide Detail');
                }
                else {
                    $.apr.error.ui.detail.fadeOut(500);
                    $(this).text('Show Detail');
                }
            },
            onUserInfoChange: function (userInfo) {
                if (isNull(userInfo) || isNull($.apr.userInfo.current)) return;
                if ($.apr.userInfo.current.IsPerformanceReportingAdmin) $.apr.error.ui.links.showDetailLink.show();
                if ($.apr.error.flags.isGetErrorOnReady && $.apr.error.flags.isErrorPage) $.apr.error.getLastError();
            }
        },
        data: {
            incidentID: null,
            lastErrorMethod: null,
            lastErrorResult: null
        },
        flags: {
            isGetErrorOnReady: true,
            isErrorPage: false
        },
        getLastError: function () {
            $.apr.error.showBusy(true);
            $.apr.proxy.systemService.GetLastError($.apr.error.callback.succeeded, $.apr.error.callback.failed, null);
        },
        goToLoginPage: function () {
            $.apr.busyBox.start();
            var url = $.apr.common.getUrl("/Login.aspx");
            $(location).attr('href', url);
        },
        init: function () {
            this.ui.container = $('#error-container');
            if (isNull(this.ui.container) || !this.ui.container.exists()) return;

            this.flags.isErrorPage = (window.location.pathname.indexOf('ErrorPage.aspx') > -1);
            this.ui.blockMsg = $('.blockUI.blockMsg');
            this.ui.header = $('#error-header', this.ui.container);
            this.ui.footer = $('#error-footer', this.ui.container);
            this.ui.message = $('#error-message', this.ui.container);
            this.ui.detail = $('#error-detail', this.ui.container);
            this.ui.links.showDetailLink = $('#showDetailLink', this.ui.container);
            this.ui.buttons.okButton = $('#errorOkButton', this.ui.container);
            this.bind("errorReceived", $.apr.error.eventHandlers.onErrorReceived);
            this.bind("goToErrorPage", $.apr.error.eventHandlers.onGoToErrorPage);
            if (!isNull($.apr.userInfo)) $.apr.userInfo.bind("userInfoChange", this.eventHandlers.onUserInfoChange);

            this.initEvents();
        },
        initEvents: function () {
            if ($.apr.error.ui.buttons.okButton.exists()) {
                $.apr.error.ui.buttons.okButton.click($.apr.error.eventHandlers.onOkButton);
            }
            if ($.apr.error.ui.links.showDetailLink.exists()) {
                $.apr.error.ui.links.showDetailLink.click($.apr.error.eventHandlers.onShowDetail);
            }
        },
        showBusy: function (isBusy) {
            if (isBusy) setTimeout('$.apr.busyBox.start();', 0);
            else setTimeout('$.apr.busyBox.stop();', 0);
        },
        showError: function () {
            setTimeout("$.blockUI({ message: $.apr.error.ui.container, css: { width: '570px', left: $.apr.common.getLeftCenter(570),  top: $.apr.common.getTopCenter(270)} });", 0);
        },
        submitIncident: function (title, description, notes, replicationProcedures) {
            $.apr.error.showBusy(true);
            $.apr.proxy.systemService.SubmitIncident(title, description, notes, replicationProcedures, $.apr.error.callback.succeeded, $.apr.error.callback.failed, null);
        },
        ui: {
            buttons: {
                okButton: null
            },
            links: {
                showDetailLink: null
            },
            blockMsg: null,
            container: null,
            detail: null,
            footer: null,
            header: null,
            message: null
        }
    };
    $.apr.app.trigger("scriptReady", $.apr.error);

});


