﻿sheetMgr = new Qww.Sheet.Mgr(
{   
    //SheetIDs: sheetIDs,
    OnActiveSheetChanged: function(mgr, prev) 
    {
        //alert("Sheet is now " + mgr.ActiveSheet);
    } 
});

var startOverButtonMgr = new Qww.Button.Mgr({ "ObjectID": 'BU_World_Lab_Top_Section_StartOver' });


function onStartButtonClicked() {
    startOverButtonMgr.SendClick();
}

function Factlab_ResetCurrentSiteAndNavigateToOtherSite(url) {
    onStartButtonClicked();

    var selectedLanguage = languageMaintainer.GetSelectedLanguage();

    // If the above returned "" then the language has not been changed.
    if (selectedLanguage != "") url = url + "?setlanguage=" + selectedLanguage;

    window.location = url;
};

function ShowSettingsPopup() {
    tb_show('Settings', "#TB_inline?height=150&width=300&inlineId=CountrySourcePopupCtl1_visitorConfirmationPopUp", false);
};

var popUpVisibilityListboxMgr = new Qww.ListBox.Mgr({ "ObjectID": 'LB813' });

sheetMgr.ScanForPanelsToManage();

Qww.Ctls.Debug = false;

Qww.Ctls.DropDown.Mgr.ScanForAndCreate();
Qww.Ctls.TextObject.Mgr.ScanForAndCreate();
Qww.Ctls.Button.Mgr.ScanForAndCreate();
Qww.Ctls.AmMap.Mgr.ScanForAndCreate();
Qww.Ctls.Chart.Mgr.ScanForAndCreate();
Qww.Ctls.LeftMenu.Mgr.ScanForAndCreate();
Qww.Ctls.Search.Mgr.ScanForAndCreate();
Qww.Ctls.CustomTableRenderer.Mgr.ScanForAndCreate();
Qww.Ctls.CustomCheckBoxList.Mgr.ScanForAndCreate();
Qww.Ctls.GoogleMap.Mgr.ScanForAndCreate();

function LanguageMaintainer(languageTextBoxId, lanaguageListBoxId) {

    function getQueryVar(variable) {
        var query = window.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
        return null;
    }

    this.GetInitialisationSets = function() {

        var sets = [];

        var language = getQueryVar("setlanguage");

        if (language && language != '') {
            var objectId = lanaguageListBoxId;

            var applicationID = null;

            // code copied from Qww.ListBox.Mgr.SearchAndClose
            sets.push(new Qww.QvsConnectorSet(null, Qww.ListBox.Mgr.CommandPrefix + objectId + ".Caption", "search", language, false));
            sets.push(new Qww.QvsConnectorSet(null, Qww.ListBox.Mgr.CommandPrefix + objectId + ".Caption", "closesearch", "accept", true));
        }

        return sets;
    };

    var initialLanguage = "";
    
    function onLanguageUpdated(txtMgr)
    {
        if (initialLanguage == "") initialLanguage = txtMgr.Text;
    };
    
    var txtObj = new Qww.TextObject.Mgr({ 'ObjectID': languageTextBoxId, OnUpdate: onLanguageUpdated});

    this.GetSelectedLanguage = function() {
    
    return txtObj.Text;
    };

    $(document).ready(function() {
        
        $("[jumptosite]").each(function() {

            var elem = $(this);
            var siteId = elem.attr('jumptosite');

            $(elem).click(function(e) {
                window.location = siteId + "/default.aspx?setlanguage=" + txtObj.Text;
                //window.location = siteId + "/default.aspx";
                return true;
            });
        });

        //        var language = getQueryVar("language");

        //        if (language && language != '') {
        //            setTimeout(function() {
        //                //Qww.ListBox.Mgr.SearchAndClose(null, lanaguageListBoxId, language, true);
        //            }, 2500); // seems we need a delay here otherwise page hangs. Should look into sending this in initial request.
        //        }

    });

    qwwHub.Register(this);
}

// This object checks for a language=??? in the query string and updates the selected language 
// accordingle. It also wires up and elements with a jumptosite=?? attribute to navigate to the 
// specified site with the current selected language
var languageMaintainer = new LanguageMaintainer('TX_SELECTED_LANGUAGE', 'LB_FactLab_Global_or_National_Langselector');