window.addEvent('domready', function(){
	 if($('searchid')!=null)
    	$('searchid').focus();
});

var Utf8 = {

// public method for url encoding
encode : function (string) {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++) {
        var c = string.charCodeAt(n);
        if (c < 128) {
            utftext += String.fromCharCode(c);
        }
        else if((c > 127) && (c < 2048)) {
            utftext += String.fromCharCode((c >> 6) | 192);
            utftext += String.fromCharCode((c & 63) | 128);
        }
        else {
            utftext += String.fromCharCode((c >> 12) | 224);
            utftext += String.fromCharCode(((c >> 6) & 63) | 128);
            utftext += String.fromCharCode((c & 63) | 128);
        }
    }
    return utftext;
},

// public method for url decoding
decode : function (utftext) {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;
    while ( i < utftext.length ) {
        c = utftext.charCodeAt(i);
        if (c < 128) {
            string += String.fromCharCode(c);
            i++;
        }
        else if((c > 191) && (c < 224)) {
            c2 = utftext.charCodeAt(i+1);
            string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
            i += 2;
        }
        else {
            c2 = utftext.charCodeAt(i+1);
            c3 = utftext.charCodeAt(i+2);
            string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
            i += 3;
        }
    }
    return string;
}
}

var input = 'searchid';     //id pola wyszukiwarki
var str = '';               //ciag do wyszukania
var oldstr = '';            //stary ciag do wyszukania
var ktory = 0;              //zaznaczony element
var poprzedni = 0;          //poprzedni zaznaczony element
var counter = 0;            //ilosc zwroconych wynikow
var timeout = 200;          //timeout do wywolania AJAXa
var press = 0;              //flaga oznaczajaca czy wsisnieto klawisz

function giveHint(){
    str = $(input).get('value');
    if(str.length>1){
        setTimeout('doHint()', timeout);
    }
    else{
        press=0;
        hide();
    }
}

function doHint(){
    if(press=1){
        if(!str){
            hide();
        }
        if(str != oldstr){
            if(str){
                getAJAXhint();
            }
            else{
                hide();
            }
            oldstr=str;
        }
    }
    else hide();
}

function keyDown(event) {
    press=0;
    var event = new Event(event);

    if(poprzedni>0 && poprzedni<=counter) $('hint'+poprzedni).className='';
    poprzedni = ktory;
    if(event.code==38 || event.code==40 || event.code==13) {
        //up
        if(event.code==38){
            ktory--;
            if(ktory<1) ktory=counter;
        }
        //down
        else if(event.code==40){
            ktory++;
            if(ktory>counter) ktory=1;
        }
        //enter
        else if (event.code==13){
            $(input).value = trim($('hint'+ktory).get('html'));
//            !@! poprawka bledu js przy wcisnieciu entera (w IE) (begin)
            if($('hint'+ktory) == null){
                hide();
                press=0;
                event.cancelBubble = true;
                event.returnValue = true;
                return true;
            }
//            !@! poprawka bledu js przy wcisnieciu entera (w IE) (end)
            press=0;
        }
        if(poprzedni>0 && poprzedni<=counter) $('hint'+poprzedni).toggleClass('highlight');
        if(ktory>0 && ktory<=counter) $('hint'+ktory).toggleClass('highlight');
    }
    else {
        ktory = 0;
        return;
    }
}

function getAJAXhint(){
    var result = 'sugg';
    var url = '/search/givehint/qt/'+str;
//    url = '/search/givehint/qt/'+Utf8.encode(str);
	new Request.JSON({
        url: url,
        method: 'get',
        onSuccess: function(jsonObj) {
            if(jsonObj.counter>0 && press==1) {
                show();
                $('hint').set('html', jsonObj.hint);
            }
            else hide();
        }
    }).send();
}

function highlight(id){
    //zerowanie podswietlonego przez kalwiature
    if(ktory>0 && ktory <=counter) $('hint'+ktory).className='';

    ktory = id;
    $('hint'+id).className='highlight';
}

function unHighlight(id){
    poprzedni = id;
    $('hint'+id).className='';
}

function selectEntry (id){
    $(input).value = trim($('hint'+id).get('html'));
    press=0;
    hide();
}

function show() {
    //pokaz warstwy podpowiedzi
    $('hintspace').setStyle('display', 'block');
    $('hint').setStyle('display', 'block');
}

function hide() {
    //wylaczenie podswietlonego elementu
    if(poprzedni>0 && poprzedni <=counter) {
        if($('hint'+poprzedni).hasClass('highlight')) $('hint'+poprzedni).toggleClass('highlight');
    }
    if(ktory>0 && ktory <=counter) {
        if($('hint'+ktory).hasClass('highlight')) $('hint'+ktory).toggleClass('highlight');
    }
    //zerowanie aktualnie wybranego elementu
    ktory=0;
    //ukrycie warstw podpowiedzi
    $('hintspace').setStyle('display', 'none');
    $('hint').setStyle('display', 'none');
}

function trim(str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}
