// JavaScript Document
if (!Objekty) {var Objekty = [];};

function Tnaseptavac(idInput, URL)
{
  // id tohoto objektu
  this.id = -1;
  
  // objekt input
  this.objInput = null;
  
  // cesta ke skriptu naseptavace
  this.URL = './naseptavacPrevodu.php';
  
  // pole s hodnotama naseptavace
  this.poleHodnot = new Array();
  
  // css divu se seznamem slov
  this.cssDiv = 'naseptavac';

  // div se seznamem slov
  this.div = null;
  
  // trida css stylu pro vybranou polozku
  this.cssVybrana = 'vyb';
  
  // trida css stylu pro nevybranou polozky
  this.cssNevybrana = '';


  /**
   *  konstruktor
   */     
  this.constructor = function(idInput, URL)
  {
    // ulozi objekt
    this.id = Objekty.length;
    Objekty[Objekty.length] = this;

    // ulozi URL
    if(URL) this.URL = URL;
    
    // label pro vypsani jednotky
    this.unit = document.getElementById('dleGUID');
    //hidden input pro zapamatovani jednotky pri error
    //this.errorJednotka = document.getElementById('errorJednotka');
    
    // vytvoreni komponent
    if (this.objInput = document.getElementById(idInput))
    {
      this.objInput.changeFunc = 'Objekty['+this.id+']';
      this.objInput.onkeyup = function(e) {eval(this.changeFunc).inputOnKeyUp(e);}
      
      this.objInput.blurFunc = 'Objekty['+this.id+'].inputOnBlur();';
      this.objInput.onblur = function() {eval(this.blurFunc);}
      
    } else
      alert('id inputu neni spravne');
    
  }

  this.constructor(idInput, URL);
}

/**
 *  vykresli obdelnik
 */
Tnaseptavac.prototype.vykresli = function()
{
  if(this.div) this.zrus();
  this.div = document.createElement('div');
  this.div.className = this.cssDiv;  

  for(var i=0;i<this.poleHodnot.length;i++)
  {
    var a = document.createElement('a');
/*    var text = document.createTextNode(this.poleHodnot[i][0]+'<b>'+this.poleHodnot[i][2]+'</b>');
    a.appendChild(text);*/
    
    //a.innerHTML = this.poleHodnot[i][0]+(this.poleHodnot[i][2] ? '<span class="jednotka"> ('+this.poleHodnot[i][2]+')</span>' : '');
    a.innerHTML = this.poleHodnot[i][0];
    if(this.poleHodnot[i][1]) 
      a.className = this.cssVybrana;
    else
      a.className = this.cssNevybrana;
    
    a.clickFunc = 'Objekty['+this.id+'].aOnClick('+i+');';
    a.onclick = function() {eval(this.clickFunc)}
    a.mouseOverFunc = 'Objekty['+this.id+'].aOnMouseOver('+i+');';
    a.onmouseover = function() {eval(this.mouseOverFunc)}
    a.mouseOutFunc = 'Objekty['+this.id+'].aOnMouseOut('+i+');';
    a.onmouseout = function() {eval(this.mouseOutFunc)}
    
    this.div.appendChild(a);
  }

  this.objInput.parentNode.appendChild(this.div);
}

/**
 *  prekresli hodnoty v obdelniku
 */ 
Tnaseptavac.prototype.prekresli = function()
{
  if(!this.div) return false;

  var as = this.div.getElementsByTagName('a');

  for(var i=0;i<as.length;i++)
  {
    var a = as[i];
  
    for(var j=0;j<this.poleHodnot.length;j++)
      if(a.clickFunc == 'Objekty['+this.id+'].aOnClick('+j+');')
      {
        if(this.poleHodnot[j][1]) 
          a.className = this.cssVybrana;
        else
          a.className = this.cssNevybrana;
      }
  
  }

}

/**
 *  zrusi vykreslene okenko
 */ 
Tnaseptavac.prototype.zrus = function()
{
  if(this.div) 
  {
    this.objInput.parentNode.removeChild(this.div);
    this.div = null;
  }
}

/**
 *  stahne ze serveru naseptavace hodnoty do pole
 */
Tnaseptavac.prototype.stahniHodnoty = function()
{
  var text = this.objInput.value;
  var tmpPole = new Array();
  

  var http_request = false;
  var string = this.objInput.value;
  var request = "string="+string;

  if (window.XMLHttpRequest) {
      http_request = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
      try {
        http_request = new ActiveXObject("Msxml2.XMLHTTP");
      } catch (eror) {
        http_request = new ActiveXObject("Microsoft.XMLHTTP");
      }
  }
  /*
  for(var i in http_request)
    document.write(i+'<br />');
  function() {zpracuj(http_request, eval('Objekty[0]'));};
  */

  this.http_request = http_request;
  var func = new Function("zpracuj(Objekty["+this.id+"].http_request, Objekty["+this.id+"]);");
  http_request.onreadystatechange = func;
  http_request.open('POST', this.URL, true);
  http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; text/html;charset=windows-1250');
  http_request.send(request);
  
  
}


