// 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 = './includes/naseptavacZkratky.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);*/


    if(this.poleHodnot[i][2] > 4) vyzn = 'významů';
    else if(this.poleHodnot[i][2] > 1 ) vyzn = 'významy';
    else if(this.poleHodnot[i][2] == 1) vyzn = 'význam';
    else vyzn = '';

    a.innerHTML = (this.poleHodnot[i][2] ? '<span class="vyznamy">'+this.poleHodnot[i][2]+' '+vyzn+' </span>' : '')+this.poleHodnot[i][0];
    //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];
//  alert(this.poleHodnot[id][0]);
  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()
{ 
  for(var i=0;i<this.poleHodnot.length;i++)
    {
        if (this.poleHodnot[i][1] == true) {
            this.objInput.value = this.poleHodnot[i][0];
            //this.unit.value = this.poleHodnot[i][2];
            //this.errorJednotka.value = "(" + this.poleHodnot[i][2] + ")";
        }
    }
   
  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+')');     
        
  } 

}




// Jine funkce 

function switchMenu(name)
{
 document.getElementById('vyber').value = name;
}

function hideObject(id)
{
	var eHide = document.getElementById(id);
	if (eHide == null) return false;

	eHide.style.visibility='hidden';
	eHide.style.display='none';
}

function showObject(id)
{
	var eShow = document.getElementById(id);
	if (eShow == null) return false;

	eShow.style.visibility='visible';
	eShow.style.display='';
}

function openDetail(url, width, height, GUID){

  Picture = window.open( url + "?GUID=" + GUID, "Obrazek", "toolbar=no, location=no, directories=no, status=no, nemubar=no, scrollbar=yes, rezisable=yes, copyhostory=no, width="+ width +", height="+ height +", top=50, left=50");

}

function vynasobKonstantou(){
  
  var div = document.getElementById('Hodnoty');
  var cislo = document.getElementById('Konstanta').value;
  
  var inputs = div.getElementsByTagName('input');

  for(var i=0;i<inputs.length;i++)
  {
    var a =  Math.round( inputs[i].value  * cislo * 1000) / 1000;
    
    inputs[i].value = a;
  
  }    
    
}

function vynasob(id){
  var div = document.getElementById(id);
  var elems = div.getElementsByTagName('input');
  
  var cislo = document.getElementById(id + 'const').value;

  for(var i=0;i<elems.length;i++) {
    var a =  Math.round( elems[i].value  * cislo * 1000) / 1000;
    elems[i].value = a;
  }
    
}

function vydel(id){
  var div = document.getElementById(id);
  var elems = div.getElementsByTagName('input');
  
  var cislo = document.getElementById(id + 'const').value;

  for(var i=0;i<elems.length;i++) {
    var a =  Math.round( elems[i].value / cislo * 1000) / 1000;
    elems[i].value = a;
  }
    
}

function vydel100(id){
  var div = document.getElementById(id);
  var elems = div.getElementsByTagName('input');
  
  var cislo = 100;

  for(var i=0;i<elems.length;i++) {
    var a =  Math.round( elems[i].value  / cislo * 1000) / 1000;
    elems[i].value = a;
  }
    
}

function display(id){

 document.getElementById(id).style.display = 'block';

 //return false;
}
function hide(id){

 document.getElementById(id).style.display = 'none';

 //return false;
}

function zmenaFormulare() {
  document.getElementById('zmeneno').value='true';
}

function byloZmeneno() {
  var zmena = document.getElementById('zmeneno').value;

  if(zmena == 'true') return true;
  else { alert('Musíte změnit alespoň jednu hodnotu.'); return false; }
}


function openImage(imgSrc, imgName, width, height){
  
  Picture = window.open("", "Obrazek", "toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, copyhostory=no, width="+ width +", height="+ height +", top=50, left=50");

  Picture.document.write("<html>\n<body style=\"margin:0px; padding: 0px;\">\n<title>" + imgName + "</title>");
  Picture.document.write("<a href=\"javascript:close()\"><img src=\"" + imgSrc + "\" alt=\"Zavřít: " + imgName + "\" border=\"0\" /></a>");
  //Picture.document.write("<br />");
  //Picture.document.write("<div style=\"width:" + width + ";text-align: center;\"><a href=\"javascript:close()\"> Zavřít </a></div>");

  Picture.document.write("\n</body>\n</html>\n");
}