// 2008 (c) Todos los derecho reservados - Tradspot Servicios Lingüísticos S.L. - www.tradspot.com

google.load("language","1");

var inputLang = "es";
var outputLang = "en";
var interfaceLang = "es";

var charLimit = 500;

var languages = [
    { id : 1, code : 'sq', name : 'Albanés'},
    { id : 2, code : 'ar', name : 'Árabe'},
    { id : 3, code : 'bg', name : 'Búlgaro'},
    { id : 4, code : 'ca', name : 'Catalán'},
    { id : 5, code : 'zh-CN', name : 'Chino'},
    { id : 6, code : 'hr', name : 'Croata'},
    { id : 7, code : 'cs', name : 'Checo'},
    { id : 8, code : 'da', name : 'Danés'},
    { id : 9, code : 'nl', name : 'Holandés'},
    { id : 10, code : 'en', name : 'Inglés'},
    { id : 11, code : 'et', name : 'Estonio'},
    { id : 12, code : 'tl', name : 'Tagalo'},
    { id : 13, code : 'fi', name : 'Finés'},
    { id : 14, code : 'fr', name : 'Francés'},
    { id : 15, code : 'gl', name : 'Gallego'},
    { id : 16, code : 'de', name : 'Alemán'},
    { id : 17, code : 'el', name : 'Griego'},
    { id : 18, code : 'iw', name : 'Hebreo'},
    { id : 19, code : 'hi', name : 'Hindi'},
    { id : 20, code : 'hu', name : 'Húngaro'},
    { id : 21, code : 'id', name : 'Indonesio'},
    { id : 22, code : 'it', name : 'Italiano'},
    { id : 23, code : 'ja', name : 'Japonés'},
    { id : 24, code : 'ko', name : 'Coreano'},
    { id : 25, code : 'lv', name : 'Letón'},
    { id : 26, code : 'lt', name : 'Lituano'},
    { id : 27, code : 'mt', name : 'Maltés'},
    { id : 28, code : 'no', name : 'Noruego'},
    { id : 29, code : 'pl', name : 'Polaco'},
    { id : 30, code : 'pt', name : 'Portugués'},
    { id : 31, code : 'ro', name : 'Rumano'},
    { id : 32, code : 'ru', name : 'Ruso'},
    { id : 33, code : 'sr', name : 'Serbio'},
    { id : 34, code : 'sk', name : 'Eslovaco'},
    { id : 35, code : 'sl', name : 'Esloveno'},
    { id : 36, code : 'es', name : 'Español'},
    { id : 37, code : 'sv', name : 'Sueco'},
    { id : 38, code : 'th', name : 'Tailancés'},
    { id : 39, code : 'tr', name : 'Tuco'},
    { id : 40, code : 'uk', name : 'Ucraniano'},
    { id : 41, code : 'vi', name : 'Vietnamita'}
];

// init
function init() {
    listInputLanguages();
    $('loading').hide();
    localize(interfaceLang);
    $('message').update(charLimit+'/'+charLimit+' characters remaining');
}

// create language list
function listInputLanguages() {
    for (var i=0; i<languages.length; i++) {
        $('inputLangs').insert("<option id='lang_"+languages[i].code+"' value='"+languages[i].code+"'>"+languages[i].name+"</option>");
    }

    // defaults
    selectOption($('inputLangs'), 'es');
    updateOutputLangs('es');
}

function selectInputLanguage(lang) {
    inputLang = lang;
    selectOption($('inputLangs'), lang);

    if(lang=="ar") {
        $("input").dir = "rtl";
    } else { 
        $("input").dir = "ltr";
    }

    updateOutputLangs(lang);
}

function updateOutputLangs(newLang) {
    // clear
    $('outputLangs').update();

    for (var i=0; i<languages.length; i++) {
        $('outputLangs').insert("<option id='lang_"+languages[i].code+"' value='"+languages[i].code+"'>"+languages[i].name+"</option>");
    }

    selectOption($('outputLangs'), outputLang);
    translate();
}

function selectOutputLanguage(lang) {
    outputLang = lang;

    if(lang=="ar") {
        $("output").dir = "rtl";
    } else { 
        $("output").dir = "ltr";
    }

    translate();
}

// translate the text in input field and print to output field
function translate() {
    $('loading').show();
    var input = $('input').value;
    google.language.translate(input, inputLang, outputLang, render);
}

// see if the new word has been typed in
// if yes, translate the whole text
function translateNewWord(e) {
    if (!e) var e = window.event;
    var code = getKeyPressed(e);

    var chars = charLimit - $('input').value.length;

    if (chars < 0) {
        $('input').value = $('input').value.substring(0, charLimit - 1);
        var chars = charLimit - $('input').value.length;
    }

    // print message
    $('message').update(chars+'/'+charLimit+' characters remaining');

    // change fill
    var maxWidth = parseInt(getStyle($('statusbar'), 'width'));
    var width = Math.round(chars / charLimit * maxWidth);

    $('fill').style.width = width;

    if (!isAlnum(code)) {
        translate();
    }
}

// print the translated text into textarea
function render(result) {
    if (!result.error) {
        $('output').value = html_entity_decode(result.translation, 'ENT_QUOTES');
    } else {
        $('output').value = result.error.toSource();
    }

    $('loading').hide();
}

// get the char of the key that was pressed
function getKeyPressed(e) {
    var code = '';
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    return String.fromCharCode(code);
}

// test if the character is alphanumeric
function isAlnum(chr) {
    var regex=/^[0-9A-Za-z]+$/;
    if(regex.test(chr)) {
        return true;
    } else {
        return false;
    }   
}

function selectOption(select, val) {
    var options = select.options;
    for (var i=0; i<options.length; i++) {
        if (options[i].value==val) {
            select.selectedIndex = i;
            return;
        }
    }
}

function ident() {
    $('loading').show();
    google.language.detect($('input').value, render_ident);
}

function render_ident(result) {
    selectInputLanguage(result.language);
    $('loading').hide();
}

function getStyle(el, style) {
   if(!document.getElementById) return;
   
     var value = el.style[toCamelCase(style)];
   
    if(!value)
        if(document.defaultView)
            value = document.defaultView.
                 getComputedStyle(el, "").getPropertyValue(style);
       
        else if(el.currentStyle)
            value = el.currentStyle[toCamelCase(style)];
     
     return value;
}

function toCamelCase( sInput ) {
    var oStringList = sInput.split('-');
    if(oStringList.length == 1)    
        return oStringList[0];
    var ret = sInput.indexOf("-") == 0 ? 
       oStringList[0].charAt(0).toUpperCase() + oStringList[0].substring(1) : oStringList[0];
    for(var i = 1, len = oStringList.length; i < len; i++){
        var s = oStringList[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1)
    }
    return ret;
}

