﻿var ua = window.navigator.userAgent;
if (ua.indexOf('MSIE') >= 0) {

    if (parseInt(ua.split('MSIE')[1]) <= 6) {
        trackPageEvent("Install", "IE6 Redirect", "", 0);
        window.location = "./Compatible.aspx" + document.location.search;
    }
}

// this script declares a pile of variables that are used if the user doesn't have silverlight, or needs to upgrade.
// the content of the variables are injected according to the particular circumstance
var PromptPart1 = "<div id='outerframe'>" +
                    "<div id='pleaseinstall'>" +
                        "<div id='gloss'>" +
                        "</div>" +
                        "<div id='header'>" +
                        "</div>" +
                        "<div id='content'>";

var PromptPart2 = "</div>" +
                        "<div id='bottomframe'>" +
                        "</div>" +
                        "<div id='product'>" +
                        "</div>" +
                    "</div>" +
                "</div>";

var ua = window.navigator.userAgent;
var isIE = (ua.indexOf('MSIE') >= 0);

var PromptInstall = PromptPart1 + "<p>" +
                                "By taking just a minute to install Microsoft’s Silverlight technology, you will be able to:" +
                                "<ul>" +
                                    "<li>browse jewellery in a new way</li>" +
                                    "<li>experience our interactive promotions</li>" +
                                    "<li>explore the Michael Hill story</li>" +
                                "</ul>" +
                                "<a id='installnow' href='javascript:InstallClicked();' title='Click to install Microsoft Silverlight'>INSTALL NOW</a>" +
                            "</p>" + PromptPart2;

var PromptUpgrade = PromptPart1 + "<p>" +
                                "Your Microsoft Silverlight plug-in just needs an upgrade. In only a minute, you will be able to:" +
                                    "<br/>- browse jewellery in a new way" +
                                    "<br/>- experience our interactive promotions" +
                                    "<br/>- explore the Michael Hill story" +
                                "<a id='installnow' href='javascript:UpgradeClicked();' title='Click to upgrade Microsoft Silverlight'>UPGRADE NOW</a>" +
                            "</p>" + PromptPart2;

var PromptFinishInstall = "";
var PromptRestart = "<p>You will need to restart your browser following installation.</p>";
var PromptNotSupported = "<p>The Michael Hill website uses Microsoft Silverlight technology. Silverlight enhances websites with features such as smooth streaming video, which this website uses.  Unfortunately, your browser or operating system is not supported. Click 'Find a Store' to find your nearest store.</p>";
var PromptFinishUpgrade = "";

if (isIE) 
{
    PromptFinishInstall = "<p>Installing, restart browser when done.</p>";
    PromptFinishUpgrade = "<p>You will need to restart your browser following installation.</p>";
}
else 
{
    PromptFinishUpgrade = "<p>A window will prompt you to save the file to your computer. Run the file and restart to complete the installation.</p>";
    PromptFinishInstall = "<p>Please restart your browser.</p>";
}

var reloadCount;
reloadCount = 0;

function showUpgradeContainer() {
    var silverlightHost = document.getElementById("silverlightControlHost");
    silverlightHost.style.height = "0%";
    silverlightHost.style.width = "0%";
    silverlightHost.style.visibility = "hidden";

    var findstoresidebarContainer = document.getElementById("findstoresidebar");
    findstoresidebarContainer.style.visibility = "visible";

    var findstoreHeadingContainer = document.getElementById("findstoreHeading");
    findstoreHeadingContainer.style.visibility = "visible";

    var contentContainer = document.getElementById("upgradeContainer");
    contentContainer.style.visibility = "visible";

    var dynamicContentContainer = document.getElementById("dynamicContent");
    dynamicContentContainer.style.visibility = "visible";
}

