    var el_kam;
    var blur_schovava = 1;
    var ul;
    var timer;
    var xhr;
    var vypln_custom;
    var onEnter_custom;
    var active_li;

$(document).ready(function()
{
     var zobraz = function(stav,blur_disable)
    {
        // Ve chvili kdy kurzor vstoupi nad UL naseptavace, ztraci INPUT moznost schovavat UL
        // tuto moznost ziska az kuroz opusti UL.
      if(blur_disable == 1) {
             if(stav==1) blur_schovava = 0;
             else blur_schovava = 1;
        }

        if(!blur_disable && blur_schovava == 0) {
            return;
        }

        if(stav == 0) {

            var f = function(){ul.css("display","none")}
            timer = setTimeout(f, 500);
        }
        else {
            clearTimeout(timer);
            ul.css("display","block")

        }
    }

    var psani = function()
    {
    	nazev = $("#stype1");
        kod_vzp = $("#stype2");
        kod_vyrobku = $("#stype3");
        fulltext = $("#stype4");

        if(fulltext.is(":checked")) {
            return false;
        }

        var typ;

        if(nazev.is(":checked")) typ = "nazev";
        else if(kod_vzp.is(":checked")) typ = "kod_vzp";
        else if(kod_vyrobku.is(":checked")) typ = "kod_vyrobku";
        else return false;


        url = baseUrl+"/ajax/naseptavac/null?q="+el_kam.val()+"&typ="+typ;

        if ($("#select_filtr_vyrobce").length) {
            url = url + "&did=" + $("#select_filtr_vyrobce").val();
        }

        if(xhr) {
            xhr.abort();
        }

        xhr = $.get(url,{},function(data)
        {
        //  onclick='vypln(this)' onmouseover='liMouseover(this)'
            if(!ul)
            {
               div =  $(el_kam.parent().find("div").get(1));
               div.html("<ul class='naseptavac' id='naseptavac'></ul>");
               ul = div.find("ul#naseptavac");
               ul.mouseenter(function(){zobraz(1,1)})
                 .mouseleave(function(){zobraz(0,1)})
                 .find("li").live("mouseover",function()
                            {
                                 active_li_change($(this));
                                 return false;
                            })
                            .live("click",function()
                            {
                            	if(vypln_custom) vypln_custom($(this));
                                else vypln($(this));
                            });
            }

            ul.css("display","block");
            ul.html(data);
            //active_li = null;
        });



        return false;
    }

    var active_li_change = function(new_li)
    {
        if(active_li) {
            active_li.removeClass("hover");
        }
        active_li = new_li;
        active_li.addClass("hover");

    }


    var vypln = function(el)
	    {
    		if(el)
    		{
	    		span = el.find("span");
		        if(span.length) hodnota = span.text();
		        else hodnota = el.text();

		        el_kam.val(hodnota);
    		}
	        ul.css("display","none");
	        el_kam.parents("form").submit();
	}

    var ul_test = function()
    {
    	if(!ul) return false;
    	ul.css("display","block")

    	return true;
    }

    $(".naseptavacProduktu")
        .blur(function(){zobraz(0)})
        .keydown(function(event){
        	   // down
               if(event.which == 40) {
                    if(!ul_test()) return false;

                    active_li = ul.find(".hover");
                    if(active_li.length) {
                        next = active_li.next(":not(.nezahrnovat)");
                        if(next.length) {
                            active_li_change(next);
                            return false;
                        }

                    }

                    active_li_change($(ul.find("li:not(.nezahrnovat)").get(0)));

                    return false;

               }
               else if(event.which == 38){
                    if(!ul_test()) return false;

                    active_li = ul.find(".hover");
                    if(active_li.length) {
                        prev =  active_li.prev(":not(.nezahrnovat)");
                        if(prev.length) {
                            active_li_change(prev);
                            return false;
                        }
                    }

                    active_li_change($(ul.find("li:not(.nezahrnovat)").get(ul.find("li:not(.nezahrnovat)").length-1)));

                    return false;

               }
               else if(event.which == 13) {
            	    if(onEnter_custom) return onEnter_custom();

                    //if(!active_li) return false;

                    if(vypln_custom) vypln_custom(active_li);
                    else vypln(active_li);

                    event.stopPropagation();
               }
            })
        .keyup(function(event)
            {
        	    if(event.which == 13 || event.which == 40 || event.which == 38) {
                    return false; // ENTER, UP, DOWN
                }

                if($(this).val().length < 3) {
                    return false;
                }

                if(!el_kam) el_kam = $(this);

                return psani();
            });
});
