// resources.js

// Init vars
var languageSite;
var regionalSettings;
var defaultLanguage = "en-US";
var lcid = 1033;
var localizedStrings = {
    1033: ["en-US", "^(0?[1-9]|1[012])/(0?[1-9]|[12][0-9]|3[01])/((19|20)\\d\\d)$", "mm/dd/yyyy"],
    2057: ["en-US", "^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)$", "dd/mm/yyyy"], // UK
    1043: ["nl-NL", "^(0?[1-9]|[12][0-9]|3[01])-(0?[1-9]|1[012])-((19|20)\\d\\d)$", "dd-mm-yyyy"],
    1031: ["de-DE", "^(0?[1-9]|[12][0-9]|3[01])\.(0?[1-9]|1[012])\.((19|20)\\d\\d)$", "dd.mm.yyyy"],
    1036: ["fr-FR", "^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)$", "dd/mm/yyyy"],
    3082: ["es-ES", "^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)$", "dd/mm/yyyy"],
    2070: ["pt-PT", "^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)$", "dd/mm/yyyy"],
    1040: ["it-IT", "^(0?[1-9]|[12][0-9]|3[01])/(0?[1-9]|1[012])/((19|20)\\d\\d)$", "dd/mm/yyyy"],
    1030: ["da-DK", "^(0?[1-9]|[12][0-9]|3[01])-(0?[1-9]|1[012])-((19|20)\\d\\d)$", "dd-mm-yyyy"],
    1044: ["nb-NO", "^(0?[1-9]|[12][0-9]|3[01])\.(0?[1-9]|1[012])\.((19|20)\\d\\d)$", "dd.mm.yyyy"],
    1053: ["sv-SE", "^((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])$", "yyyy-mm-dd"]
};

$(document).ready(function () {
    SP.SOD.executeFunc('sp.js', 'SP.ClientContext', retrieveLanguage);
});

function retrieveLanguage() {
    var clientContext = SP.ClientContext.get_current();
    languageSite = clientContext.get_web();
    regionalSettings = languageSite.get_regionalSettings();

    clientContext.load(languageSite);
    clientContext.load(regionalSettings);
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQueryRetrieveLanguageSucceeded), Function.createDelegate(this, this.onQueryRetrieveLanguageFailed));
}

function onQueryRetrieveLanguageSucceeded(sender, args) {
    var languageSiteUrl = languageSite.get_url();
    lcid = languageSite.get_language();
    var localeId = regionalSettings.get_localeId();

	var siteCollectionUrl = window.location.protocol + "//" + window.location.host + _spPageContextInfo.siteServerRelativeUrl;

    // Determine language
    var language = defaultLanguage;
    if (typeof lcid != "undefined" && lcid != null && lcid != "") {
        if (localizedStrings[lcid] != null && localizedStrings[lcid] != "") {
            language = localizedStrings[lcid][0];
        }

        // UK lcid override
        if (lcid == 1033 && localeId == 2057) {
            lcid = 2057;
        }
    }

    // Load resource file
    language = language.toLowerCase();
    //console.log("Lcid: " + lcid + ", Lang: " + language + ", LocaleId: " + localeId);
    var resourcesFile = siteCollectionUrl + "/Style%20Library/Anywhere365/Content/resources/Resources." + language + ".js";
    $("head").append("<script type='text/javascript' language='javascript' src='" + resourcesFile + "'></script>");

    // Load other files
    if (typeof scriptLoadList != "undefined" && scriptLoadList != null) {
        $.each(scriptLoadList, function (key, obj) {
            var otherFile = siteCollectionUrl + "/" + obj.src;
            //console.log("Load: " + otherFile);
            if (obj.type == "js") {
                $("head").append("<script type='text/javascript' language='javascript' src='" + otherFile + "'></script>");
            }
            else if (obj.type == "css") {
                $("head").append("<link rel='stylesheet' type='text/css' href='" + otherFile + "'>");
            }
        });
    }
}

function onQueryRetrieveLanguageFailed(sender, args) {
    console.log('Language request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}