/**
 *  kliknuto na a
 */
Tnaseptavac.prototype.aOnClick = function(id)
{
  this.objInput.value = this.poleHodnot[id][0];
  document.getElementById('formPrevodnik').submit();
  this.zrus();
  
}

/**
 *  onmouseover a
 */ 
Tnaseptavac.prototype.aOnMouseOver = function(id)
{
  for(var i=0;i<this.poleHodnot.length;i++)
    this.poleHodnot[i][1] = false;
    this.poleHodnot[id][1] = true;
    this.prekresli();
}

/**
 *  onmouseout a
 */ 
Tnaseptavac.prototype.aOnMouseOut = function(id)
{
  for(var i=0;i<this.poleHodnot.length;i++)
    this.poleHodnot[i][1] = false;
  this.prekresli();
}

/**
 *  odznaceni inputu
 */
Tnaseptavac.prototype.inputOnBlur = function()
{ 

  // volana funkce zrus naseptavac zrusila drive, nez se stihlo provest take onClick na polozku
  // nyni pokud alespon jedna hodnota "zvolena" tak se naseptavac nerusi
  var zrusit = 0;
  
    for(var i=0;i<this.poleHodnot.length;i++)
    {
        if (this.poleHodnot[i][1] == true) {
            this.objInput.value = this.poleHodnot[i][0];
            zrusit = 1;
        }
    }
   
  if(zrusit == 0) this.zrus();
   
}

/**
 *  zmena v inputu
 */
Tnaseptavac.prototype.inputOnKeyUp = function(e)
{
  
  // dolu
  if((e ? e.keyCode : event.keyCode) == 40)
  { 
    var nastaveno = false;  
    
    for (var i=0;i<this.poleHodnot.length-1;i++) {

      if (this.poleHodnot[i][1] == true && nastaveno == false ) {
      
            this.poleHodnot[i][1] = false;
            this.poleHodnot[i+1][1] = true;
            this.objInput.value = this.poleHodnot[i+1][0];
            nastaveno = true;       
      }

    }
    if (nastaveno == false) {
        this.poleHodnot[this.poleHodnot.length-1][1] = false;
        this.poleHodnot[0][1] = true; 
        this.objInput.value = this.poleHodnot[0][0];
    }
    this.prekresli();
  }
  
  // nahoru
  else if((e ? e.keyCode : event.keyCode) == 38)
  { 
    var nastaveno = false;  
    for (var i=1;i<this.poleHodnot.length;i++) {

      if (this.poleHodnot[i][1] == true ) {
         // if (i != 0) {  
            this.poleHodnot[i][1] = false;
            this.poleHodnot[i-1][1] = true;
            this.objInput.value = this.poleHodnot[i-1][0];
            nastaveno = true;
         // }        
      }

    }
    if (nastaveno == false) {
        this.poleHodnot[0][1] = false;
        this.poleHodnot[this.poleHodnot.length-1][1] = true;
        this.objInput.value = this.poleHodnot[this.poleHodnot.length-1][0];
    }
    this.prekresli();
  }
  

  // enter
  else if((e ? e.keyCode : event.keyCode) == 13)
  {
    this.objInput.blur();
    /*for(var i=0;i<this.poleHodnot.length;i++)
    {
        if (this.poleHodnot[i][1] == true) this.objInput.value = this.poleHodnot[i][0];
    }*/
    
    this.zrus();
    //var mnozstvi = document.getElementById('mnozstvi').focus(); //a co doba?? 
  }
   

  // backspace - input prazdny ? ? ?
  else if((e ? e.keyCode : event.keyCode) == 8)
  {
    this.stahniHodnoty();
    if (this.objInput.value == "") this.zrus();
  } 
  
  else if (e ? e.keyCode : event.keyCode)
  {
      this.stahniHodnoty();
  }
     
}


function zpracuj(http_request, obj) 
{
  if (http_request.readyState == 4 && obj) 
  {
    if (http_request.status == 200) 
    {
      obj.poleHodnot = new Array();
      
      var tmpPole = http_request.responseText.split(';');
      if(!tmpPole.length) return false;
      for(var i=0;i<tmpPole.length;i++)
        tmpPole[i] = tmpPole[i].split('|');
      for(var i=0;i<tmpPole.length;i++)
        obj.poleHodnot.push([tmpPole[i][0], false, tmpPole[i][1]]);
      
      obj.vykresli();

    } //else
      //alert('Chyba: zpracuj('+http_request.status+')');     
        
  } 

}

