var objHttpRequest=null;
var objInputBox=null;
var objWhispererBox=null;
var lastPhrase='';
var hideTimer=null;
var maxPhrases=0;
var focusedPhrase=0;
var theInfo = new Object();

function whispererInit(inputElemID)
{objInputBox=document.getElementById(inputElemID);if(objInputBox!=null)
{var bodyElements=document.getElementsByTagName('body')
if(bodyElements.length>0)
{objInputBox.onblur=onSearchBoxBlur;objInputBox.onkeydown=onSearchBoxKeyDown;objInputBox.onkeyup=onSearchBoxKeyUp;objWhispererBox=document.createElement('div');objWhispererBox.setAttribute('class','whispererBox');objWhispererBox.setAttribute('className','whispererBox');bodyElements[0].insertBefore(objWhispererBox,bodyElements[0].firstChild);}}
whispererHide();}

function whispererShow()
{if(hideTimer!=null)
clearTimeout(hideTimer);hideTimer=null;if((objWhispererBox!=null)&&(objInputBox!=null))
{var top=objectTop(objInputBox);var left=objectLeft(objInputBox);var width=parseInt(objInputBox.offsetWidth,10);var heigt=parseInt(objInputBox.offsetHeight,10);objWhispererBox.style.top=top+heigt+'px';objWhispererBox.style.left=left+'px';objWhispererBox.style.width=width+300+'px';objWhispererBox.style.visibility='visible';}}

function whispererHide()
{if(hideTimer!=null)
clearTimeout(hideTimer);hideTimer=null;if(objWhispererBox!=null)
objWhispererBox.style.visibility='hidden';}

function isWhispererVisible()
{return((objWhispererBox!=null)&&(objWhispererBox.style.visibility=='visible'))?true:false;}

function focusPhrase()
{if(objWhispererBox!=null)
{for(var i=0;i<objWhispererBox.childNodes.length;i++)
{var style=(focusedPhrase==i)?'focused':'';objWhispererBox.childNodes[i].setAttribute('class',style);objWhispererBox.childNodes[i].setAttribute('className',style);}}}

function onSearchBoxBlur()
{if(hideTimer==null)
hideTimer=setTimeout("whispererHide()",1000);}

function onSearchBoxKeyDown(e)
{if(isWhispererVisible()==true)
{if(!e)var e=window.event;if(e.keyCode==13)
{if((focusedPhrase>-1)&&(focusedPhrase<objWhispererBox.childNodes.length))
{unmark2();whispererHide();document.vyhledavani.submit();return false;}}
else if(e.keyCode==27)
{whispererHide();return false;}}}

function onSearchBoxKeyUp(e)
{if(objWhispererBox!=null)
{var doLeave=false;if(!e)e=window.event;if(e.keyCode==38)
{if(isWhispererVisible()==true)
{if(--focusedPhrase<0)
focusedPhrase=0;}
else
{focusedPhrase=0;}
focusPhrase();doLeave=true;}
else if(e.keyCode==40)
{if(isWhispererVisible()==true)
{if(++focusedPhrase>=maxPhrases)
focusedPhrase=(maxPhrases-1);}
else
{focusedPhrase=0;}
focusPhrase();doLeave=true;}
else if(e.keyCode==13)
{return false;}
if(doLeave==true)
{if((isWhispererVisible()==false)&&(objInputBox.value==lastPhrase)&&(objWhispererBox.childNodes.length>0))
{whispererShow();}
return false;}}
var navigate=false;if(objInputBox!=null)
{var trimmedValue=trim(objInputBox.value);if(trimmedValue.length>2)
{if(objInputBox.value!=lastPhrase)
{lastPhrase=objInputBox.value;navigate=true;}}}
if(navigate==false)
{if(isWhispererVisible()==true)
{return;}}
else
{if(hideTimer!=null)
clearTimeout(hideTimer);hideTimer=setTimeout("whispererHide()",1000);if(objHttpRequest==null)
{try{objHttpRequest=new XMLHttpRequest();}
catch(e){objHttpRequest=null;}}
if(objHttpRequest==null)
{try{objHttpRequest=new ActiveXObject("Microsoft.XMLHTTP");}
catch(e){objHttpRequest=null;}}
if(objHttpRequest!=null)
{if(objHttpRequest.readyState!=0)
objHttpRequest.abort();objHttpRequest.open("GET","/septej.php?w="+encodeURIComponent(lastPhrase),true);objHttpRequest.onreadystatechange=function()
{if((objHttpRequest.readyState==4)&&(objHttpRequest.responseText.length>0))
whispererRequestReceive(objHttpRequest.responseText);}
objHttpRequest.send(null);return;}}
whispererHide();}

