var kb={
	cnt:10,
	addr:null,
	Data:null,
	empty:new Array(),
	kbDiv:null,
	jsDiv:null,
	wordDiv:new Array(),
	relDiv:new Array(),
	act:null,
	mouseT:null,
	delT:null,
	selT:null,
	inpObj:null,
	repNum:null,
	on:true,
	mkKbDiv:function(id){
	this.kbDiv=document.createElement('div');
	this.kbDiv.id=('kibitz');
	this.kbDiv.style.display='none';
	var subDiv=document.createElement('div');
	this.jsDiv=document.createElement('div');
	this.jsDiv.id='kibitz-data';
	document.getElementById(id).form.appendChild(this.jsDiv);
	
	for(i=0; i<this.cnt; i++){
		var ln=document.createElement('p');
		ln.style.display='none';
		var sp1=document.createElement('span');
		var sp2=document.createElement('span');
		var t1=document.createTextNode('.');
		var t2=document.createTextNode('.');
		this.wordDiv[i]=sp1;
		this.relDiv[i]=t2;
		sp1.appendChild(t1);
		sp2.className='c';
		sp2.appendChild(t2);
		ln.appendChild(sp2);
		ln.appendChild(sp1);
		subDiv.appendChild(ln);
		ln.onclick=kb._inmclick
		ln.onmouseout=kb._inmout;
		ln.onmouseover=kb._inmover;
	}
	var offA=document.createElement('a');
	offA.id='kib-off';
	offA.href='http://www.centrum.cz/setup/';
	var offT=document.createTextNode('Vypnout Rádce');
	offA.appendChild(offT);

	var infA=document.createElement('a');
	infA.id='kib-inf';
	infA.href='http://search.centrum.cz/inteligentni-vyhledavani-centrum-cz/#novinky';
	var infT=document.createTextNode('Co je Rádce?');
	infA.appendChild(infT);

	var navP=document.createElement('p');
	navP.id='kib-nav';
	navP.appendChild(offA);
	navP.appendChild(infA);

	this.kbDiv.appendChild(subDiv);
	this.kbDiv.appendChild(navP);
	document.getElementById(id).form.appendChild(this.kbDiv)
	},
	init:function(id){
	kb.ie=kb.ie?kb.ie:'utf-8';
	kb.oe=kb.oe?kb.oe:'utf-8';
		if(!kb.kbDiv){kb.mkKbDiv(id)}
		else{kb._kbInstall(id)}
		kb.inpObj=document.getElementById(id);
		kb.inpObj.onkeyup=kb._keyup;
		if(document.all)kb.inpObj.onkeydown=kb._keyCatch;
		else kb.inpObj.onkeypress=kb._keyCatch;
		kb.inpObj.onclick=kb._unselItem;
		kb.inpObj.onblur=function(){kb.selT=setTimeout("kb.clr()",650)}
	},
	grammar:function(i,vars) {
	if(i>4||i==0) return vars[2];
	if(i>1) return vars[1];
	return vars[0];
	},
	nformat:function(num){
	var sep = ' ';
	var snum = '' + num;
	var fnum = '';
	var beg = snum.length % 3;

	fnum = snum.substr(0, beg);
	for (k=beg; k < snum.length; k=k+3) {
		fnum += sep + snum.substr(k,3);
	}

	return(fnum);
	},
	fill:function(el,wrd){
	// cleanup
	var fch=el.firstChild;
	while (fch != null) { el.removeChild(fch); fch=el.firstChild }

	// prepare array
	if (kb.inpObj.value.length>0) {
		var inArr=kb.inpObj.value.replace(/[ ]+/,' ').replace(/^[ ]+|[ ]+$/,'').split(' ');
		var tmp=' '+wrd;
		for (var k=0;k<inArr.length;k++){
			tmp=tmp.replace(' '+inArr[k],' &&'+inArr[k]+'&&');
		}
		tmp.replace(/^[ ]/,'');
		var arr=tmp.split('&&');

		for (var j=0;j<arr.length;j++) {
			if (arr!=null && arr[j]!='') {
				match = 0;
				for(var k=0;k<inArr.length;k++) {
					if (arr[j]==inArr[k]) { match=1; break; }
				}
				if (match) {
					var b=document.createElement('b');
					var t=document.createTextNode(arr[j]);
					b.appendChild(t);
					el.appendChild(b);
				} else {
					var t=document.createTextNode(arr[j]);
					el.appendChild(t);
				}
			}
		}
	} else {
	// no match
		var t=document.createTextNode(wrd);
		el.appendChild(t);
	}
	},
	disp:function(arr){
		clearTimeout(kb.selT);
		kb.selT=null;
		kb.Data=arr;
		kb.repNum=arr.length>=kb.cnt?kb.cnt:arr.length;
		var view=false;
		for(i=0; i<kb.cnt; i++){
			if(kb.Data[i]){
				var wrd=kb.Data[i][0];
				var rel=kb.Data[i][1];
				kb.fill(kb.wordDiv[i],wrd);
				kb.relDiv[i].nodeValue=kb.nformat(rel)+' strán'+kb.grammar(rel,Array('ka','ky','ek'))+' vyhovuje';
				kb.wordDiv[i].parentNode.style.display='block';
				view=true
			} else{
				kb.wordDiv[i].nodeValue='.';
				kb.relDiv[i].nodeValue='.';
				kb.wordDiv[i].parentNode.style.display='none'
			}
		}
		if(view){
			kb._unselItem();
			kb.kbDiv.style.display='block'
		} else {
			kb.kbDiv.style.display='none'
		}
	},
	_escape:function(q) {
	var tmp = encodeURI(q);
	tmp = tmp.replace('&','%26'); 
	tmp = tmp.replace('=','%3D'); 
	tmp = tmp.replace('/','%2F'); 
	tmp = tmp.replace('+','%2B'); 
	return tmp;
	},
	_submit:function(){
		kb.inpObj.form.submit();
		return false
	},
	_kbInstall:function(ids){
		oldParent=kb.kbDiv.parentNode;
		newParent=document.getElementById(ids).form;
		kb.kbDiv.style.display='none';
		newParent.appendChild(kb.kbDiv)
	},
	_get:function(){
		if(kb.inpObj.value&&kb.on){
			kb.jsDiv.innerHTML='';
			kb.Data=null;
			var source=kb.addr+'?q='+kb._escape(kb.inpObj.value)+'&ie='+kb.ie+'&oe='+kb.oe;
			var ldr=document.createElement('script');
			ldr.type='text/javascript';
			ldr.src=source;
			kb.jsDiv.appendChild(ldr)
		} else{kb.disp(kb.empty)}
	},
	_inmover:function(){
		clearTimeout(kb.mouseT);
		kb._mover(this)
	},
	_inmout:function(){kb.mouseT=setTimeout("kb._mout(this)",150)},
	_inmclick:function(){return kb._mclick(this)},
	_mout:function(){kb._unselItem()},
	_mover:function(obj){
		if(kb.act!=null){kb.act.className=''}
		kb.act=obj;
		kb.act.className='hover'
	},
	_mclick:function(obj){
		var tmp = document.getElementById('s_kibitz');
		kb.inpObj.value=obj.childNodes[1].innerHTML.replace(/<\/?[b]>/gi,'').replace(/^[ ]+|[ ]+$/,'');
		setTimeout("kb.inpObj.focus()",15);
		if(tmp!=null) tmp.value = 1;
		kb._submit()
	},
	_unselItem:function(){
		if(kb.act){kb.act.className='';
		kb.act=null}
	},
	_selItem:function(kId){
		if(kb.Data&&(kb.Data.length>0)){
			if(kb.act){if(kId==40){
				kb.act.className='';
				newAct=kb.act==kb.kbDiv.getElementsByTagName('p')[kb.repNum-1]?kb.act:kb.act.nextSibling;
				newAct.className='hover';
				kb.act=newAct
				} else{
					kb.act.className='';
					newAct=kb.kbDiv.firstChild.firstChild==kb.act?kb.act:kb.act.previousSibling;
					newAct.className='hover';
					kb.act=newAct
				}
			} else { 
				kb.kbDiv.firstChild.firstChild.className='hover';
				kb.act=kb.kbDiv.firstChild.firstChild
			}
		}
	},
	_select:function(key){
		var tmp = document.getElementById('s_kibitz');
		if(kb.act){
			kb.inpObj.value=kb.act.childNodes[1].innerHTML.replace(/<\/?[b]>/gi,'').replace(/^[ ]+|[ ]+$/,'');
			if(tmp!=null) tmp.value = 1;
			if(key!=13){kb._get()} return true
		} else return true
	},
	_getKey:function(e){
		if(!e)e=window.event;
		return e.keyCode
	},
	_keyCatch:function(e){
		kId=kb._getKey(e);
		if((kId==13)){return kb._select(kId)}
		if((kId==38)||(kId==40)){
			kb._selItem(kId);
			return false
		}
		if(kId==27){kb.clr()}
	},
	_keyup:function(e){
		kId=kb._getKey(e);
		if((kId==8)||(kId==46)){
			clearTimeout(kb.delT);
			kb.delT=null;
			kb._unselItem();
			kb.delT=setTimeout('kb._get()',90)
		} else if( ((kId<33)||(kId>39))&&(kId!=13)&&(kId!=17)&&
					(kId!=18)&&(kId!=27)&&(kId!=40)&&(kId!=44)&&(kId!=45)) {
			clearTimeout(kb.delT);
			kb.delT=null;
			kb.delT=setTimeout('kb._get()',230)
		}
	},
	clr:function(){
		kb._unselItem();
		kb.kbDiv.style.display='none';
		kb.disp(kb.empty)
	}
};