/**
 * Projekt PPF.cz
 * No-flash verze vyberu jazyka na intro strane webu PPF
 * Marek Lapis <marek at lapis dot cz>
 * 20.12 2006
 */

// Globals
var languages;

// Onload function
function loadLanguages (directory, defaultLangNumber)
{
  languages = new Languages (directory);
  //languages.showLanguage(defaultLangNumber);
}

// class Languages : begin
function Languages (dir)
{
  this.dir            = this.initDir(dir);

  this.numberOfLangs  = 2; //4;
  this.activeLang     = -1;
  this.imgMapDefault  = this.initImgMapDefault();
  this.langs          = this.initLangs();
  this.domDiv         = this.initDomDiv();
  this.domMenuAnchors = this.initMenuDomAnchors();
}

Languages.prototype.initDir = function (dirname)
{
  var dir = dirname;
  if (dirname != "")
    dir += '/';
  return dir;
}

Languages.prototype.initImgMapDefault = function ()
{
  var img = new Image();
  img.src = this.dir + 'images/noflash/mapa_off.jpg' ;
  return img;
}

Languages.prototype.initLangs = function ()
{
  var langs = new Array();
  
  for (var i = 0; i < this.numberOfLangs; i++)
  {
    var id = i;
    langs[i] = {
      id          : id, 
      imgMapOut   : new Image(), 
      imgMapOver  : new Image(),
      imgMenuOut  : new Image(),
      imgMenuOver : new Image()
    };
    langs[i].imgMapOut.src   = this.imgMapDefault.src ;
    langs[i].imgMapOver.src  = this.dir + 'images/noflash/mapa' + id + '.jpg' ;
    langs[i].imgMenuOut.src  = this.dir + 'images/noflash/lang-' + id + '-out.jpg' ;
    langs[i].imgMenuOver.src = this.dir + 'images/noflash/lang-' + id + '-over.jpg' ;
  }
  return langs;
}

Languages.prototype.initDomDiv = function ()
{
  var anchor = document.getElementById('langMap');
  return anchor ? anchor : null;
}

Languages.prototype.initMenuDomAnchors = function ()
{
  var menuHolder = document.getElementById('langMenuIntro');

  if (menuHolder)
  {
    var anchors = menuHolder.getElementsByTagName('a');

    if (!anchors)
      return null;

    return anchors;
  }
  else
    return null;
}

Languages.prototype.setMenuOverState = function ()
{
  if (!this.domMenuAnchors)
    return null;

  var indx = this.activeLang;
  for (var i = 0; i < this.domMenuAnchors.length; i++) {
    (i == indx)
      ? this.setStyleBgImage (this.domMenuAnchors[i], this.langs[ i ].imgMenuOver.src )
      : this.setStyleBgImage (this.domMenuAnchors[i], this.langs[ i ].imgMenuOut.src );  
  }
  return true;
}

Languages.prototype.showLanguage = function (langNumber)
{
  if (this.domDiv)
  {
    this.setActiveLang(langNumber);
    this.setMenuOverState();
    this.setMapOverState();

    return true;
  }
  return false;
}

Languages.prototype.hideLanguage = function ()
{
  if (this.domDiv)
  {
    this.setActiveLang(-1);
    this.setMenuOverState();
    this.setMapOutState();

    return true;
  }
  return false;
}

Languages.prototype.setMapOverState = function ()
{
  if ( this.domDiv && this.activeLang >= 0 && this.activeLang < this.numberOfLangs )
  {
    this.setStyleBgImage (this.domDiv, this.langs[ this.activeLang ].imgMapOver.src);
    return true;
  }
  return false;
}

Languages.prototype.setMapOutState = function ()
{
  if ( this.domDiv && this.activeLang == -1 )
  {
    this.setStyleBgImage (this.domDiv, this.imgMapDefault.src);
    return true;
  }
  return false;
}

Languages.prototype.setStyleBgImage = function (element, src)
{
  element.style.backgroundImage = "url(" + src + ")";
}

Languages.prototype.setActiveLang = function (langNumber)
{
  this.activeLang = langNumber;
}

// class Languages : end