function onPhraseBoxClick(e)
{if(objInputBox!=null)
{if(!e)var e=window.event;var target=(window.event)?e.srcElement:e.target;if(target!=null)
{unmark2();whispererHide();document.vyhledavani.submit();}}
return true;}

function onPhraseBoxMouseMove(e)
{if(isWhispererVisible()==true)
{if(!e)var e=window.event;var target=(window.event)?e.srcElement:e.target;if((target!=null)&&(target.offsetParent!=null))
{for(var i=0;i<target.offsetParent.childNodes.length;i++)
{if(target.offsetParent.childNodes[i]==target)
{focusedPhrase=i;focusPhrase();break;}}}}}

function whispererRequestReceive(responseCode)
{
if(objWhispererBox!=null)
{responseCode=trim(responseCode);
if(responseCode.length>0)
{
if(hideTimer!=null)
clearTimeout(hideTimer);
hideTimer=null;
objWhispererBox.innerHTML='';
var arrLines=responseCode.split('\r\n');
for(var i=0;i<arrLines.length;i++)
{
var arrLines1=arrLines[i].split('|');
theInfo[i] = arrLines[i];
var phraseBox=document.createElement('div');
phraseBox.innerHTML=markw(trim(arrLines1[2]));
if(arrLines1[0] == '1') {
phraseBox.innerHTML+=' <span>(vrobce)</span>';
}
else if(arrLines1[0] == '2') {
phraseBox.innerHTML+=' <span>(kategorie)</span>';
}
phraseBox.onclick=onPhraseBoxClick;
phraseBox.onmousemove=onPhraseBoxMouseMove;
objWhispererBox.appendChild(phraseBox);
}
maxPhrases=arrLines.length;
focusedPhrase=-1;
if(objWhispererBox.childNodes.length>0)
{
focusPhrase();
whispererShow();
return;
}
}
}
whispererHide();
}

function trim(str,chars){return ltrim(rtrim(str,chars),chars);}
function ltrim(str,chars){chars=chars||"\\s";return str.replace(new RegExp("^["+chars+"]+","g"),"");}
function rtrim(str,chars){chars=chars||"\\s";return str.replace(new RegExp("["+chars+"]+$","g"),"");}
function objectLeft(anObject){var left=0;while(anObject!=null){left+=parseInt(anObject.offsetLeft,10);anObject=anObject.offsetParent;}return left;}
function objectTop(anObject){var top=0;while(anObject!=null){top+=parseInt(anObject.offsetTop,10);anObject=anObject.offsetParent;}return top;}

function bezdiak(txt) 
{ 
sdiak = "ͼݎ"; 
bdiak = "aacdeeeilnooorrstuuuyzAACDEEEILNOOORRSTUUUYZ"; 
tx = ""; 
for(p = 0; p < txt.length; p++) 
{ 
if (sdiak.indexOf(txt.charAt(p)) != -1) 
{ 
tx += bdiak.charAt(sdiak.indexOf(txt.charAt(p))); 
} 
else tx += txt.charAt(p); 
} 
return tx;
} 

function markw(str)
{
ib_w = trim(objInputBox.value);
pos_w = bezdiak(str).toLowerCase().search(bezdiak(ib_w).toLowerCase());
if (pos_w>-1)
{
w = str.substr(pos_w,ib_w.length);
return str.replace (new RegExp(w,"gi") ,"<b>$&</b>");
}
return str;
}

function unmark(str)
{
var inq=theInfo[focusedPhrase].split('|');
document.vyhledavani.stc_slovo.value = trim(objInputBox.value);
document.vyhledavani.stc_typ.value = inq[0];
document.vyhledavani.stc_kod.value = inq[1];
return inq[2];
}

function unmark2()
{
var inq=theInfo[focusedPhrase].split('|');
document.vyhledavani.stc_typ.value = inq[0];
document.vyhledavani.stc_kod.value = inq[1];
}


