var langData = {};
var l_loading = "";
var l_account_deletion_success_info = "";
var l_account_deletion_error_info = "";
var l_select_country = "";
var l_required = "";
var l_error = "";
var l_username_email = "";
var l_old_password = "";
var l_password = "";
var l_change_password_error_info = "";
var l_change_password_success_info = "";
var l_select_owner = "";
var l_select_asset = "";
var l_deactivated_account = "";
var l_deactivated_account_info = "";

var l_history = "";
var l_alerts = "";
var l_live = "";
var l_tracking = "";
var l_live_tracking = "";

var selected_lang = window.localStorage.getItem("lang_selected");

if(selected_lang === null || selected_lang === undefined){
    selected_lang = "EN";
    window.localStorage.setItem("lang_selected", selected_lang);
    getLanguage(selected_lang);
    $("#current_lang").html('<span id="current_lang">'+selected_lang+' <ion-icon name="caret-down-outline" style="font-size: 11px;"></ion-icon></span>');
    $("#def_lang_"+selected_lang).addClass('lang_selected');
    $("#language_selector").val(selected_lang);
} else {
    getLanguage(selected_lang);
    $("#current_lang").html('<span id="current_lang">'+selected_lang+' <ion-icon name="caret-down-outline" style="font-size: 11px;"></ion-icon></span>');
    $("#def_lang_"+selected_lang).addClass('lang_selected');
    $("#language_selector").val(selected_lang);
}

function getLanguage(lang){
    var lang_path = "/assets/lang/"+lang+".json";
    console.log("PATH: "+lang_path);
    $.getJSON(lang_path, function(data){
        
        langData = data;
        //console.log(data);
        //console.log(data.welcome);
        
        $.each(data, function(key, value ) {
            $("#"+key).html(escapeHtml(value));
            $("."+key).html(escapeHtml(value));
            if(key == "l_loading"){
                l_loading = escapeHtml(value);
            }
            if(key == "l_account_deletion_success_info"){
                l_account_deletion_success_info = escapeHtml(value);
            }
            if(key == "l_account_deletion_error_info"){
                l_account_deletion_error_info = escapeHtml(value);
            }
            if(key == "l_select_country"){
                l_select_country = escapeHtml(value);
            }
            if(key == "l_required"){
                l_required = escapeHtml(value);
            }
            if(key == "l_error"){
                l_error = escapeHtml(value);
            }
            if(key == "l_username_email"){
                l_username_email = escapeHtml(value);
            }
            if(key == "l_old_password"){
                l_old_password = escapeHtml(value);
            }
            if(key == "l_password"){
                l_password = escapeHtml(value);
            }
            
            if(key == "l_change_password_error_info"){
                l_change_password_error_info = escapeHtml(value);
            }
            if(key == "l_change_password_success_info"){
                l_change_password_success_info = escapeHtml(value);
            }
            if(key == "l_select_owner"){
                l_select_owner = escapeHtml(value);
            }
            if(key == "l_select_asset"){
                l_select_asset = escapeHtml(value);
            }
            if(key == "l_deactivated_account"){
                l_deactivated_account = escapeHtml(value);
            }
            if(key == "l_deactivated_account_info"){
                l_deactivated_account_info = escapeHtml(value);
            }
            //
            if(key == "l_live_tracking"){
                l_live_tracking = escapeHtml(value);
            }
            if(key == "l_live"){
                l_live = escapeHtml(value);
            }
            if(key == "l_tracking"){
                l_tracking = escapeHtml(value);
            }
            if(key == "l_alerts"){
                l_alerts = escapeHtml(value);
            }
            if(key == "l_history"){
                l_history = escapeHtml(value);
            }
          //console.log( key + ": " + value );
        });
        
        
    }).fail(function(){
        console.log("An error has occurred.");
    });
}

function switchLanguage(element){
    selected_lang = $(element).val();
    window.localStorage.setItem("lang_selected", selected_lang);
    $('#loader').show();
    
    getLanguage(selected_lang);
    
    //Close Loading Gif
    pageLoaderInitClose();
    
}