function initSearchSuggestions(sokInputId, sokKnappId, forslagDivId, urlprefix) {
	initSearchSuggestionsDo(sokInputId, sokKnappId, forslagDivId, urlprefix, 'searchw', '', '')
}

function initSearchSuggestionsDo(sokInputId, sokKnappId, forslagDivId, urlprefix, urlform, clickFunction, onchangeFunction) {
	var sokInput = "#" + sokInputId; 
	var forslagDiv = "#" + forslagDivId;
	var sokKnapp = "#" + sokKnappId;
	var old_selection = "";
	var soktext_delay = "";
	var typed_text = "";
	var cache = new Array();
	
	if($(sokInput).length > 0 && $(forslagDiv).length > 0) {
		
		$(forslagDiv).addClass("sokforslag_div");
		$(sokInput)
			.bind("keypress keydown keyup", function (e)
			{
				var key = e.keyCode;
				var type = e.type;
				if (type == 'keyup') {
					if (key == 13) { 
						sokInput_soktext_klick() ;
						$(forslagDiv).trigger("gom");
					} else if (key == 32) {
						sokInput_fill(" ");
					} else if (key == 39) {
						sokInput_fill("");
					} else if (key == 38 || key == 40) {
						if ($(forslagDiv).trigger("visa"))
						{
							if (key == 38)
								$(forslagDiv).trigger("forra");
							else
								$(forslagDiv).trigger("nasta");
						}
						else 
						{
							ajaxSearchSuggestions();
						}
					} else if (key == 27) {
						$(forslagDiv).trigger("gom");
					} else if ((key > 45 && key < 90) || key==8 || (key > 190 && key < 230)) {
						if (onchangeFunction)
							onchangeFunction();
						ajaxSearchSuggestions();
						typed_text = $(sokInput).val()
					}
				} else if (type=='keydown') {
					if (key == 13) { 
						return false;
					}
				} else if (type=='keypress') {
					if (key == 9) {
						sokInput_fill("");
					}
				}
			});
		
		$(forslagDiv)
			.bind("gom", function () 
				{
					clearTimeout(soktext_delay);
					soktext_delay = setTimeout(function () {
						$(forslagDiv).hide().css("display", "none");
					}, 10);
				})
			.bind("tom", function () 
				{
					$(this).html("");
				})
			.bind("visa", function () 
				{
					$("body").bind("mouseup", function() { $(forslagDiv).trigger("gom"); $("body").unbind("mouseup"); });
					if ($(this).find("a").length>0) {
						$(this).show();
						return true;
					} else {
						$(forslagDiv).trigger("gom");
						return false;
					}
				})
			.bind("nasta", function() 
				{
//					alert(!old_selection)
//					alert($(this).find("a:first").attr("id"))
					var new_selection;
					if (!old_selection)
						new_selection = $(this).find("a:first").attr("id");
					else 
						new_selection = $("#" + old_selection).next("a").attr("id");
					$(this).trigger("hover", new_selection);
					$(sokInput).val($("#" + new_selection).text());
				})
			.bind("forra", function() 
				{
					var new_selection;
					if (!old_selection) 
						new_selection = $(this).find("a:last").attr("id");
					else 
						new_selection = $("#" + old_selection).prev("a").attr("id");
					$(this).trigger("hover", new_selection);
					$(sokInput).val($("#" + new_selection).text());
				})
			.bind("hover", function(event, s) 
				{
					//alert(s);
					if (old_selection)
					{
						$("#" + old_selection).removeClass("selected");
					}
					if (s)
					{
						$("#" + s).addClass("selected");
					}
					else
					{
						$(sokInput).val(typed_text)
					}
					old_selection = s;
					//alert(s)
				});
			
		
		ajaxSearchSuggestions = function() { 
			//forslagDiv.gom(); 
			//forslagDiv.tom(); 
			
			onSuccess = function (httpRequest) {
				$(sokInput).removeClass("loading");
				var t = httpRequest.responseText
				
				var a = new Array();
				
				// Get JSON results
				s = eval(t)[0];
				a = eval(t)[1];
				
				cache[s] = new Array();
				cache[s] = a;
				doResults(s, a);
			}
			
			doResults = function(s, a) {	
				var b = new Array();
				var output = '';
				
				if (a.length==0) {
					if (noresultsFunction)
						noresultsFunction();
				} else {
					for (i=0 ; i<a.length && b.length<10 ; i++) {
						if (a[i]!='') {
							if (clickFunction!='') {
								tempname = a[i][1];
								tempid = a[i][0];
							} else {
								tempname = tempid = a[i];
							}
							tempname = tempname.toLowerCase();
							if (!b.contains(tempname)&&!b.contains(tempname.substr(0,tempname.length-1))) {
								b.push(tempname);
								temptext = '<b>' + tempname.substr(0, s.length) + '</b>' + tempname.substr(s.length,tempname.length);
								output+='<a href="#" tabindex=1000 id="forslag'+ i + '"' + ' title="'+ tempname+'" alt="'+tempid+'">'+temptext+'</a>';
							}
						}
					}
				}
				$(forslagDiv)
					.html(output)
					.find("a")
						.bind("click", function() { $(sokInput).val($(this).text()).focus(); return false; })
						.bind("mouseover", function() { $(forslagDiv).trigger("hover", $(this).attr("id")) })
						.end()
					.trigger("visa");
				old_selection = "";
			}
			
			if ($(sokInput).val().length>0) {
				//$(sokInput).addClass("loading");
				tmp =  cache[$(sokInput).val()];
				if (tmp)
				{
					doResults($(sokInput).val(), tmp);
				}
				else 
				{
					clearTimeout(soktext_delay)
					soktext_delay = setTimeout(function () 
					{
						makeAjaxRequest('search_suggestions', urlprefix+urlform+'.htm', 'soktext='+$(sokInput).val(), onSuccess, onSuccess, 0);
					}, 100);
				}
			} else {
				$(forslagDiv).trigger("gom");
			}
		};
		
		function sokInput_fill(s) {
			var t = "";
			if (old_selection)
				t = $("#" + old_selection).attr("title");
			if (t)
				$(sokInput).val(t+s);
			$(forslagDiv).trigger("gom");
		}
		
		function sokInput_soktext_klick(s, t) 
		{
			sokInput_fill("");
			if (clickFunction!="")
				clickFunction(s, t);
			else 
				$(sokKnapp).trigger("click");
			$(sokInput).trigger("focus");
			return false;
		}
		
	}
}
