var intSelectIndex = 0;
var intSelectIndexMax = 0;
var intSelectIndexLast = 0;
var bAction = 0;
var intKeyPress = 0;
var Timer;

function goWhisperer(e){ 
	var EXPS = objGet("EXPS"); 
	if(EXPS.value.length > 2){
		initWhisperer();
		if (EXPS.value != self.last){ 
			self.last = EXPS.value; 
			if(xml){ 
	      		if (xml.readyState != 0) xml.abort(); 
	      		xml.open ("GET", "/FilesForExecute/Whisperer.asp?phrase=" + self.encFunc(NonDiacritic(EXPS.value)), true); 
	      		xml.onreadystatechange = function(){ 
					if (xml.readyState == 4){ 
	          			GetResult(xml.responseText); 
						if(EXPS.value.length > 0 && xml.responseText.length > 0){
							objSelect.style.left = ((document.body.offsetWidth/2) + 182 + intDistCol).toString() + "px";
							objSelect.style.visibility = 'visible';
						}else{
							objSelect.style.visibility = 'hidden';
						} 
	        		} 
				} 
	      		xml.send(null); 
			} 
		}
		intKeyPress = 0;
		clearTimeout(Timer);
		KeyMove(e); 
	}else{
		self.last = "";
		objGet("Whisperer").style.visibility = 'hidden';
	}
} 
function initWhisperer(){ 
	clearTimeout(Timer);
	self.objSelect = objGet("Whisperer");
	self.xml = objXml("Msxml2.XMLHTTP");
	if (!xml) xml = objXml("Microsoft.XMLHTTP"); 
	if (!xml){ 
    	if (typeof XMLHttpRequest!="undefined") 
      		xml = new XMLHttpRequest(); 
  	}; 
  	self.encFunc = encodeURIComponent ? encodeURIComponent : escape; 
  
	objSelect.onclick = ClickHandler;
	objSelect.onmouseover = SelectClear;
	objGet("EXPS").onblur = ClearWhisperer;
	document.body.onkeypress = PressHandler;
	document.body.onkeydown = MoveHandler;
 
} 
function GetResult(strItem){ 
	var arrData = strItem.split(',');
	var arrSub;
	objSelect.innerHTML = ''; 
	SetIndexClear();
	for (var i=0; i < arrData.length-1; i++){ 
		arrSub = arrData[i].split('|')
		if(arrSub[1] == '1'){
			objSelect.innerHTML += '<div class=\'Whis_p\' id=\'option' + (i+1) + '\' >' + arrSub[0] + ' <span>(značka)</span></div>'
			i += 1;
		}else if(arrSub[1] == '2'){
			objSelect.innerHTML += '<div class=\'Whis_p\' id=\'option' + (i+1) + '\' >' + arrSub[0] + ' <span>(kategorie)</span></div>'
			i += 1;
		}
		objSelect.innerHTML += '<div class=\'Whis_p\' id=\'option' + (i+1) + '\'>' + arrSub[0] + '</div>'
	} 
	intSelectIndexMax = i;
} 
function MoveHandler(e){
	if(intKeyPress > 0){return}
	intKeyPress = 0;
	//objGet('test').innerHTML = objGet('test').innerHTML + intSelectIndexMax + "....." + intSelectIndex + "<br />" ;
	if (KeyCodeGet(e) == 38 && objSelect.style.visibility == 'visible') {
		intKeyPress = 38;
		Timer = window.setInterval("MoveHandlerCycle(intKeyPress)",200);
	}
	if (KeyCodeGet(e) == 40 && objSelect.style.visibility == 'visible') {
		intKeyPress = 40;
		Timer = window.setInterval("MoveHandlerCycle(intKeyPress)",200);
	}
}
function MoveHandlerCycle(intKey){
	if((parseFloat(intSelectIndex) == parseFloat(intSelectIndexMax) && intKeyPress == 40) || (parseFloat(intSelectIndex) == 1 && intKeyPress == 38)){
		intKeyPress = 0;
		clearTimeout(Timer);
		return;
	}
	if(intKeyPress > 0){
		KeyMove(intKey);
	}else{
		clearTimeout(Timer);
	}
}
function PressHandler(e){
	if (KeyCodeGet(e) == 13 && objSelect.style.visibility == 'visible') {
		var id = 'option' + intSelectIndex;
		text = objGet(id).innerHTML;
		Final(text);
	}
}
function ClickHandler(e){
	if (!e) var e = window.event;
	var srcEl = e.srcElement? e.srcElement : e.target;
	if(srcEl.tagName.toLowerCase() == 'span')
		{
		if(IE)
			{
				srcEl = srcEl.offsetParent
			}
		else
			{
				srcEl = srcEl.parentNode;
			}
		}
	var intIndex = parseFloat(srcEl.id.replace('option',''))
	SetActive(srcEl.id);
	SetPassive();
	intSelectIndexLast = intIndex;
	Final(srcEl.innerHTML);
}
function Final(sText){
	bAction = 1; 
	if(IE){event.returnValue = false} 
	sText = sText.toLowerCase().replace('<span>','')
	sText = sText.replace('</span>','')
	objGet("EXPS").value = sText;
	objSelect.style.visibility = 'hidden';
	objGet("SearchButt").focus()
	objGet("SearchButt").click()
}
function KeyMove(e){
	if(typeof(e) == 'object'){
		var uniKey = KeyCodeGet(e);
	}else{
		var uniKey = e;
	}
	var intIndexPlus = intSelectIndex + 1;
	var intIndexMinus = intSelectIndex - 1;
	var intLength = intSelectIndexMax;
	
	if(intIndexMinus < 0){intIndexMinus = 0}
	if(intIndexPlus > intLength){intIndexPlus = intLength}
	
	if (uniKey == 40) {
		if(parseFloat(intSelectIndexMax) == parseFloat(intSelectIndex)){return}
		if (objSelect.style.visibility == 'visible') {
			SetActive('option' + intIndexPlus);
			SetPassive();
			intSelectIndex = intSelectIndex + 1;
			intSelectIndexLast = intSelectIndex;
			return;
		}
	}
	if (uniKey == 38) {
		if(parseFloat(intSelectIndex) == 1){return}
		if (objSelect.style.visibility == 'visible') {
			SetActive('option' + intIndexMinus);
			SetPassive();
			intSelectIndex = intSelectIndex - 1;
			intSelectIndexLast = intSelectIndex;
			return;
		}
	}
}
function KeyCodeGet(e){
		if(e){
			return e.charCode ? e.charCode : e.keyCode;
		}else{
			return window.event.charCode ? window.event.charCode : window.event.keyCode;
		}
	}
function objXml(obj){ 
	try { 
  		return new ActiveXObject(obj); 
	}catch(e){
		return null; 
	} 
} 
function SetIndexClear(){
	intSelectIndex = 0;
	intSelectIndexMax = 0;
	intSelectIndexLast = 0;
	intKeyPress = 0;
}
function ClearWhisperer(){
	if(bAction == 0){
		setTimeout("ClearWhispererSub()",1000)
	}
}
function ClearWhispererSub(){
	objGet("Whisperer").style.visibility = 'hidden';
}
function SelectClear(){
	SetPassive();
}
function SetPassive(){
	if(intSelectIndexLast != 0){objGet('option' + intSelectIndexLast).className = 'Whis_p'};
}
function SetActive(sId){
	objGet(sId).className = 'Whis_a';
}




