var problematicSL11Version = "1.1.20416.10"; // this version has problems, it was a very early alpha
var requirementsAnim = null;
var instructionsAnim = null;
var minimalInstallContentAnim = null;
var xAnim = null;
var movedMinimalInstallContent = false;
var slPresentOnLoad = false;
var lastDetectedVersion = null;
var hash = null;
var probe = null;

pageLoad = function()
{
    if ((typeof(window.isMac) == "undefined") || (window.isMac == null) || window.isMac())
    {
        // We only need the probe (which is sometimes used by the JS function detectAgControlVersion)
        // on the Mac. So we only need to build it if we are on a Mac or if we can't figure out if we are on
        // a Mac.
        initProbe();    
    }
        
    if (lastDetectedVersion == null)
    {
        var detectedVersionHiddenField = document.getElementById("ctl00_MainContent_DetectedVersionHiddenField");
        if (detectedVersionHiddenField.value == "-2")
        {
            // The hidden field is initialized to -2. So if we want to know if SL was present on this client
            // computer when this page FIRST loaded then we need to make sure that we are remembering the
            // state of SL's presence only when this hidden first is still -2, that is, when the page is
            // absolutely fresh. After that, the user may choose to install SL, which will cause the
            // UpdatePanel to refresh and the page will no longer be able to tell us if SL was or wasn't
            // present when it was first loaded.
            //
            // We determine if SL is initially present by looking at the version number that of SL that was
            // found when the page first loaded (i.e., when the hidden field still was set to -2).
            // If SL is present then the version variable passed to this function will be something like
            // 2.0.30226. If SL isn't installed on the client then the version is set to -1. This explains
            // the following boolean expression...
            slPresentOnLoad = detectAgControlVersion(probe) != "-1";
            
            // UpdatePanel can eat the hash info on the URL so remember it now.
            hash = parent.location.hash.toLowerCase();
        }
        else
        {
            // Firefox has the odd characteristic that it doesn't clear its form variables if you "refresh"
            // the page though it does if you re-navigate explicitly to the page. So we also need to deal
            // with the case where the page is essentially being reloaded (signified by the fact that 
            // lastDetectedVersion is null) but our form isn't really reset (signified by the fact that
            // detectedVersionHiddenField.value is something other than -2.
            slPresentOnLoad = detectedVersionHiddenField.value != "-1";
        }
    }

    if ((typeof(window.setTimeout) != "undefined") && (window.setTimeout != null))
    {
        window.setTimeout("testVersionInfo()", 1000);
    }    
}

initProbe = function()
{
    if ((typeof(window.Silverlight) != "undefined") && (window.Silverlight != null) && (typeof(window.Silverlight.isInstalled) != "undefined") && (window.Silverlight.isInstalled != null) && window.Silverlight.isInstalled(null))
    {
        var container = document.getElementById("probe-container");
        if (container != null)
        {
            Silverlight.createObjectEx({
                source: "#probe-xaml",
                parentElement:container,
                id:"probe",
                properties:{
                    inplaceInstallPrompt: false,
                    width:"1",
                    height:"1",
                    background:"black",
                    version:"1.0"},
                events:{
                    onLoad: probeLoaded,
                    onError: function() {}}}); 
        }
    }
}

probeLoaded = function(pluginHost, userContext, rootCanvas)
{
    probe = document.getElementById("probe");
}

testVersionInfo = function()
{
    var detectedVersion = detectAgControlVersion(probe);
    if (lastDetectedVersion != detectedVersion)
    {
        if (RestartRequired(null))
        {
            // We've gone from a state where SL is present to one where it is absent (or we've upgraded
            // from SL 1 to 2 RTW). We need to restart the browser.
            
            var restartRequiredHiddenField = document.getElementById("ctl00_MainContent_RestartRequiredHiddenField");
            if (restartRequiredHiddenField != null)
            {
                restartRequiredHiddenField.value = "true";
            }
        }
        lastDetectedVersion = detectedVersion;
        forceServerUpdate(detectedVersion);
    }
    lastDetectedVersion = detectedVersion;
    window.setTimeout("testVersionInfo()", 5000);
}

