﻿
// When the page loads, set up the history manager.
Sys.Application.add_init(function() {
    Sys.Application.set_enableHistory(true);
    Sys.Application.add_navigate(handleNavigate);
});

// Called by the application to add an entry to the history.
function addHistory(data) {
    handleNavigate.ignoreNext = true;
    Sys.Application.addHistoryPoint({ data: data }, document.title);
}
handleNavigate.ignoreNext = true;

// Called whenever the browser back/next button is used, and calls into the Silverlight app to update its state.
function handleNavigate(sender, args) {
    if (handleNavigate.ignoreNext) {
        handleNavigate.ignoreNext = false;
        return;
    }
    var client = document.getElementById("ccwclient");
    if (client != null && client.Content != null) {
        if (document.location.hash.indexOf("s") == 1) {
            // the app will manually change the hash when bookmarks are saved. ignore that change.
            return;
        }
        client.Content.Page.GoToState(args.get_state().data ? args.get_state().data.toString() : "");
    }
}

function resetMap() {
    if (window.location.toString().indexOf("#") != -1) {
        window.location = window.location.toString().substr(0, window.location.toString().indexOf("#"));
    }
    else {
        window.location.reload(false);
    }
    return false;
}

var PromptFinishInstall = '<div><img src="Images/PromptFinishInstall.png" /></div>'; // "<div><p>You are now installing Silverlight, refresh your browser when done.</p></div>";
var PromptUpgrade = '<div><a href="#" onclick="UpgradeClicked()"><img src="Images/PromptUpgrade.png" /></a></div>'; // "<div><p onclick='UpgradeClicked'>This application needs you to upgrade the Silverlight plugin that runs it. An older version is installed. Click here to upgrade it.</p></div>";
var PromptFinishUpgrade = '<div><img src="Images/PromptFinishUpgrade.png" /></div>'; // "<div><p>You are now upgrading Silverlight. When this is done, please restart your browser.</p></div>";
var PromptRestart = '<div><img src="Images/PromptRestart.png" /></div>'; // "<div><p>Please restart your browser.</p></div>";
var PromptNotSupported = '<div><img src="Images/PromptNotSupported.png" /></div>'; // "<div><p>This browser doesn't support Silverlight, sorry!</p></div>";

function onSilverlightLoad(sender) {
    Silverlight.IsVersionAvailableOnLoad(sender);
}

Silverlight.onRequiredVersionAvailable = function() {
};

Silverlight.onRestartRequired = function() {
    document.getElementById("silverlightControlHost").innerHTML = PromptRestart;
};

Silverlight.onUpgradeRequired = function() {
    document.getElementById("silverlightControlHost").innerHTML = PromptUpgrade;
};

Silverlight.onInstallRequired = function() {
};

function UpgradeClicked() {
    window.location = "http://go2.microsoft.com/fwlink/?linkid=124807";
    document.getElementById("silverlightControlHost").innerHTML = PromptFinishUpgrade;
}
function InstallClicked() {
    window.location = "http://go2.microsoft.com/fwlink/?linkid=124807";
    document.getElementById("silverlightControlHost").innerHTML = PromptFinishInstall;
}

function CheckSupported() {
    var tst = Silverlight.supportedUserAgent();
    if (tst) {
        // Do nothing
    }
    else {
        document.getElementById("silverlightControlHost").innerHTML = PromptNotSupported;
    }
}

// Silverlight error handler.
function onSilverlightError(sender, args) {
    var appSource = "";
    if (sender != null && sender != 0) {
        appSource = sender.getHost().Source;
    }
    var errorType = args.ErrorType;
    var iErrorCode = args.ErrorCode;

    var errMsg = "Unhandled Error in Silverlight 2 Application " + appSource + "\n";

    errMsg += "Code: " + iErrorCode + "    \n";
    errMsg += "Category: " + errorType + "       \n";
    errMsg += "Message: " + args.ErrorMessage + "     \n";

    if (errorType == "ParserError") {
        errMsg += "File: " + args.xamlFile + "     \n";
        errMsg += "Line: " + args.lineNumber + "     \n";
        errMsg += "Position: " + args.charPosition + "     \n";
    }
    else if (errorType == "RuntimeError") {
        if (args.lineNumber != 0) {
            errMsg += "Line: " + args.lineNumber + "     \n";
            errMsg += "Position: " + args.charPosition + "     \n";
        }
        errMsg += "MethodName: " + args.methodName + "     \n";
    }

    throw new Error(errMsg);
}
