﻿var LANGUAGE_COOKIE_NAME = 'userLanguage';
var LANGUAGE_COOKIE_OPTIONS = {
    path: '/',
    expires: 365
}
$(document).ready(function () {

    var selectedLangCode = $.urlParam('set');
    if (selectedLangCode) {
        // Preselect the country
        $("#country-picker").val(selectedLangCode);
    }
    else {
        if ($.cookie(LANGUAGE_COOKIE_NAME) != null) {
            $("#country-picker").val($.cookie(LANGUAGE_COOKIE_NAME));
        }
    }

    function getLocationToGo(langCode) {
        var basedomain = location.host;
        var domain = "http://www.bravecommunity";
        var isDevelopment = false;

        // FTI: this code below is to redirect user to the existing UK brave community site, when the country code is selected en-GB
        if (langCode == "en-GB") {
            var ukSite = "http://www.bravecommunity.co.uk/lang/?set=en-GB";
            return ukSite;
        }

        // TODO: Temp code for MLR review
        if (langCode == "en-US") {
            return "http://" + basedomain + "/Default.aspx";
        }
        else {
            return "http://" + basedomain + "/lang/Default.aspx";
        }
        // TODO: End Temp code for MLR review

        if (basedomain.indexOf("genuineinteractive") > -1) {
            domain = "http://bravecommunity-redesign.stage.genuineinteractive";
            isDevelopment = true;
        }
        else if (basedomain.indexOf("stage.") > -1) {
            domain = "http://stage.bravecommunity";
            isDevelopment = false;  //set this as false so that they can see this working on a ctp
        }
        else if (basedomain.indexOf("local") > -1) {
            domain = "local.bravecommunity-redesign";
            isDevelopment = true;
        }

        if (isDevelopment) {
            var dotcom = ".com";
            if (basedomain.indexOf("http://localhost") > -1)
                dotcom = "";
            if (langCode == "en-US")
                return domain + dotcom;

            return domain + dotcom + "/lang/?set=" + langCode;
        }

        var domainSuffix = "";
        if (langCode == "en-US") {
            domainSuffix = ".com/";
        }
        // Great Brittian
        else if (langCode == "en-GB") {
            domainSuffix = ".co.uk/lang/?set=" + langCode;
        }
        /* Brazil
        else if (langCode == "pt-BR") {
        domainSuffix = ".br/lang/Welcome.aspx?set=" + langCode;
        }*/
        // Canada
        else if ((langCode == "en-CA") || (langCode == "fr-CA")) {
            domainSuffix = ".ca/lang/Welcome.aspx?set=" + langCode;
        }
        // France
        else if (langCode == "fr-FR") {
            domainSuffix = ".fr/lang/Welcome.aspx?set=" + langCode;
        }
        /* Italy
        else if (langCode == "it") {
        domainSuffix = ".it/lang/Welcome.aspx?set=" + langCode;
        }*/
        // Germany
        else if (langCode == "de") {
            domainSuffix = ".de/lang/Welcome.aspx?set=" + langCode;
        }
        /* Mexico
        else if (langCode == "es-MX") {
        domainSuffix = ".mx/lang/Welcome.aspx?set=" + langCode;
        }*/
        // Spain
        //        else if (langCode == "es") {
        //            domainSuffix = ".es/lang/Welcome.aspx?set=" + langCode;
        //        }
        else {
            domainSuffix = ".com/lang/Welcome.aspx?set=" + langCode;
        }

        return domain + domainSuffix;
    }

    // behavior for dropdown
    $("#country-picker").change(
                function (e) {
                    // $("#dialog").dialog("close");
                    // set the language cookie
                    var langCode = $(this).attr("value");
                    $.cookie(LANGUAGE_COOKIE_NAME, langCode, LANGUAGE_COOKIE_OPTIONS);
                    var goToLocation = getLocationToGo(langCode);
                    window.location = (goToLocation);
                }
            );
});
