/* Adam Haken, 2006 */
var kb={
    iinit:0,
    ua:navigator.userAgent.toLowerCase(),
    ac:0,
    qvals:new Array(),
    qpos:-1,
    maxmoznosti:8,
    qval:'',
    qvalclear:'',
    ie:'',
    oe:'',
    qwin:null,
    el:'',
    useradce:0,
    qfocus:0,
    lastradce:0,
    lastqval:'',
    lastkey:0,
    drzeni:0,
    zadanoq:0,
    submit:0,
    keytimeout:null,
    kibitz:null,
    init:function(id,ie,oe,max) {
	if (max!=null) kb.maxmoznosti=max;
	if (id==null) return;
	kb.el=id;
	kb.iinit=1;
	kb.qwin=document.getElementById(id);
	kb.kibitz=document.getElementById('kibitz');
	kb.kibitz.style.left='-10000pt';
	if (ie) kb.ie=ie; else kb.ie='utf-8';
	if (oe) kb.oe=oe; else kb.oe='utf-8';
	kb.qwin.setAttribute("autocomplete", "off");
	kb.qwin.onkeyup=kb.check_radce;
	kb.qwin.onfocus=kb.focus;
	kb.qwin.onblur=kb.blur;
	if (document.all) kb.qwin.onkeydown=kb.listen_key; else kb.qwin.onkeypress=kb.listen_key;
    },
    destroy:function() {
	kb.iinit = 0;
    },
    zvyrazni:function(vyhledani,query) {
	var tmp;
	query=query.replace(/^[ ]/,'');
	query=query.replace(/[ ]$/,'');
	var arr=vyhledani.split(' ');
	var arc=arr.length;
	var arr2=query.split(' ');
	var arc2=arr2.length;
	var tmp1;
	var tmp2;
	var tmp3;
	var tmp0=new String;
	var tmp=document.createElement('span');
	if (arc>0)
	    for (j=0;j<arc;j++) {
		tmp1=null;
		tmp2=null;
		if (arc2>0)
		    for (k=0;k<arc2;k++) {
			if (arr[j].substring(0,arr2[k].length)==arr2[k]) {
			    tmp1=document.createElement('b');
			    tmp3=document.createTextNode(arr2[k]);
			    tmp1.appendChild(tmp3);
			    tmp2=document.createTextNode(arr[j].substring(arr2[k].length));
			}
		    }
		if (tmp1==null) tmp2=document.createTextNode(arr[j]);
		if (tmp1) tmp.appendChild(tmp1);
		if (tmp2) tmp.appendChild(tmp2);
		if (j<arc-1) {
		    tmp2=document.createTextNode(' ');
		    tmp.appendChild(tmp2);
		}
	    }
	return tmp;
    },
    disp:function(arr) {
	var sklon;
	var fraze=new String;
	var tmp1;
	var tmp2;
	var tmp3;
	var tmp4;
	var tmp5;
	var tmp6;
	tmp1=kb.kibitz.firstChild;
	while (tmp1!=null) {
    	    kb.kibitz.removeChild(tmp1);
	    tmp1=kb.kibitz.firstChild;
	}
	kb.ac=arr.length;
	if (kb.ac>0) {
	    tmp1=document.createElement('div');
	    for(i=0;i<kb.ac;i++)
		if (arr[i] && i<kb.maxmoznosti) {
		    sklon='ka';
		    if (arr[i][1]>1) sklon='ky';
		    if (arr[i][1]>4||arr[i][1]==0) sklon='ek';
		    tmp2=document.createElement('p');
		    tmp2.id='kbitz-'+i;
		    tmp2.onmouseover=kb.over;
		    tmp2.onclick=kb.click;
		    tmp2.style.cursor='hand';
		    tmp3=document.createElement('span');
		    tmp3.className='c';
		    tmp5=document.createTextNode(arr[i][1]+' str'+String.fromCharCode( 225 )+'n'+sklon+' vyhovuje');
		    tmp3.appendChild(tmp5);
    		    tmp4=kb.zvyrazni(arr[i][0],kb.qvalclear.toLowerCase());
		    tmp2.appendChild(tmp3);
		    tmp2.appendChild(tmp4);
		    tmp1.appendChild(tmp2);
		    kb.qvals[i]=arr[i][0];
		}
		kb.kibitz.appendChild(tmp1);
		tmp1=document.createElement('p');
		tmp2=document.createElement('a');
		tmp3=document.createElement('a');
		tmp1.setAttribute('id','kib-nav');
		tmp1.className='kib-nav';
		tmp2.setAttribute('onclick', 'switchOffKibitz();');
		tmp2.setAttribute('title','Vypnúť Radcu');
		tmp2.setAttribute('id','kib-off');
		tmp2.className = 'kib-off';
		tmp4=document.createTextNode('vypnúť Radcu');
		tmp2.appendChild(tmp4);
		tmp1.appendChild(tmp2);
		kb.kibitz.appendChild(tmp1);
		kb.useradce=1;
		kb.kibitz.style.left='112px';
	}
	else {
    	    useradce=0;
	    kb.kibitz.style.left='-10000pt';
	}
    },	
    rewrite_hover:function() {
        for (var i=0;i<kb.ac;i++) {
	    var el = document.getElementById('kbitz-'+i);
	    if(el) {
		if (i==kb.qpos) {el.className='hover'; }
		    else {el.className='';}
	    }
	}
    },
    focus:function() {
	kb.qpos=-1;
	kb.qfocus=1;
	kb.rewrite_hover();
    },
    set_blur:function() {
	if (kb.qfocus==0) kb.close_radce();
    },
    blur:function() {
	window.setTimeout('kb.set_blur()',500);
	kb.qfocus=0;
    },
    get_pos:function(tmp) {
        var tmp2=tmp.lastIndexOf('-');
        if (tmp2>0) return tmp.substring(tmp.lastIndexOf('-')+1);
    	    else return false;
    },    
    over:function() {
	kb.qpos=kb.get_pos(this.id);
	kb.rewrite_hover();
	kb.zadanoq=0;
    },
    out:function(i) {
	kb.qpos=-1;
	document.getElementById('kbitz-'+i).className='';
    },
    click:function(){
        kb.qpos=kb.get_pos(this.id);
	kb.qwin.value=kb.qvals[kb.qpos];
	document.getElementById(kb.el).form.s_kibitz.value='1';
	kb.kibitz.style.left='-10000pt';
	window.setTimeout('kb.submit()',50);
    },
    q_escape:function(q) {
	var tmp=q;
	if(navigator.userAgent.indexOf('MSIE 5.0')==-1) tmp=encodeURI(q); else tmp=kb.encode(q);
	tmp=tmp.replace('&','%26');
	tmp=tmp.replace('=','%3D');
	tmp=tmp.replace('/','%2F');
	tmp=tmp.replace('+','%2B');
	return tmp;
    },
    check_radce:function() {
        if (kb.iinit==0) return;
	kb.lastkey=0;
	if (kb.qwin.value==kb.lastqval) return;
	kb.zadanoq=1;
	kb.lastqval=kb.qwin.value;
	kb.lastradce++;
	window.setTimeout('kb.load_radce('+kb.lastradce+')',500);
    },
    load_radce:function(lastradce) {
	if (kb.iinit==0) return;
	if (kb.lastradce!=kb.lastradce) return;
	var q_sv = document.getElementById('q_sv');
	var q=kb.qwin.value;
	if (q.length==0) {
    	    kb.close_radce();
	    return;
	}
	kb.qvalclear=q;
	if(q_sv!=null) q_sv.value = q;
	q=kb.q_escape(q);
	kb.qval=q;
	var head=document.getElementsByTagName('head').item(0);
	var old=document.getElementById('last_id');
	if (old)head.removeChild(old);
	script=document.createElement('script');
	script.src='http://radce.centrum.sk/?q='+q.toLowerCase()+'&ie='+kb.ie+'&oe='+kb.oe+'';
	script.type='text/javascript';
	script.defer=true;
	script.id='last_id'
	void(head.appendChild(script));
	kb.qpos=-1;
	kb.qwin.focus();
	window.setTimeout('kb.zadanoq=1',100);
    },
    close_radce:function() {
	var tmp1=kb.kibitz.firstChild;
	while (tmp1!=null) {
	    kb.kibitz.removeChild(tmp1);
	    tmp1=kb.kibitz.firstChild;
	}
	kb.kibitz.style.left='-10000pt';
    },
    minus:function() {
	if (kb.lastkey!=38) return;
	clearTimeout(kb.keytimeout);
	kb.qpos--;
	if (kb.qpos<-1) kb.qpos=-1;
	kb.rewrite_hover();
	kb.zadanoq=0;
	if (kb.drzeni==0) kb.keytimeout=window.setTimeout('kb.minus()',450);
	    else kb.keytimeout=window.setTimeout('kb.minus()',70);
	kb.drzeni++;
	return false;
    },
    plus:function() {
	if (kb.lastkey!=40) return;
	clearTimeout(kb.keytimeout);
	if (kb.qpos<(kb.maxmoznosti-1)) {
	    kb.qpos++;
	    if (kb.qpos>=kb.ac) kb.qpos=kb.ac-1;
	    kb.rewrite_hover();
	    kb.zadanoq=0;
	    if (kb.drzeni==0) kb.keytimeout=window.setTimeout('kb.plus()',450);
		else kb.keytimeout=window.setTimeout('kb.plus()',70);
	    kb.drzeni++;
	}
	return false;
    },
    listen_key:function(e) {
	if (kb.useradce==0) return;
    	if(!e) if (window.event) e=window.event;
	var key;
	if (typeof(e.which)=='number' ) key=e.which;
	else if (typeof(e.keyCode)=='number') key=e.keyCode;
	else if (typeof(e.charCode)=='number') key=e.charCode;
   	else return;
	key=e.keyCode;
	kb.lastkey=key;
	kb.drzeni=0;
	if (key==27) {kb.close_radce();}
	else if (key==13) {
	    kb.qwin.blur();
	    if (kb.qpos>=0 && kb.zadanoq==0) {
		kb.qwin.value=kb.qvals[kb.qpos];
		document.getElementById(kb.el).form.s_kibitz.value='1';
	    }
	    if (kb.ua.indexOf("opera") == -1) window.setTimeout('kb.submit()',50);
	    return false;
	}
        else if (key==38) {
	    kb.minus();
	    return false;
	}
	else if (key==40) {
	    kb.plus();
	    return false;
	}
    },
    submit:function() {
	kb.close_radce();
	document.getElementById(kb.el).form.submit();
    },
    encode:function(q) {
	var tmp = q;
	if (q!='') { tmp=tmp.replace('e','%C3%A9');tmp=tmp.replace('e','%C4%9B');tmp=tmp.replace('r','%C5%99');tmp=tmp.replace('t','%C5%A5');tmp=tmp.replace('u','%C5%AF');
		     tmp=tmp.replace('u','%C3%BA');tmp=tmp.replace('i','%C3%AD');tmp=tmp.replace('o','%C3%B3');tmp=tmp.replace('a','%C3%A1');tmp=tmp.replace('s','%C5%A1');
		     tmp=tmp.replace('d','%C4%8F');tmp=tmp.replace('z','%C5%BE');tmp=tmp.replace('c','%C4%8D');tmp=tmp.replace('n','%C5%88');tmp=tmp.replace('y','%C3%BD');
	}
	return tmp;
    }
}

function switchOffKibitz() {
    if (confirm("Naozaj chcete Radcu vypnúť?")) {
	var hp_cookie = hp_rc('personal');
	var today = new Date();
	var time_personal = today.getMilliseconds()+7776000000;
	var day = new Date(time_personal*1000);
        if (hp_cookie) { cookie_ele = hp_cookie.split('|'); } else {cookie_ele = false;}
	cookie_ele[6] = 'ki:0';
    	if (cookie_ele) document.cookie = "personal="+cookie_ele.join("|")+"; domain=centrum.sk; path=/; expires="+day;
	    else document.cookie = "personal=||||||ki:0; domain=centrum.sk; path=/; expires="+day;

        kb.destroy();
    }
}
