﻿// Javascript to localize the Text2Go website
// Copyright 2008 Tumbywood Software

var defaultLocale = 
{
    "trial-voice-name" : "Samantha",
    "trial-voice-size" : "109",
    "full-trial-voice-name" : "US English Samantha",
    "trial-voice-download" : "/portals/0/TrialVoices/Text2GoTrialSamanthaVoiceSetup.msi",
    "home-male-sample": "/portals/0/SpeechSamples/IVONA-Brian-main.mp3",
    "home-male-sample-alt" : "Listen to a Male Voice (UK English Brian)",
    "home-female-sample": "/portals/0/SpeechSamples/IVONA-Amy-main.mp3",
    "home-female-sample-alt" : "Listen to a Female Voice (UK English Amy)"
};

var australianLocale = 
{
    "trial-voice-name" : "Karen",
    "full-trial-voice-name" : "Australian English Karen",
    "trial-voice-size" : "109",
    "trial-voice-download" : "/portals/0/TrialVoices/Text2GoTrialKarenVoiceSetup.msi",
    "home-male-sample": "/portals/0/SpeechSamples/IVONA-Brian-main.mp3",
    "home-male-sample-alt": "Listen to a Male Voice (UK English Brian)",
    "home-female-sample": "/portals/0/SpeechSamples/IVONA-Amy-main.mp3",
    "home-female-sample-alt": "Listen to a Female Voice (UK English Amy)"
};

var ukLocale = 
{
    "trial-voice-name" : "Daniel",
    "trial-voice-size" : "97",
    "full-trial-voice-name" : "UK English Daniel",
    "trial-voice-download" : "/portals/0/TrialVoices/Text2GoTrialDanielVoiceSetup.msi",
    "home-male-sample": "/portals/0/SpeechSamples/IVONA-Brian-main.mp3",
    "home-male-sample-alt": "Listen to a Male Voice (UK English Brian)",
    "home-female-sample": "/portals/0/SpeechSamples/IVONA-Amy-main.mp3",
    "home-female-sample-alt": "Listen to a Female Voice (UK English Amy)"
};

var spanishLocale = 
{
    "trial-voice-name" : "Monica",
    "trial-voice-size" : "100",
    "full-trial-voice-name" : "Spanish Monica",
    "trial-voice-download" : "/portals/0/TrialVoices/Text2GoTrialMonicaVoiceSetup.msi",
    "home-male-sample" : "/portals/0/SpeechSamples/Realspeak-Diego-long-speech-sample.mp3",
    "home-male-sample-alt" : "Listen to a Male Voice (Spanish Diego)",
    "home-female-sample" : "/portals/0/SpeechSamples/Realspeak-Monica-long-speech-sample.mp3",
    "home-female-sample-alt" : "Listen to a Female Voice (Spanish Monica)"
};


var localResources =
{
    "en-au" : australianLocale,
    "en-nz" : australianLocale, // Sorry NZ - it's the closest we've got.
    
    "en-gb" : ukLocale,
    "en-ie" : ukLocale,
    "en-za" : ukLocale,
    
    "es" : spanishLocale
};

function getLocalResource(strId)
{
    var currentLanguage = "en-us";
    if(navigator.userLanguage != undefined)
    {
        currentLanguage = navigator.userLanguage.toLowerCase();
    }
    else if(navigator.systemLanguage != undefined)
    {
        currentLanguage = navigator.systemLanguage.toLowerCase();
    }
    else if(navigator.language != undefined)
    {
        currentLanguage = navigator.language.toLowerCase();
    }
    var currentResources = defaultLocale;
    if(localResources[currentLanguage] != undefined)
    {
        currentResources = localResources[currentLanguage];
    }
    else if(currentLanguage.length > 2)
    {
        currentLanguage = currentLanguage.slice(0,2);
        if(localResources[currentLanguage] != undefined)
        {
            currentResources = localResources[currentLanguage];
        }
    }
    var resource = currentResources[strId];
    return resource != undefined ? resource : "";
}

function localizeElement(elementId, strId)
{
    localizeElementProperty(elementId, "innerHTML", strId);
}

function localizeHRef(elementId, strId)
{
    localizeElementProperty(elementId, "href", strId);
}

function localizeAlt(elementId, strId)
{
    localizeElementProperty(elementId, "alt", strId);
}

function localizeElementProperty(elementId, propertyName, strId)
{
    var elem = document.getElementById(elementId);
    var localStr = getLocalResource(strId);
    if(elem != null && localStr != null)
    {
        elem[propertyName] = localStr;
    }
}
