
//

var autocompleteF = function (req, resp) {
    var s, ss, suggestions,
    core, params, url, term0, term, match, mode, type;
    core = 'nutch';
    //core = $("select[name=c] option:selected").attr("value");
    //alert(core);
    mode = 's';
    //mode = $("select[name=s] option:selected").attr("value");
    //alert(mode);
    url = 'suggest.html';
    //alert(req.term);
    term0 = '';
    term = req.term;
    match = term.match(/^(.*?)(\S+)$/);
    if (!match) {
	return([]);
    }
    type = (mode === "s") ? 'json' : 'text';
    term0 = match[1];
    term = match[2];    
    /*
    if (mode === "w") {
	term += "*";
    }
    */
    params = {q: term, wt: 'json', c: core, s: mode };
    //resp(['a', 'b']);
    $.ajax({url: url,
            data: params,
            type: 'GET',
            dataType: type,
            timeout: 5000,
            success: function (res) {
                //suggestions = text.split(/\n/);
                //resp(suggestions);
		//resp(['a', 'b']);
		//resp(res.spellcheck.suggestions[1].suggestion);
		if (mode === "s") {
		    ss = res.spellcheck.suggestions[1].suggestion;
		    suggestions = [];
		    $.each(ss, function(i, v) {
			    suggestions.push(term0 + v);
			});
		}
		else {
		    ss = res.split("\t");
		    suggestions = [];
		    $.each(ss, function(i, v) {
			    suggestions.push(term0 + v);
			});
		}
		resp(suggestions);
            },
            error: function (xhr, status, exception) {
                var error = true;
		/*
                if (status === "error") {
                    if (exception) {
                        alert('autocomplete error, status="' + status +
                              '", exception="' + exception + '"');
                    }
                    else {
                        alert('autocomplete error, status="' + 
                              status + '"');
                    }
                }
		*/
            }
           });
};


var onDocumentReady = function() {
    //alert('hier');
    $("input[name=q]").
    autocomplete({source: autocompleteF});
    //autocomplete('option', 'delay', 10});
};
$(document).ready(onDocumentReady);

