﻿// http://msdn.microsoft.com/msdnmag/code/Default.aspx?level=root%2cscripts&file=Silverlight.js&url=http%3a%2f%2fmsdn.microsoft.com%2fmsdnmag%2fissues%2f08%2f01%2fExtremeASPNET%2fdefault.aspx
function detectAgControlVersion()
{
    var agVersion = "-1";       
    var ctrlParent = null;
    var agCtrl = null;

    try
    {
        agCtrl = new ActiveXObject('AgControl.AgControl');
    }
    catch (e)
    {
        agCtrl = navigator.plugins["Silverlight Plug-In"] ;
        if (agCtrl)
        {
            agVersion = agCtrl.description;
            if ( agVersion === "1.0.30226.2")
            {
                agVersion = "2.0.30226.2";
            }
        }
    }

    if (agVersion != "-1")
    {
        agCtrl = null;
    }
    else
    {
        try
        {
            var knownVersions = new Array();

            knownVersions[0]    = "0.90.20416.10";
            knownVersions[1]    = "0.95.20416.10";

            knownVersions[2]    = "1.0.20724.0";
            knownVersions[3]    = "1.0.20730.0";
            knownVersions[4]    = "1.0.20806.0";
            knownVersions[5]    = "1.0.20813.0";
            knownVersions[6]    = "1.0.20816.0";
            knownVersions[7]    = "1.0.20926.0";
            knownVersions[8]    = "1.0.21115.0";
            knownVersions[9]    = "1.0.30109.0";
            knownVersions[10]   = "1.0.30401.0";
            
            knownVersions[11]   = "1.1.20724.0";
            knownVersions[12]   = "1.1.20730.0";
            knownVersions[13]   = "1.1.20806.0";
            knownVersions[14]   = "1.1.20813.0";
            knownVersions[15]   = "1.1.20816.0";
            knownVersions[16]   = "1.1.20926.0";
            knownVersions[17]   = "2.0.30226";
            knownVersions[18]   = "2.0.30523";

            var versionCount = knownVersions.length;
            for (var iVer = versionCount - 1; iVer >= 0; iVer--)
            {
                if (agCtrl.IsVersionSupported(knownVersions[iVer]))
                {
                    agVersion = knownVersions[iVer];
                    break;                
                }
            }
            
            agCtrl = null;
        }
        catch(e)
        {
            // Special case for a very early version of 1.1
            if ((agCtrl != null) &&
                (typeof(agCtrl.Settings) != "undefined") && (agCtrl.Settings != null) &&
                (typeof(agCtrl.Settings.version) != "undefined") && (agCtrl.Settings.version != null) &&
                (agCtrl.Settings.version == "0.95.20416"))
            {
                agVersion = "1.1.20416.10";
            }
            
            agCtrl = null;
        }
    }

    if (ctrlParent)
    {
        document.body.removeChild(ctrlParent);
    }

    return agVersion;
}

function VersionComparison(v1, v2)
{
    var v1Array = v1.split(".");
    while (v1Array.length > 4)
    {
        v1Array.pop();
    }
    var v2Array = v2.split(".");
    while (v2Array.length > 4)
    {
        v2Array.pop();
    }
    var dimensionCount = Math.min(v1Array.length, v2Array.length);

    var result = 0;    
    for (var i=0; i<dimensionCount; i++)
    {
        try
        {
            var v1Int = parseInt(v1Array[i]);
            var v2Int = parseInt(v2Array[i]);
            if (v1Int > v2Int)
            {
                result = 1;
                break;
            }
            else if (v1Int < v2Int)
            {
                result = -1;
                break;
            }
        }
        catch (e)
        {
            result = -1;
            break;
        }
    }
    
    return result;
}

function pseudoTextAlignmentCenter(xamlElement, offset)
{
    try
    {
        var deltaForCentering = (xamlElement.Width - xamlElement.ActualWidth) / 2;
        if (typeof(offset) != "undefined")
        {
            deltaForCentering += offset;
        }
        xamlElement.SetValue("Canvas.Left", deltaForCentering);
    }
    catch(e)
    {
    }
}

doAfterSilverlightJsLoads = function()
{
    if ((typeof(Silverlight) != "undefined") && (typeof(Silverlight.onSilverlightInstalled) != "undefined") && (typeof(Silverlight.isBrowserRestartRequired) != "undefined"))
    {
        Silverlight.onSilverlightInstalled = 
            function () 
            {
                if (!Silverlight.isBrowserRestartRequired)
                {
                    if ((typeof(window.__pageSpecificSilverlightStartup) != "undefined") && (window.__pageSpecificSilverlightStartup != null) &&
                        (typeof(window.useSilverlight) != 'undefined') && (window.useSilverlight != null) && useSilverlight() &&
                        (typeof(window.Silverlight) != 'undefined') && (typeof(window.Silverlight.isInstalled) != 'undefined') && (window.Silverlight.isInstalled('1.0.20816.0')))
                    {
                        window.__pageSpecificSilverlightStartup();
                    }
                }
                else
                {
                    window.location.reload(false);
                }
            };
    }
    else
    {
        // Silverlight.js probably hasn't had a chance to load yet. Give it another couple of seconds.
        setTimeout(doAfterSilverlightJsLoads, 2000);
    }
}

generalSilverlightStartup = function()
{
    doAfterSilverlightJsLoads();
    if (window.removeEventListener)
    { 
       window.removeEventListener('load', generalSilverlightStartup , false);
    }
    else
    { 
        window.detachEvent('onload', generalSilverlightStartup);
    }
}

if (window.addEventListener) 
{
    window.addEventListener('load', generalSilverlightStartup , false);
}
else 
{
    window.attachEvent('onload', generalSilverlightStartup);
}
