/*
  Babelfish - A simple Javascript language translator/dictionary.
  Copyright (C) 2004 James White
  License: LGPL
  Notes:
    - Currently only tested with English and Latvian languages.
    - Requires the cssQuery function at http://dean.edwards.name/my/cssQuery.js.html
  
  This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License
  as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version.
  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the 
  Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

function Babelfish()
{
  this.activeLanguage = '';
  this.languages = Array();
  
  /*** Language Maint ***/
  this.addLanguage = function(langName,langVocab)
  {
    langName = langName.toUpperCase();
    this.languages[langName] = langVocab;
  };
  this.hasLanguage = function(langName)
  {
    langName = langName.toUpperCase();
    if(this.languages[langName]) return(true); else return(false);
  };
  this.removeLanguage = function(langName)
  {
    langName = langName.toUpperCase();
    if (this.languages[langName])
      delete(this.languages[langName]);
  };
  this.getLanguage = function(langName)
  {
    langName = langName.toUpperCase();
    if(this.languages[langName]) return(this.languages[langName]); else return(false);
  };
  /*** Active Language ***/
  this.setActiveLanguage = function(langName)
  {
    this.activeLanguage = langName.toUpperCase();
  };
  this.getActiveLanguage = function()
  {
    return(this.activeLanguage);
  };
  /*** Translate ***/
  this.translate = function(params)
  {
    if ((typeof(params) != 'array') && (typeof(params) != 'object'))
    {
      params = Array();
      params.langName = this.getActiveLanguage();
    };
    params.langName = params.langName.toUpperCase();
    if (this.hasLanguage(params.langName)==false) { return(false); };
    if (!params.translateSource) params.translateSource = document.body;
    if (!params.matchMethod) params.matchMethod = "[translate_word]";
    var tags = cssQuery(params.matchMethod,params.translateSource);
    for(var tagidx in tags)
    {
      if(tagidx=='toJSONString')continue;
      var word=tags[tagidx].attributes.translate_word.value;  // faster, less safe?
      if (this.languages[params.langName][word])
        tags[tagidx].innerHTML = this.languages[params.langName][word];
    };
    return(true);
  };
  /*** TranslateWord ***/
  this.translateWord = function(params)
  {
    if ((typeof(params) != 'array') && (typeof(params) != 'object'))
      if (typeof(params) == 'string')
      {
        var word = params;
        params = Array();
        params.langName = this.getActiveLanguage();
        params.word = word;
      }
      else return(false);
    params.langName = params.langName.toUpperCase();
    if (this.hasLanguage(params.langName)==false) { return(false); };
    if (typeof(params['word']) != 'string') return(false);
    if (this.languages[params.langName][params.word])
      return(this.languages[params.langName][params.word]);
    return(params.word);
  };
  /*** Word Lookup ***/
  this.lookup = function(langName,word)
  {
    langName = langName.toUpperCase();
    //word = word.toLowerCase();
    if (this.languages[langName])
      if (this.languages[langName][word])
        return(this.languages[langName][word]);
    return('');
  };
};
