﻿
Type.registerNamespace('Roblox.Client');

Roblox.Client._installHost = null;
Roblox.Client._modalBehavior = null;
Roblox.Client._installSuccess = null;
Roblox.Client._CLSID = "76D50904-6780-4c8b-8986-1A7EE0B1716D";    
Roblox.Client._continuation = null;    
Roblox.Client._skip = null;    
Roblox.Client._launcherDivID = null;
Roblox.Client._isIDE = null;
Roblox.Client._isRobloxBrowser = null;

Roblox.Client.ReleaseLauncher = function(o) {
    if (o!=null)
        $get(Roblox.Client._launcherDivID).removeChild(o);
}

Roblox.Client.CreateLauncher = function(useCodebase) {
    var launcherDiv = $get(Roblox.Client._launcherDivID);
    var o = launcherDiv.children("o");
    if (!o)
    {
        var content = 'id="o" classid="clsid:' + Roblox.Client._CLSID + '"';
        if (useCodebase)
            content = content  + ' codeBase="' + Roblox.Client._installHost + '"';
        var s = '<object ' + content + '>';
        s = s + '<' + '/object>';
        launcherDiv.innerHTML = s;
        o = launcherDiv.children("o");
    }
    try
    {
        o.Hello(); // fails if object isn't fully loaded
        var host = o.InstallHost;	// fails if Roblox is not fully installed
        if (host!=Roblox.Client._installHost)
	        throw "wrong InstallHost: " + host;
        return o;
    }
    catch (ex)
    {
        launcherDiv.removeChild(o);
        return null;
    }
}

Roblox.Client.isIDE = function() {
    if (Roblox.Client._isIDE == null) {
        Roblox.Client._isIDE = false;
        Roblox.Client._isRobloxBrowser = false;

        if (window.external) {
            try {
                if (window.external.IsRobloxAppIDE !== undefined) {
                    Roblox.Client._isIDE = window.external.IsRobloxAppIDE;
                    Roblox.Client._isRobloxBrowser = true;
                }
            }
            catch (ex) {
            }
        }
    }
    return Roblox.Client._isIDE;
}

Roblox.Client.isRobloxBrowser = function() {
    Roblox.Client.isIDE();
    return Roblox.Client._isRobloxBrowser;
}

Roblox.Client.robloxBrowserInstallHost = function() {
    if (window.external) {
        try {
            return window.external.InstallHost;
        }
        catch (ex) {

        }
    }
    return "";
}

Roblox.Client.IsRobloxProxyInstalled = function() {
    var o = Roblox.Client.CreateLauncher(false);
    Roblox.Client.ReleaseLauncher(o);
    if (o != null || Roblox.Client.isRobloxBrowser())
        return true;
    return false;
}

Roblox.Client.IsRobloxInstalled = function() {
    try {
        var o = Roblox.Client.CreateLauncher(false);
        var host = o.InstallHost;
        Roblox.Client.ReleaseLauncher(o);
        // TODO: Check version, etc.
        return host == Roblox.Client._installHost;
    }
    catch (e) {
        if (Roblox.Client.isRobloxBrowser()) {
            host = Roblox.Client.robloxBrowserInstallHost();
            return host == Roblox.Client._installHost;
        }
        
        return false;
    }
}

Roblox.Client.Update = function() {
    try 
    {
        var o = Roblox.Client.CreateLauncher(false);
        o.Update();
        Roblox.Client.ReleaseLauncher(o);
    } 
    catch(e) 
    {
    }
}


Roblox.Client.WaitForRoblox = function(continuation) {
    if (Roblox.Client._skip)
    {
        window.location=Roblox.Client._skip;
        return false;
    }
    Roblox.Client._continuation = continuation;
    Roblox.Client._cancelled = false;

    if (!Roblox.Client.IsRobloxProxyInstalled())
    {
        Roblox.Client._modalBehavior.show();
        window.setTimeout(function() { Roblox.Client._ontimer(); }, 3000);
        return true;
    }
    else
    {
        Roblox.Client._continuation();
        return false;
    }
}

Roblox.Client._onCancel = function()
{
    Roblox.Client._cancelled = true;
    return false;
}

Roblox.Client._ontimer = function()
{
    if (Roblox.Client._cancelled)
        return;
        
    if (Roblox.Client.IsRobloxProxyInstalled())
    {
        Roblox.Client._modalBehavior.hide();
        Roblox.Client._continuation();
        if (Roblox.Client._installSuccess)
            Roblox.Client._installSuccess();
    }
    else
    {
        window.setTimeout(function() { Roblox.Client._ontimer(); }, 3000);
    }
}