function onSilverlightError(sender, args) {
    // this traps unhandled errors from silverlight.  in production, the error is swallowed and logged to google analytics
    if (args.ErrorCode == 8001) {
        trackPageEvent("Install", "Upgrade Required", "", 0);
        showUpgradeContainer();
        document.getElementById("dynamicContent").innerHTML = PromptUpgrade;
    }
    else if (args.ErrorCode == 8002) {
        document.getElementById("dynamicContent").innerHTML = PromptRestart;
    }
    else {
        var appSource = "";
        if (sender != null && sender != 0) {
            appSource = sender.getHost().Source;
        }

        var errorType = args.ErrorType;
        var iErrorCode = args.ErrorCode;

        var errMsg = "Unhandled Error" + 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";
        }
        
        if (isDebugging) {

            var errLoc = document.getElementById("errorLocation");

            errLoc.innerHTML = "<p>" + errMsg.toString() + "<p>";

            errLoc.style.height = "300px";
            errLoc.style.visibility = "visible";
        }
        else {
            // This is critical for the production environment.  Effectively, any error should reload the page and be hidden from the user.
            // However in theory it should only be catastrophic silverlight runtime errors that get through.  Application errors should be swallowed by
            // Application_UnhandledException 

            // The parameter of true is important as it forces a refresh on the server
            if (errorType == "RuntimeError") {
                reloadPage();
            }
        }
    }
}

// the following series of functions handles events from the silverlight plugin
function onSilverlightLoad(sender) {
    Silverlight.IsVersionAvailableOnLoad(sender);
}

Silverlight.onRequiredVersionAvailable = function() {};

Silverlight.onRestartRequired = function() {
    document.getElementById("content").innerHTML = PromptRestart;
};

Silverlight.onUpgradeRequired = function() {
    trackPageEvent("Install", "Upgrade Required", "", 0);
    showUpgradeContainer();
    var host = document.getElementById("dynamicContent");

    if (host) {
        host.innerHTML = PromptUpgrade;
    }
};

Silverlight.onInstallRequired = function() {
    trackPageEvent("Install", "Install Required", "", 0);

    showUpgradeContainer();
    var host = document.getElementById("dynamicContent");

    if (host) {
        host.innerHTML = PromptInstall;
    }
};

function UpgradeClicked() {
    trackPageEvent("Install", "Upgrade Clicked", "", 0);
    // Use the silverlight js to construct the correct uri
    Silverlight.getSilverlight("3.0.40818.0");

    var content = document.getElementById("content");

    if (content) {
        content.innerHTML = PromptFinishUpgrade;
    }
}

function InstallClicked() {
    trackPageEvent("Install", "Install Clicked", "", 0);
    // Use the silverlight js to construct the correct uri and trigger the get
    Silverlight.getSilverlight("3.0.40818.0");

    var content = document.getElementById("content");

    if (content) {
        content.innerHTML = PromptFinishInstall;
    }

}

// Finds the stores iframe and reveals it to the user
function ShowStores() {
    try {
        var storesFrame = document.getElementById("_storesFrame");
        storesFrame.src = "Stores.aspx?Country=" + userLocation;
        storesFrame.style.width = "100%";
        storesFrame.style.height = "100%";

        var findStoreDiv = document.getElementById("findstoresidebar");
        findStoreDiv.style.visibility = "visible";
        findStoreDiv.style.height = "80%";
        findStoreDiv.style.width = "500px";

        document.getElementById("dynamicContent").style.left = "520px";
    }
    catch (e) {
    }
}

function ShowStoresFallback() {
    try {
        var storesFrame = document.getElementById("_storesFrameFallback");
        storesFrame.src = "Stores.aspx?Country=" + userLocation;
        storesFrame.style.width = "100%";
        storesFrame.style.height = "100%";

        var findStoreDiv = document.getElementById("findstoresidebarFallback");
        findStoreDiv.style.visibility = "visible";
        findStoreDiv.style.height = "80%";
        findStoreDiv.style.width = "500px";

        document.getElementById("SLInstallFallback").style.left = "520px";
    }
    catch (e) {
    }
}