﻿// Copyright 2009: Thomson Reuters Global Resources. All Rights Reserved. Proprietary and Confidential information of TRGR. Disclosure, Use or Reproduction without the written authorization of TRGR is prohibited.
/// <reference path="~/shared/js/jquery-vsdoc.js"/>
/// <reference path="~/shared/js/namespaces.js"/>

Type.registerNamespace("Westlaw.Core");

Sys.Application.add_init(initCore);

var WestlawUser;

function initCore()
{
    WestlawUser = $create(Westlaw.Core.WestlawUser);

    // Add error logging component to WestlawUser
    WestlawUser.set_feature(Westlaw.Core.Feature.ErrorLogging, $create(Westlaw.Core.ErrorLogger, { id: 'errorLogger', mode: logMode }));

    window.onerror = WestlawUser.Log.UncaughtError;

    // Wrap jQuery's bind command to call our error logger
    var jQueryBind = jQuery.fn.bind;
    jQuery.fn.bind = function(type, data, fn)
    {
        if (!fn && data && typeof data == 'function')
        {
            fn = data;
            data = null;
        }
        if (fn)
        {
            var origFn = fn;
            var wrappedFn = function()
            {
                try
                {
                    origFn.apply(this, arguments);
                }
                catch (ex)
                {
                    var logger = $find('errorLogger');
                    if (!logger.get_rethrowing())
                    {
                        logger.UncaughtError(ex.message);
                    }
                }
            };
            fn = wrappedFn;
        }
        return jQueryBind.call(this, type, data, fn);
    };
}

// Feature enum -------------------------------------------------------------------

Westlaw.Core.Feature = function()
{
    throw Error.notImplemented();
}

Westlaw.Core.Feature.prototype = {
    Analytics: 1,
    ErrorLogging: 2
}
Westlaw.Core.Feature.registerEnum('Westlaw.Core.Feature');

// Log mode enum -------------------------------------------------------------------

Westlaw.Core.LogMode = function()
{
    throw Error.notImplemented();
}

Westlaw.Core.LogMode.prototype = {
    Off: 1,
    LogAndHandle: 2,
    LogOnly: 3
}
Westlaw.Core.LogMode.registerEnum('Westlaw.Core.LogMode');

// WestlawUser -------------------------------------------------------------------

Westlaw.Core.WestlawUser = function()
{
    Westlaw.Core.WestlawUser.initializeBase(this);
    this.Log = null;
    this.Analytics = null;
}

Westlaw.Core.WestlawUser.prototype = {
    initialize: function()
    {
        Westlaw.Core.WestlawUser.callBaseMethod(this, 'initialize');
    },

    dispose: function()
    {
        Westlaw.Core.WestlawUser.callBaseMethod(this, 'dispose');
    },

    set_feature: function(feature, obj)
    {
        switch (feature)
        {
            case Westlaw.Core.Feature.Analytics:
                this.Analytics = obj;
                break;
            case Westlaw.Core.Feature.ErrorLogging:
                this.Log = obj;
                break;
        }
    }
}
Westlaw.Core.WestlawUser.registerClass('Westlaw.Core.WestlawUser', Sys.Component);

// Error logger -------------------------------------------------------------------

Westlaw.Core.ErrorLogger = function()
{
    Westlaw.Core.ErrorLogger.initializeBase(this);
    this.mode = false;
    this.rethrowing = false;
}

Westlaw.Core.ErrorLogger.prototype = {
    initialize: function()
    {
        Westlaw.Core.ErrorLogger.callBaseMethod(this, 'initialize');

        if (this.mode === 'off')
        {
            this.mode = Westlaw.Core.LogMode.Off;
        }
        else if (this.mode === 'logAndHandle')
        {
            this.mode = Westlaw.Core.LogMode.LogAndHandle;
        }
        else if (this.mode === 'logOnly')
        {
            this.mode = Westlaw.Core.LogMode.LogOnly;
        }
        else
        {
            throw Error.invalidOperation();
        }
    },

    dispose: function()
    {
        Westlaw.Core.ErrorLogger.callBaseMethod(this, 'dispose');
    },

    AppError: function(ex)
    {
        if (this.mode !== Westlaw.Core.LogMode.Off)
        {
            this._logError(ex.message);
        }
        if (this.mode === Westlaw.Core.LogMode.Off || this.mode === Westlaw.Core.LogMode.LogOnly)
        {
            this._set_rethrowing(true);
            throw (ex);
            this._set_rethrowing(false);
        }
    },

    UncaughtError: function(msg, url, lineNo)
    {
        if (this.mode !== Westlaw.Core.LogMode.Off)
        {
            this._logError(msg);
        }
        return (this.mode === Westlaw.Core.LogMode.LogAndHandle);
    },

    _logError: function(errorMsg)
    {
        var msg = { "msg": errorMsg + "|" + document.title + "|" + window.location };

        var serializer = Sys.Serialization.JavaScriptSerializer;
        var json = serializer.serialize(msg);

        $.ajax({
            type: 'POST',
            url: '/ClientErrorLogger.svc/V1/LogError',
            data: json,
            contentType: "application/json; charset=utf-8"
        });
    },

    get_rethrowing: function()
    {
        return this.rethrowing;
    },

    _set_rethrowing: function(flag)
    {
        this.rethrowing = flag;
    }
}
Westlaw.Core.ErrorLogger.registerClass('Westlaw.Core.ErrorLogger', Sys.Component);

if (typeof (Sys) !== "undefined")
{
    Sys.Application.notifyScriptLoaded();
}