// Because we use testVersionInfo (on a repeating timer) to continually watch for the visitor installing
// a new version of SL, we don't need to use the usual heuristic that reloads the whole page when SL is
// newly installed. We can disable that by defining our own method for that situation that is called instead
// of window.reload (if it that special method is defined). Look in scripts\silverlight\general.js at the
// doAfterSilverlightJsLoads method.
__pageSpecificSilverlightStartup = function() {}

forceServerUpdate = function(version)
{
    var detectedVersionHiddenField = document.getElementById("ctl00_MainContent_DetectedVersionHiddenField");
    if (detectedVersionHiddenField != null)
    {
        detectedVersionHiddenField.value = version;
        __doPostBack("ctl00_MainContent_DetectedVersionHiddenField","");
    }
}

renderVersionInfo = function() {
    requirementsAnim = $find("RequirementsBehavior").get_OnClickBehavior().get_animation();
    instructionsAnim = $find("InstructionsBehavior").get_OnClickBehavior().get_animation();
    minimalInstallContentAnim = $find("MinimalInstallContentBehavior").get_OnClickBehavior().get_animation();
    xAnim = minimalInstallContentAnim.get_animations()[0].get_animations()[0];
    createJitAnimations();

    if (hash == null) {
        hash = parent.location.hash.toLowerCase();
    }
    else if (hash.length > 0) {
//        parent.location.hash = hash;
    }

    var mode = "";
    if ((typeof (window.getQueryString) != "undefined") && (window.getQueryString != null)) {
        mode = window.getQueryString("mode").toLowerCase();
    }

    if ((hash.indexOf("sysreq") > -1) || (mode == "sysreq")) {
        //showSystemRequirements();
    }
    else if ((hash.indexOf("instruct") > -1) || (mode == "instruct")) {
        //showInstallationInstructions();
    }
    else if ((typeof (window.isSilverlightSupported) != "undefined") && (!isSilverlightSupported)) {
        // If we are being visited by a browser that doesn't actually support Silverlight (can't be installed)
        // show the system requirements to give the visitor some clue as to what is happening.
        //showSystemRequirements();
    }

    var detectedVersion = detectAgControlVersion(probe);
    if ((!slPresentOnLoad) && (detectedVersion != -1)) {
        var showOffContainer = document.getElementById("show-off");
        if (showOffContainer != null) {
            showOffContainer.style.display = "block";
        }
    }
}

trueHistoryBack = function()
{
    if((typeof(document.referrer) != 'undefined') && (document.referrer != null) && (document.referrer.length > 0))
    {
        window.location.href = document.referrer;
    }
    else
    {
        history.back();
    }
}

moveMinimalInstallContent = function() {
    if (!movedMinimalInstallContent) {
        var elementToMove = document.getElementById("minimal-install-content");
        var mainContentContainer = document.getElementById("main-content");

        if ((minimalInstallContentAnim != null) && (xAnim != null) && (elementToMove != null) && (mainContentContainer != null)) {
            var mainContentOffsetLeft = Math.min(-1 * mainContentContainer.offsetLeft, -25);
            elementToMove.style.left = (elementToMove.offsetLeft - mainContentOffsetLeft).toString() + "px";
            elementToMove.style.marginLeft = "0";
            elementToMove.style.marginRight = "0";

            xAnim.set_startValue(elementToMove.style.left);
            xAnim.set_endValue(mainContentOffsetLeft);

            minimalInstallContentAnim.play();

            movedMinimalInstallContent = true;
        }
    }
    if (!movedMinimalInstallContent) {
        // On rare occasions we need to retry this because the DOM isn't yet fully built. This appears to be
        // a threading (timing) problem but it could be the hint of something more complicated.
        setTimeout("moveMinimalInstallContent()", 2000);
    }
}

