var naseptavac = function (inputNaseptavace, obsahNaseptavace) {
this.cache = [];
this.xmlHttp = vytvorXmlHttpRequestObjekt ();
this.odesli = function (obsluha, id, argumenty) {
//pridej do cache
if (obsluha) {
var query = "obsluha=" + encodeURIComponent (obsluha);
query += "&id=" + encodeURIComponent (id);
query += "&jazyk=" + encodeURIComponent (jazyk);
for (var i = 0; i < argumenty.length; i++)
query += "&hodnota" + (i + 1) + "=" + encodeURIComponent (argumenty[i]);
this.cache.push (query);
}
if ((this.xmlHttp.readyState == 4 || this.xmlHttp.readyState == 0) && this.cache.length > 0) {
var cachePolozka = this.cache.pop ();
this.cache = [];
this.xmlHttp.open ("POST", ajaxCesta, true);
this.xmlHttp.setRequestHeader ("Content-Type", "application/x-www-form-urlencoded");
this.xmlHttp.setRequestHeader ("Content-length", cachePolozka.length);
this.xmlHttp.setRequestHeader ("Connection", "close");
var objekt = this;
this.xmlHttp.onreadystatechange = function () {objekt.zpracuj()};
this.xmlHttp.send (cachePolozka);
}
}
this.zpracuj = function () {
if (this.xmlHttp.readyState == 1)
zaneprazdneno.zobraz ();
else if (this.xmlHttp.readyState == 4) {
zaneprazdneno.skryj ();
if (this.xmlHttp.status == 200) {
this.vypis ();
}
else {
//dokonceno s chybou prenosu, napr. server neodpovedel, server je spadly apod... moznost zkusit znovu
//this.xmlHttp.statusText
}
//zpracuj dalsi polozky v cache
this.odesli ();
}
}
this.vypis = function () {
var xmlDokument = this.xmlHttp.responseXML.documentElement;
var pocet = 0;
var htmlVysledek = '';
this.poleAkci = new Array ();
var vysledek = xmlDokument.getElementsByTagName ("vyber")[0].childNodes;
for (var i = 0; i < vysledek.length; i++) {
if (vysledek.item (i).nodeName == 'polozka') {
var akce = vysledek.item (i).getElementsByTagName ("akce")[0];
var html = vysledek.item (i).getElementsByTagName ("html")[0].childNodes[0].nodeValue;
this.poleAkci[pocet] = akce;
htmlVysledek += '
| ' + html + ' |
';
pocet++;
}
}
//schvalne opozdene, at se s tim nepracuje drive
this.pozice = -1;
if (pocet > 0) {
this.pocetVysledku = pocet;
this.obsahNaseptavace.innerHTML = '';
this.obsahNaseptavace.style.display = 'block';
this.obsahNaseptavace.scrollTop = 0;
this.odsazeni = 0;
var objekt = this;
this.poleVysek = new Array ();
for (var i = 0; i < pocet; i++) {
var prvek = document.getElementById ('naseptavac_' + i);
this.poleVysek[i] = prvek.offsetHeight;
prvek.onclick = function () {objekt.klik (objekt.nactiPozici (this.id));};
prvek.onmouseover = function () {objekt.nastavPozici (objekt.nactiPozici (this.id));};
}
}
else {
this.pocetVysledku = 0;
this.obsahNaseptavace.innerHTML = '';
this.obsahNaseptavace.style.display = 'none';
}
}
this.klik = function (pozice) {
var druh = this.poleAkci[pozice].getElementsByTagName ("druh")[0].childNodes[0].nodeValue;
switch (druh) {
case 'presmerovat':
var url = this.poleAkci[pozice].getElementsByTagName ("url")[0].childNodes[0].nodeValue;
location.href = url;
break;
case 'vyplnit':
var retezec = this.poleAkci[pozice].getElementsByTagName ("retezec")[0].childNodes[0].nodeValue;
this.inputNaseptavace.value = retezec;
this.skryj ();
break;
};
}
this.nactiPozici = function (prvek) {
var cislo = prvek.split ('_');
return Number (cislo[1]);
}
this.nastavPozici = function (pozice) {
if (this.pozice != -1)
document.getElementById ('naseptavac_' + this.pozice).className = '';
this.pozice = pozice;
document.getElementById ('naseptavac_' + this.pozice).className = 'zvyrazneny';
var odsazeni = 0;
for (var i = 0; i < this.pozice; i++)
odsazeni += this.poleVysek[i];
if (odsazeni < this.odsazeni) {
this.odsazeni = odsazeni;
this.obsahNaseptavace.scrollTop = this.odsazeni;
}
if (odsazeni >= (this.odsazeni + this.vyskaDiv - this.poleVysek[i])) {
this.odsazeni = odsazeni - this.vyskaDiv + this.poleVysek[i];
this.obsahNaseptavace.scrollTop = this.odsazeni;
}
}
this.kodKlavesy = function (e) {
if (e) {
return e.charCode ? e.charCode : e.keyCode;
}
else {
return window.event.charCode ? window.event.charCode : window.event.keyCode;
}
}
this.posunPoziceDolu = function () {
//posun dolu
if ((this.pozice + 1) >= this.pocetVysledku)
this.nastavPozici (0);
else
this.nastavPozici (this.pozice + 1);
}
this.posunPoziceNahoru = function () {
//posun nahoru
if ((this.pozice - 1) < 0)
this.nastavPozici (this.pocetVysledku - 1);
else
this.nastavPozici (this.pozice - 1);
}
this.klavesaDole = function (e) {
var unikod = this.kodKlavesy (e);
if (unikod == 13) {
if (this.pozice != -1)
if (e.preventDefault)
e.preventDefault();
else
e.returnValue = false;
//nechat false
return false;
}
if (this.pocetVysledku > 0) {
if (unikod == 40)
this.posunPoziceDolu ();
if (unikod == 38)
this.posunPoziceNahoru ();
}
}
this.klavesaNahore = function (e) {
var unikod = this.kodKlavesy (e);
if (unikod == 13) {
if (this.pozice != -1)
this.klik (this.pozice);
// if (e.preventDefault)
// e.preventDefault ();
// else
// e.returnValue = false;
//nechat false
return false;
}
//vyrazeni sipek
if (unikod == 40 || unikod == 38 || unikod == 37 || unikod == 39)
return;
if (this.inputNaseptavace.value.length > this.minimalniPocetZnaku) {
var argumenty = new Array ();
for (var i = 1; i < arguments.length; i++)
argumenty[i - 1] = arguments[i];
var modul = argumenty.shift ();
var id = argumenty.shift ();
argumenty.unshift (this.inputNaseptavace.value);
this.odesli (modul, id, argumenty);
}
else {
this.pozice = -1;
this.pocetVysledku = 0;
this.obsahNaseptavace.innerHTML = '';
this.obsahNaseptavace.style.display = 'none';
}
}
this.skryj = function () {
this.pozice = -1;
this.pocetVysledku = 0;
this.obsahNaseptavace.innerHTML = '';
this.obsahNaseptavace.style.display = 'none';
}
this.skryjZpozdene = function () {
if (!this.obsahNaseptavaceAktivni && !this.inputNaseptavaceAktivni) {
var objekt = this;
setTimeout (function () {objekt.skryj ()}, 500);
}
}
this.nastavObsahNaseptavace = function (stav) {
this.obsahNaseptavaceAktivni = stav;
}
this.nastavInputNaseptavace = function (stav) {
this.inputNaseptavaceAktivni = stav;
}
this.minimalniPocetZnaku = 3;
this.pocetVysledku = 0;
this.pozice = -1;
this.poleAkci = new Array ();
this.vyskaDiv = 250;
this.poleVysek = new Array ();
this.odsazeni = 0;
this.inputNaseptavace = document.getElementById (inputNaseptavace);
this.inputNaseptavace.setAttribute ('autocomplete', 'off');
this.inputNaseptavaceAktivni = false;
var objekt = this;
var funkceOnFocus = (objekt.inputNaseptavace.onfocus) ? objekt.inputNaseptavace.onfocus : function () {};
this.inputNaseptavace.onfocus = function () {
funkceOnFocus ();
objekt.nastavInputNaseptavace (true);
};
var funkceOnBlur = (objekt.inputNaseptavace.onblur) ? objekt.inputNaseptavace.onblur : function () {};
this.inputNaseptavace.onblur = function () {
funkceOnBlur ();
objekt.nastavInputNaseptavace (false);
objekt.skryjZpozdene ();
};
this.obsahNaseptavace = document.getElementById (obsahNaseptavace);
this.obsahNaseptavaceAktivni = false;
this.obsahNaseptavace.onmousedown = function () {
objekt.nastavObsahNaseptavace (true);
};
this.obsahNaseptavace.onmouseout = function () {
if (objekt.obsahNaseptavaceAktivni) {
objekt.nastavObsahNaseptavace (false);
objekt.inputNaseptavace.focus ();
}
};
};