﻿/// <reference path="jquery-1.2.6-vsdoc.js" />

/*  jquery.logging.js
Script to add logging TraceConsole to page and global logging functions.
Also adds F12 show/hide functionality for the TraceConsole.
*/

function FailedCallback(error) {
    var stackTrace = error.get_stackTrace();
    var message = error.get_message();
    var statusCode = error.get_statusCode();
    var exceptionType = error.get_exceptionType();
    var timedout = error.get_timedOut();

    // Display the error.    
    Sys.Debug.trace("Stack Trace: " + stackTrace + "<br/>" +
        "Service Error: " + message + "<br/>" +
        "Status Code: " + statusCode + "<br/>" +
        "Exception Type: " + exceptionType + "<br/>" +
        "Timedout: " + timedout);

    alert("An error occurred loading the data. Please try again or contact support.");
}


(function($) {
    var logDetached = false;
    
    // Add the TraceConsole to the page
    $(function() {

        var traceDiv = $("<div id='TraceDiv' style='display: none;'></div>");
        var traceConsole = $("<textarea id='TraceConsole' style='width: 98%; height: 140px;' cols='160' rows='10'></textarea>");
        var traceDetach = $("<div id='TraceDetach' style='border: solid 1px black; cursor: pointer; width: 65px; text-align: center; background: #005137; color: #fff'>Detach</div>");

        traceDiv.append(traceConsole).append(traceDetach);
        traceDiv.appendTo($("body"));

        // Detach/Attach functionality
        traceDetach.click(function() {
            if (!logDetached) {
                var traceDiv = $('#TraceDiv');
                var traceTop = traceDiv.getBottomPage();
                traceDiv.css("top", traceTop).css("position", "absolute");
                $('#TraceDetach').html("Attach");
                logDetached = true;
            }
            else {
                $('#TraceDiv').css("position", "static").css("top", 0);
                $('#TraceDetach').html("Detach");
                logDetached = false;
            }
        });

        // Bind F12 to toggle displaying the TraceConsole
        $("body").bind("keydown", function(e) {
            if (e.keyCode == 123) {
                $('#TraceDiv').toggle();
            }
        });
    });
    
    // Get the elements "top" y-value from the bottom of the page.
    $.fn.getBottomPage = function() {
        var pos = parseInt($(window).scrollTop()) + parseInt($(window).height());
        var traceHeight = this.height() + 5;
        return (pos - traceHeight) + "px";
    };

    //scroll the message box to the top offset of browser's scrool bar
    $(window).scroll(function() {
        if (logDetached) {
            var traceDiv = $('#TraceDiv');
            var traceTop = traceDiv.getBottomPage();
            traceDiv.animate({ top: traceTop }, { queue: false, duration: 100 });
        }
    });

    // global assert function
    $.Assert = function(condition, msg, displayCaller) {
        Sys.Debug.assert(condition, msg, displayCaller);
    }

    // global log function to log the message optionally dumping the specified object.
    $.log = function(msg, obj) {
        if (obj != null) {
            Sys.Debug.traceDump(obj, msg);
        } else {
            Sys.Debug.trace(msg);
        }
    };

    // log the object w/ a tracedump
    $.fn.log = function(msg) {
        Sys.Debug.traceDump(this, msg);
    };

})(jQuery);  