OgfGbLauncher_OgfSiteUrl = 'http://www.onegreatfamily.com';
// !! the colon is REQUIRED or the GB will cause you grief !!
// unlike the site url, do not include http://
OgfGbLauncher_GbServiceUrl = 'www.onegreatfamily.com:/z/zhttp.aspx';

OgfGbLauncher = (function () {
    var pluginMimeType = 'application/x-ogf-gblauncher-v1';

    var isEmpty = function (v) {
        return v === null || v === undefined;
    };

    //version checking lifted from Ext-core
    var ua = navigator.userAgent.toLowerCase();
    var check = function (r) {
        return r.test(ua);
    };

    var isOpera = check(/opera/);
    var isChrome = check(/chrome/) || check(/\bchrome\b/);
    var isWebKit = check(/webkit/);
    var isSafari = !isChrome && check(/safari/);
    var isSafari2 = isSafari && check(/applewebkit\/4/); // unique to Safari 2
    var isSafari3 = isSafari && check(/version\/3/);
    var isSafari4 = isSafari && check(/version\/4/);
    var isIE = !isOpera && check(/msie/);
    var isIE7 = isIE && check(/msie 7/);
    var isIE8 = isIE && check(/msie 8/);
    var isIE6 = isIE && !isIE7 && !isIE8;
    var isGecko = !isWebKit && !isChrome && check(/gecko/);
    var isGecko2 = isGecko && check(/rv:1\.8/);
    var isGecko3 = isGecko && check(/rv:1\.9/);
    var isGecko4 = isGecko && check(/rv:2\.0/);
    var isWindows = check(/windows|win32/);

    var InstallPlugin = function () {
        WaitCreateInstance();

        var ext = null;

        if (isChrome) {
            ext = 'crx';
        }
        else if (isGecko) {
            ext = 'xpi';
        }

        window.location = OgfGbLauncher_OgfSiteUrl + '/OgfGbLauncher.' + ext;
    };

    var CreateInstance = function () {
        var emel = document.createElement('embed');
        emel.setAttribute('id', 'global_gb_launcher');
        emel.setAttribute('height', '0%');
        emel.setAttribute('width', '0%');
        emel.setAttribute('type', pluginMimeType);

        document.body.appendChild(emel);
    };

    WaitCreateInstance = function () {
        setTimeout(function wait$create() {
            if (isEmpty(navigator.mimeTypes[pluginMimeType])) {
                setTimeout(wait$create, 2000);
                return;
            }

            CreateInstance();
        }, 5000);
    };

    LaunchGb = function (gbLaunchArgs) {
        var gbel = document.getElementById('global_gb_launcher');
        if (!gbel) {
            return;
        }

        var gbArgs = 'u=' + gbLaunchArgs.u + ' upwd=' + gbLaunchArgs.upwd + ' ulen=' + gbLaunchArgs.ulen +
			' g=' + gbLaunchArgs.g + ' gpwd=' + gbLaunchArgs.gpwd + ' glen=' + gbLaunchArgs.glen + ' anchor=' + gbLaunchArgs.anchor + 
            ' url=' + OgfGbLauncher_GbServiceUrl + ' init=' + gbLaunchArgs.init + ' ogfn=' + gbLaunchArgs.ogfn +
            ' gens=' + gbLaunchArgs.gens + ' token=' + gbLaunchArgs.token;

        gbel.Launch(gbArgs);
    }

    Initialize = function () {
        if (isIE || !isWindows) {
            //do nothing for now
            return;
        }

        if (isEmpty(navigator.mimeTypes[pluginMimeType])) {
            InstallPlugin();
            return;
        }
    };

    return {
        IsGbSupported: function () {
            if (isIE || !isWindows) {
                return false;
            }

            return true;
        },

        IsPluginInstalled: function () {
            return !isEmpty(navigator.mimeTypes[pluginMimeType]);
        },

        Launch: function (gbLaunchArgs) { CreateInstance(); LaunchGb(gbLaunchArgs); },

        IsOpera: function () { return isOpera; },

        IsChrome: function () { return isChrome; },

        IsIE: function () { return isIE; },

        IsFirefox: function () { return isGecko; },

        IsWindows: function () { return isWindows; },

        Initialize: function () { Initialize(); }

    };
})();

