/**
 * language Toggle Class for Kaco Download
 * 
 * Copyright (C) 2009 ballyhoo Werbeagentur GmbH
 * 
 */

function languageToggler(containerId) {
   var that      = this;
   var container = document.getElementById(containerId);
   var languages = [];
   
   var rows      = container.getElementsByTagName('tr');   
   this.select   = container.getElementsByTagName('select')[0];
   this.nodes    = {};
   this.current = 'en';

   for (var x = 0; x < rows.length; x++) {
      var tr = rows[x];
      if (!tr.id)
         continue;

      var id = tr.id.substr(tr.id.length - 2);
      if (!this.nodes[id]) {
         this.nodes[id] = [];         
         languages.push(id);
      }
      this.nodes[id].push({
         row: tr,
         head: tr.parentNode.parentNode.parentNode.getElementsByTagName('h3')[0]
      });       
   }

   var removeList = [];
   for (x = 0; x < this.select.options.length; x++) {
      if (languages.indexOf(this.select.options[x].value) == -1) {
         removeList.push(this.select.options[x]);
      }
   }
   for (var y=0; y<removeList.length; y++) {
      this.select.removeChild(removeList[y]);
   }
   
   this.select.onchange = function() { that.toggle(); };
}

languageToggler.prototype = {

   select     : null,
   current    : 'en',
   nodes      : {},

   toggle     : function() {
      this.setDisplay(this.current, 'none');
      this.current = this.select.options[this.select.selectedIndex].value;
      this.setDisplay(this.current, '');
   },

   setDisplay : function(key, value) {     
      for (var x = 0; x < this.nodes[key].length; x++) {           
         this.nodes[key][x].row.style.display = value;
         if (this.nodes[key][x].head) {
            this.nodes[key][x].head.style.display = (value == '' ? 'block' : value);
         }
      }
   }

}

