﻿var ua = window.navigator.userAgent;

var SilverlightVersion = "4.0";//this will need to change manually when version of silverlight changes 

if (ua.indexOf('MSIE') >= 0) {
    if (parseInt(ua.split('MSIE')[1], 10) <= 6) {
        if (getSilverlightVersion() !== "Silverlight/" + SilverlightVersion) {
            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' class='outerframeClass'>" +
                    "<div id='pleaseinstall' class='pleaseinstallClass'>" +
                        "<div id='gloss' class='glossClass'>" +
                        "</div>" + 
                        "<div id='header' class='headerClass'>" +
                        "</div>" + 
                        "<div id='content' class='contentClass'>";

var PromptPart2 = "</div>" + 
                        "<div id='bottomframe' class='bottomframeClass'>" +
                        "</div>" + 
                        "<div id='product' class='productClass'>" + 
                        "</div>" + 
                        "</div>" + 
                        "<div id='installexplanation' class='installexplanationClass'>";

var PromptPart2a = "</div>" +
                        "<div id='bottomframe' class='bottomframeClass'>" +
                        "</div>" +
                        "<div id='product' class='productClass'>" +
                        "</div>" +
                        "</div>" +
                        "</div>";

var PromptPart3 = "</div>" + 
                  "</div>";

var isIE = (ua.indexOf('MSIE') >= 0);
var isFireFox = (ua.indexOf('Firefox') >= 0);
var isChrome = (ua.indexOf('Chrome') >= 0);
var isSafari = (ua.indexOf('Safari') >= 0 && ua.indexOf('Chrome') < 0);

var isSystemSupported = "";
var isBrowserSupported = "";
var isSlInstalled = "";
var isSlCurrent = "";

var browserName = "";

// Note: mandatory redirects for mobile devices are now handled in IIS' URL Rewriter
var optionalRedirect = false;
var redirectAddress = getBasePath() + "Mobile/";
var optionalRedirectPrompt = "<p>To view michael hill products you do have the option of going to the mobile web site.</p>" +
                            "<div id = 'mobileRedirectDiv' class = 'mobileRedirectDivClass'>" +
                                "<a id='mobileRedirect' href='javascript:mobileRedirect();' title='Click to go to mobile site'>Mobile site</a>" +
                            "</div>";

if (isIE) {
    browserName = "Internet Explorer";
}
else if (isFireFox) {
    browserName = "Firefox";
}
else if (isChrome) {
    browserName = "Google Chrome";
}
else if (isSafari) {
    browserName = "Safari";
}
else {
    browserName = "unknown browser";
}

var osName = "";

if ((ua.indexOf('Windows NT 5.0') >= 0)) {
    osName = "Windows 2000";
}
else if ((ua.indexOf('Windows NT 5.1') >= 0)) {
    osName = "Windows XP";
}
else if ((ua.indexOf('Windows NT 5.2') >= 0)) {
    osName = "Windows XP x64 Edition";
}
else if ((ua.indexOf('Windows NT 6.0') >= 0)) {
    osName = "Windows Vista";
}
else if ((ua.indexOf('Windows NT 6.1') >= 0)) {
    osName = "Windows 7";
}
else if ((ua.indexOf('Windows 98') >= 0)) {
    osName = 'Windows 98';
}
else if (ua.indexOf('Intel Mac OS X') >= 0) {
    osName = "Mac OS X (Intel)";
}
else if (ua.indexOf('PPC Mac OS X') >= 0) {
    osName = "Mac OS X (PPC)";
}
else if (ua.indexOf('iPod') >= 0) {
    osName = 'iPod';
}
else if (ua.indexOf('iPhone') >= 0) {
    osName = "iPhone";
}
else if (ua.indexOf('iPad') >= 0) {
    osName = 'iPad';
}
else if (ua.indexOf('Android') >= 0) {
    osName = "Android Linux mobile";
}
else if (ua.indexOf('BlackBerry') >= 0) {
    osName = "BlackBerry";
}
else if (ua.indexOf('Windows Phone OS 7.0') >= 0) {
    osName = "Windows Phone 7";
}
else if (ua.indexOf('Windows Phone') >= 0 || ua.indexOf('Windows CE') >= 0) {
    osName = "Windows Phone";
}
else if (ua.indexOf('Nokia') >= 0 || ua.indexOf('SymbianOS') >= 0) {
    osName = "Nokia";
}
else if (ua.indexOf('Linux') >= 0) {
    osName = "Linux";
    optionalRedirect = true;
}
else {
    osName = "an unknown operating system";
    optionalRedirect = true;
}


if (isIE || isFireFox || (osName.indexOf("Mac") < 0 && isChrome) || (osName.indexOf("Mac") >= 0 && isSafari)) {
    isBrowserSupported = true;
}
else {
    isBrowserSupported = false;
}

var supportedOss = "Windows XP, Windows Vista, Windows 7, Mac OS X (Intel)";

if (supportedOss.indexOf(osName) >= 0) {
    isSystemSupported = true;
}
else {
    isSystemSupported = false;
}

var isShortVersion = "";

var PromptInstall = "<p>" +
                        "By taking just a minute to install Silverlight,  you will be able to:" +
                        "<ul>" +
                            "<li>shop online</li>" +
                            "<li>browse jewellery in a new way</li>" +
                            "<li>experience our interactive promotions</li>" +
                        "</ul>" +
                    "</p>";

var PromptUpgrade = "<p>" +
                        "Your Silverlight plugin just needs an upgrade. In only a minute, you will be able to:" +
                        "<ul>" +
                            "<li>shop online</li>" +
                            "<li>browse jewellery in a new way</li>" +
                            "<li>experience our interactive promotions</li>" +
                        "</ul>" +
                    "</p>";

var PromptReInstall = "<p>" +
                        "Your Silverlight plugin is not working properly and needs to be re-installed. In only a minute, you will be able to:" +
                        "<ul>" +
                            "<li>shop online</li>" +
                            "<li>browse jewellery in a new way</li>" +
                            "<li>experience our interactive promotions</li>" +
                        "</ul>" +
                    "</p>";

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.</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 your browser to complete the installation.</p>";
    PromptFinishInstall = "<p>Please restart your browser.</p>";
}

var reloadCount;
reloadCount = 0;

function mobileRedirect() {
    var qs = window.location.search.substring(1).toLowerCase();
    var splitString = qs.split("&");
    var param;
    var name = "sku";
    var addr = redirectAddress;
    for (i = 0; i < splitString.length; i++) {
        param = splitString[i].split("=");
        if (param[0] == name) {
            addr += "?Sku=" + param[1];
        }
    }
    window.location = addr;
}

function showUpgradeContainer() {
    trackPageView(); //track page event will only show if fallback page shows
    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";
    contentContainer.style.height = "100%";
    contentContainer.style.width = "100%";

    var dynamicContentContainer = document.getElementById("dynamicContent");
    dynamicContentContainer.style.visibility = "visible";

    var noSilverlightContainer = document.getElementById("noSilverlightDiv");
    if (noSilverlightContainer !== null) {
        noSilverlightContainer.style.display = "block";
        //noSilverlightContainer.style.visibility = "visible";
        //noSilverlightContainer.style.height = "100%";
        //noSilverlightContainer.style.width = "100%";
    }

    var ribbonFooter = document.getElementById("ribbonfooter");
    if (ribbonFooter !== null) { ribbonFooter.style.visibility = "visible"; }


    //    var productDetailContainer = document.getElementById("productDiv")
    //    productDetailContainer.style.visibility = "visible"
    //    productDetailContainer.style.height = "100%";
    //    productDetailContainer.style.display = "block";

    var SLInstallFallback = document.getElementById("SLInstallFallback");
    if (SLInstallFallback !== null) {
        SLInstallFallback.style.display = "none";
    }

}


// 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;
};

function systemPrompt() {
    var system = "";
    if (browserName !== "unknown browser" && osName !== "an unknown operating system") { 
        system = "<p> Your browser is " + browserName + " running on " + osName + "</p>";
    }
    return system;
}

function stepByStep() {
    var step;
    if (osName.indexOf("Mac OS") >= 0 && !isChrome) {
        step = "<p> Please <a id='stepByStep' class='stepByStepClass' href='InstallSL.aspx' title='Click for step by step guide'>click here</a>" +
                " to see a step by step guide on how to install " +
                "Silverlight.</p>";
    }
    else {
        step = "";
    }
    return step;
}

function recommendationPrompt() {
    var recommendation = "";
    if (!isSystemSupported) {
        recommendation = optionalRedirectPrompt;
    } else if (!isBrowserSupported) {
        recommendation = "You can install or upgrade to one of the supported browsers from the links below.";
        if (osName.indexOf('Windows') >= 0) {
            recommendation = recommendation +
                            "<p><a href='http://www.microsoft.com/windows/internet-explorer/' title='Click to install Microsoft Internet Explorer'>Microsoft Internet Explorer</a></p>" +
                            "<p><a href='http://www.google.com/chrome/' title='Click to install Google Chrome'>Google Chrome</a></p>";
        }
        else if (osName.indexOf('Mac') >= 0) {
            recommendation = recommendation +
                            "<p><a href='http://www.apple.com/safari/' title='Click to install Apple Safari'>Apple Safari</a></p>";
        }
        recommendation = recommendation + "<p><a href='http://www.mozilla.com/' title='Click to install Mozilla Firefox'>Mozilla Firefox</a></p>";

    } else if (!isSlInstalled) {
        if (osName.indexOf("Mac") >= 0) {
            recommendation = " To view the whole site please install the Silverlight plugin.";
        } else {
            recommendation = " To view the whole site please install Microsoft Silverlight. It has been certified as safe by Microsoft.";
        }
        recommendation = recommendation + stepByStep() +
                            "<div id = 'installnowDiv' class = 'installnowDivClass'>" +
                                "<a id='installnow' class='installnowClass' href='javascript:InstallClicked();' title='Click to install Silverlight'>INSTALL NOW</a>" +
                            "</div>";
    } else if (!isSlCurrent) {
        if (getSilverlightVersion() === "No Silverlight") {//current bug on safari on Mac getSilverlight returns "No SIlverlight" when version 3 is installed. workaround.
            recommendation = "However to view the whole site please upgrade the Silverlight plugin already installed on your system." + stepByStep() +
                        "<p>An older version of Silverlight is installed on your system. This website requires Silverlight " + SilverlightVersion + " or later.</p>" +
                        "<div id = 'installnowDiv' class = 'installnowDivClass'>" +
                            "<a id='installnow' class='installnowClass' href='javascript:UpgradeClicked();' title='Click to upgrade Silverlight'>UPGRADE NOW</a>" +
                        "</div>";
        } else {
            recommendation = "However to view the whole site please upgrade the Silverlight plugin already installed on your system." + stepByStep() +
                        "<p>Silverlight version " + getSilverlightVersion().replace('/', ' ') + " is installed on your system. This website requires Silverlight " + SilverlightVersion + " or later.</p>" +
                        "<div id = 'installnowDiv' class = 'installnowDivClass'>" +
                            "<a id='installnow' class='installnowClass' href='javascript:UpgradeClicked();' title='Click to upgrade Silverlight'>UPGRADE NOW</a>" +
                        "</div>";
        }
    }
    return recommendation;
}

function supportedPrompt() {
    var supported = "";
    if (isBrowserSupported && isSystemSupported) {
        supported = " Our website is designed to function well with your system. ";
    } else if (!isSystemSupported) {
        supported = "Unfortunately the silverlight plugin used extensively on this website is not supported by your operating system. ";
    } else if (isSystemSupported && !isBrowserSupported) {
        supported = "Unfortunately the silverlight plugin used extensively on this website is not supported by your browser. ";
    }
    return supported;
}

function whyPrompt() {
    var why = "";

    if (isBrowserSupported && isSystemSupported) {
        if (isShortVersion) {
            if (osName.indexOf("Mac") >= 0) {
                why = "<p>Why install Silverlight? We use smooth streaming and deep zoom technology to present our diamonds and provide you with the best possible experience.</p>";
            }
            else {
                why = "<p>Why install Microsoft Silverlight? We use smooth streaming and deep zoom technology to present our diamonds and provide you with the best possible experience.</p>";
            }
        } else {

            if (osName.indexOf("Mac") >= 0) {
                why = "<p>Why should you install the Silverlight plugin?</p>";

            }
            else {
                why = "<p>Why should you install the Microsoft Silverlight plugin?</p>";
            }
            why = why + "<p>This site uses the advanced capabilities of Silverlight to present images and video better than is currently " +
        "possible with other technology. Smooth streaming is used to show video in the highest resolution " +
       "that your system and connection can handle. Deep zoom technology is used extensively " +
       "to provide smooth loading and panning of high resolution images. <p>";
        }
    }
    return why;
}

function installExplanation() {
    var iEx;
    iEx = "<p>" + systemPrompt() + "</p><p>" + supportedPrompt() + recommendationPrompt() + "</p><p>" + whyPrompt() + "</p>";
    return iEx;
}

function reinstallExplanation() {
    var iEx;

    iEx = "<h1>Silverlight Install Issue</h1>" +
    "<p>There appears to be a problem with Microsoft Silverlight installation on this machine and it needs to be re-installed.</p>" +
    "<p>You will first need to uninstall Silverlight ; it can then be re-installed by revisiting this site.</p>" +
    "<p/>" +
            "<p style=\"text-align:center; border: thin solid #C0C0C0\" ><br/>How do I Uninstall Silverlight?<br/><br/>" +
            "See a <a style=\"text-decoration:underline\" href=\"http://go.microsoft.com/fwlink/?LinkID=98920\">video</a> on how to uninstall Silverlight on Windows XP<br/><br/>" +
            "See a <a style=\"text-decoration:underline\" href=\"http://go.microsoft.com/fwlink/?LinkID=98919\">video</a> on how to uninstall Silverlight on Windows Vista<br/><br/>" +
            "See a <a style=\"text-decoration:underline\" href=\"http://go.microsoft.com/fwlink/?LinkID=98918\">video</a> on how to uninstall Silverlight on Mac<br/><br/>" +
            "</p><p>" + whyPrompt().replace("install", "reinstall") + "</p>";
    return iEx;
}

function UpgradeClicked() {
    trackPageEvent("Install", "Upgrade Clicked", "", 0);
    // Use the silverlight js to construct the correct uri
    Silverlight.getSilverlight("4.0.50401.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("4.0.50401.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) {
    }
}

try {
    if (!isDebugging) {
        if (google.loader.ClientLocation) {
            userLocation = google.loader.ClientLocation.address.country_code;
        }
    }

} catch (err) {
    try {
        userLocation = 'Error: ' + err;
    } catch (err) {
        userLocation = 'catch 2';
    }
}

if (userLocation != null) {
    if (userLocation == 'US') {
        var title1 = document.title;
        title1 = title1.replace("Jewellery", "Jewelry");
        title1 = title1.replace("Jewellers", "Jewelers");
        document.title = title1;
    }
}