showSystemRequirements = function()
{
    //moveMinimalInstallContent();
    //requirementsAnim.play();
    //parent.location.hash = "sysreq";
}

showInstallationInstructions = function()
{
    //moveMinimalInstallContent();
    //instructionsAnim.play();
    //parent.location.hash = "instruct";
}

createJitAnimations = function()
{
    // first (1) animation collection
    var animCollection = minimalInstallContentAnim.get_animations()[0];
    var animTarget = findElementByClass("DIV", "InstallSuccessAnimation");
    if (animTarget != null)
    {
        var newAnim = new AjaxControlToolkit.Animation.LengthAnimation();
        newAnim.set_target(animTarget);
        newAnim.set_property("style");
        newAnim.set_propertyKey("top");
        newAnim.set_startValue(137);
        newAnim.set_endValue(25);
        newAnim.set_unit("px");
        animCollection.add(newAnim);
    }
 
    // second (2) animation collection
    animCollection = minimalInstallContentAnim.get_animations()[1];
    animTarget = findElementByClass("DIV", "InstallSuccessAnimation");
    if (animTarget != null)
    {
        var newAnim = new AjaxControlToolkit.Animation.LengthAnimation();
        newAnim.set_target(animTarget);
        newAnim.set_property("style");
        newAnim.set_propertyKey("left");
        newAnim.set_startValue(95);
        newAnim.set_endValue(260);
        newAnim.set_unit("px");
        animCollection.add(newAnim);
    }
    animTarget = findElementByClass("DIV", "sl-not-yet-installed");
    if (animTarget != null)
    {
        var newAnim = new AjaxControlToolkit.Animation.LengthAnimation();
        newAnim.set_target(animTarget);
        newAnim.set_property("style");
        newAnim.set_propertyKey("marginLeft");
        newAnim.set_startValue(112);
        newAnim.set_endValue(30);
        newAnim.set_unit("px");
        animCollection.add(newAnim);
    }    
    animTarget = findElementByClass("DIV", "sl-is-installed");
    if (animTarget != null)
    {
        var newAnim = new AjaxControlToolkit.Animation.LengthAnimation();
        newAnim.set_target(animTarget);
        newAnim.set_property("style");
        newAnim.set_propertyKey("marginLeft");
        newAnim.set_startValue(112);
        newAnim.set_endValue(30);
        newAnim.set_unit("px");
        animCollection.add(newAnim);
    }    
    animTarget = findElementByClass("DIV", "latest-version-installed");
    if (animTarget != null)
    {
        var newAnim = new AjaxControlToolkit.Animation.LengthAnimation();
        newAnim.set_target(animTarget);
        newAnim.set_property("style");
        newAnim.set_propertyKey("marginLeft");
        newAnim.set_startValue(40);
        newAnim.set_endValue(0);
        newAnim.set_unit("px");
        animCollection.add(newAnim);
    }    
    animTarget = findElementByClass("DIV", "old-version-installed");
    if (animTarget != null)
    {
        var newAnim = new AjaxControlToolkit.Animation.LengthAnimation();
        newAnim.set_target(animTarget);
        newAnim.set_property("style");
        newAnim.set_propertyKey("marginLeft");
        newAnim.set_startValue(40);
        newAnim.set_endValue(0);
        newAnim.set_unit("px");
        animCollection.add(newAnim);
    }    
    animTarget = findElementByClass("DIV", "install-extra-actions");
    if (animTarget != null)
    {
        var newAnim = new AjaxControlToolkit.Animation.LengthAnimation();
        newAnim.set_target(animTarget);
        newAnim.set_property("style");
        newAnim.set_propertyKey("marginLeft");
        newAnim.set_startValue(112);
        newAnim.set_endValue(30);
        newAnim.set_unit("px");
        animCollection.add(newAnim);
    }    
}
