/**
 * Enkel function för att översätta nycklar i Java script.
 *
 * @package Skipass
 */
AppLanguage = Class.create();
AppLanguage.prototype =
{
    initialize: function(site,lang)
    {
      this.site = site;
      this.siteTag = site.toUpperCase().substr(0,1);
      this.lang = lang;

      this.r2Texts = $H();
      this.shortTexts = $H();
      this.requestMissing = false;
    },
    cacheR2Texts: function(r2texts)
    {
      var me = this;
      r2texts.each(function(pair) { me.r2Texts[pair.key] = pair.value; });
    },
    cacheShortTexts: function(texts)
    {
      var me = this;
      texts.each(function(pair) { me.shortTexts[pair.key] = pair.value; });
    },
    getText: function(key,domain)
    {
      if(domain && domain == "R2")
          return this._getR2Text(key);
      else
          return this._getShortText(key);
    },
    _getShortText: function(k)
    {
      var t = this.shortTexts[k];

      if(t)
          return t;
      else
          return this._missing(k);
    },
    _getR2Text: function(k)
    {
       // OK, fix key
       if(k.substr(0,3) == 'TXT')
       {
         var firstChoice = "TXT" + this.siteTag + k.substr(4);

         var t = this.r2Texts[firstChoice];
         if(t)
             return t;
       }

       var t = this.r2Texts[k];

       if(t)
           return t;
       else
           return this._missing(k,'R2');
    },
    _missing: function(k,domain)
    {
      if(this.requestMissing)
      {
          this.callResult = "";

          var p = $H({site: this.site, lang: this.lang,key: k, domain: domain });
          new Ajax.Request('/app/projects/common/templates/applang/api/gettext.php',{method: 'get',parameters: p,asynchronous: false,onSuccess: this.onSuccess.bind(this)});
          
          if(this.callResult.length > 0)
          {
            if(domain && domain == "R2")
                this.r2Texts[k] = this.callResult;
            else
                this.shortTexts[k] = this.callResult;
                
            return this.callResult;
          }
      }
      alert('Missing: ' + k + domain);
      return k;
    },
    onSuccess: function(transport)
    {
        this.callResult = transport.responseText;
    }
};
/*
Ex:
var bAppLang = new AppLanguage('are','sv');

bAppLang.cacheShortTexts($H({one: 'ett', two: 'två'}));
bAppLang.cacheR2Texts($H({TXTABOK1: 'Åre boka1', TXTXBOK1: 'Common boka1'}));

alert(bAppLang.getText('one')); => ett
alert(bAppLang.getText('TXTXBOK1')); => Åre boka1

*/

showInlineHelp=function(txtkey,txttitle,site,lang,titlecat)
{
  if(!titlecat)
      titlecat = "";
      
    IP_show(this.title,'/app/projects/common/templates/help/inlineMain.php?KeepThis=true&txtKey=' + txtkey + '&titleKey=' + txttitle + '&titleCat=' + titlecat + '&bLang='+lang + '&bSite='+site +'&TB_iframe=true&height=400&width=300');
}