$().ready(function() {
    function findValueCallback(event, data, formatted) {
        $("<li>").html("selected: " + formatted).appendTo("#result");
    }
    
    function formatItem(row) {
        if (row != '&nbsp;')
            return row[0];
        else 
            return false;
    }
    
    function formatResult(row) {
        return row[0].replace('&nbsp;', '');
    } 
    
    $("#quicksearch_keywords").autocomplete("index.php?page=nk_autocomplete", {
        cacheLength: 1,
        minChars: 1,
        max: 12,
        autoFill: false,
        matchContains: false,
        scrollHeight: 220,
        formatItem: formatItem,
        formatResult: formatResult
    });
       
    $("#quicksearch_keywords").result(function(event, data, formatted) {
        if (data){
            $(this).parent().next().find("input").val(data[1]);
        } 
    }); 
            
    $(":text").result(findValueCallback).next().click(function() {
        $(this).prev().search();
    });
    
    $("#clear").click(function() {
        $(":input").unautocomplete();
    });
    
});
