function formatItemMAIN(row, position, n, term) {
//console.debug("position:" + position);
//console.debug("positionMax:" + this.max);
/* Used to solve cache problems - flush Cache after each printed item*/
/* Återskapa bug genom att kommentera bort nedan och testa med "företag" */
//console.debug("position:" + position);
//if(position === (this.max)){
	//console.debug("flushCache");
	//console.debug("position:" + position);
/*	if($("#keywords-h").length > 0){
		$("#keywords-h").flushCache();
	}
	if($("#keywords-m").length > 0){
		$("#keywords-m").flushCache();
	}
	if($("#keywords-f").length > 0){
		$("#keywords-f").flushCache();
	}
*/
if(position == 1){
	return "<span>" + row[0] + "</span><span id=\"ac_suggestion\">Förslag</span>";
 }
 else{
	return "<span>" + row[0] + "</span>";
 }
}

function formatResultMAIN(row) {
	return row[0];
}

function formatMatchMAIN(row) {
	return row[0];
}

function formatItemSG(row, position) { 

    var row_array = row[0].split(":"); 
     if(position == 1){ 
            return "<span>" + row_array[0] + " ("+row_array[1]+" i "+row_array[2]+")</span><span id=\"ac_suggestion\">Förslag</span>"; 
     } 
     else{ 
            return "<span>" + row_array[0] + " ("+row_array[1]+" i "+row_array[2]+")</span>"; 
     } 
} 

function formatResultSG(row) { 
    var row_array = row[0].split(":"); 
    return row_array[0]; 
} 
    
function formatMatchSG(row) { 
    var row_array = row[0].split(":"); 
    return row_array[0] + " " + row_array[1]; 
} 


jQuery(document).ready(function () {
	var url = "/wps/wcm/jsp/acBackend.jsp?url=http://www9.goteborg.se/perlgsa/final_querycompletion_popular_queries_manually_added.txt&method=std";
	//var urlSg = "/wps/wcm/jsp/acBackend.jsp?url=http://www4.goteborg.se/prod/sk/serviceguide.nsf/agGoogleIndexM&method=sg"
	//Initialize autocompletion
	$("#keywords-m").autocomplete(url, {
        minChars: 2,
        delay: 250,
        scroll: true, 
        selectFirst: false, 
        matchContains:true, 
        cacheLength:10, 
        matchSubset:true,
        max:100,
		formatItem: formatItemMAIN,
		formatResult: formatResultMAIN,
		formatMatch: formatMatchMAIN		
	}).result(function(event, data, formatted) {
		// submit form when value is choosen in drop down list
		$("#keywords-m").parent().parent("form").submit();
	}); 
	
	//Initialize autocompletion
	$("#keywords-h").autocomplete(url, {
        minChars: 2, 
        delay: 250,
        scroll: true, 
        selectFirst: false, 
        matchContains:true, 
        cacheLength:10, 
        matchSubset:true,
        max:100,
		formatItem: formatItemMAIN,
		formatResult: formatResultMAIN,
		formatMatch: formatMatchMAIN		
	}).result(function(event, data, formatted) {
		// submit form when value is choosen in drop down list
		$("#keywords-h").parent().parent("form").submit();
	});
	
	//Initialize autocompletion
	$("#keywords-f").autocomplete(url, {
        minChars: 2, 
        delay: 250,
        scroll: true, 
        selectFirst: false, 
        matchContains:true, 
        cacheLength:10, 
        matchSubset:true,
        max:100,
		formatItem: formatItemMAIN,
		formatResult: formatResultMAIN,
		formatMatch: formatMatchMAIN		
	}).result(function(event, data, formatted) {
		// submit form when value is choosen in drop down list
		$("#keywords-f").parent().parent("form").submit();
	});
/* Autocomplete for service guide search not used at the moment	
	$("#keywords-m-sg").autocomplete(urlSg, { 
        minChars: 2, 
        scroll: true, 
        selectFirst: false, 
        matchContains:true, 
        cacheLength:10, 
        matchSubset:true,
        max:100,
        formatItem: formatItemSG, 
        formatResult: formatResultSG, 
        formatMatch: formatMatchSG 
	}); */
});

