JSONDecoder = new (function(){
	this.decode = function(json){
		return eval("(" + json + ')');
	};
})();

var searchReq;
var complete = false;

function doServerQuery(searchTerm) {
	//if(searchReq==undefined){
		complete = false;
		searchTerm = encodeURI(searchTerm);
		searchReq = $.ajax({
			url: searchSuggestURL,
			type : "GET",
			data : "q=" + searchTerm,
			success: function(response){
				complete = true;
				handleSearchSuggest(response);
			}
		});
	//}
	//else{
	//	searchReq.data = "q=" + searchTerm;
	//	searchReq.request(searchSuggestURL);
	//}
}

/**
 * Starts a delayed search suggest. 
 */
function suggest()
{
	window.setTimeout('searchSuggest()', 500);
}

/**
 * Does the actual suggest.
 */
function searchSuggest() {
	if (suggestvisible == false) {
		$('#search_suggest').fadeOut(500).html('');
	}else {
		//$('#search_suggest').html('');
	}
	var str = $("#searchField").val();
	
	//do not ask server for suggestions if user typed in 0 characters,
	//because of the huge amount of suggestions this would make no sense
	if(str.length == 0) return;

	//do a server call only if there is no request still on the run
	//because it keeps us from making requests quicker than we recieve them
	//if (searchReq==undefined || searchReq.transport.readyState == 4 || searchReq.transport.readyState == 0) {
	if (searchReq==undefined || complete == true) {
		doServerQuery(str);
	}
}

/**
 * Handles the returned JSON response.
 */
function handleSearchSuggest(responseText) {
	if (suggestvisible == false) {
		$('#search_suggest').fadeOut(300).html('');
	}else {
		$('#search_suggest').empty();
	}
	var jsonPayload	= JSONDecoder.decode(responseText);
	if(jsonPayload.suggestions.length==0) {
		$('#search_suggest').empty().hide();
		suggestvisible = false;
	}
	for(i=0; i < jsonPayload.suggestions.length; i++) {
		//Build our element string.  This is cleaner using the DOM, but
		//IE doesn't support dynamically added attributes.
		var suggest = '<div onmouseover="javascript:suggestOver(this);" ';
		suggest += 'onmouseout="javascript:suggestOut(this);" ';
		suggest += 'onclick="javascript:setSearch(this,\''+jsonPayload.suggestions[i].suggestion+'\');" ';
		suggest += 'suggestion="'+ jsonPayload.suggestions[i].suggestion + '" ';
		suggest += '><span class="hitcount">'+ '</span><span class="term">' + jsonPayload.suggestions[i].suggestion+' ('+jsonPayload.suggestions[i].count+')</span></div>';

		$('#search_suggest').append(suggest);
	}
	if (suggestvisible == false) {
		if (jsonPayload.suggestions.length != 0) {
			$('#search_suggest').fadeIn(100);
			suggestvisible = true;
		}
	}else {
		if (jsonPayload.suggestions.length != 0) {
			$('#search_suggest').fadeIn(1);
		}
	}
}

/**
 * Handles mouse over
 */
function suggestOver(div_value) {
	div_value.className = 'search_suggest_over';
}

/**
 * Handles mouse out
 */
function suggestOut(div_value) {
	div_value.className = '';
}

/**
 * Handles suggest click (alternative 1)
 */
function setSearch(divNode,suggest_kw) {
	$("#searchField").attr("value",suggest_kw);
	$('#search_suggest').fadeOut(500);
	suggestvisible = false;
	document.forms.SimSearch.submit();
}

/**
 * Handles suggest click (alternative 2)
 */
function setSearchTerm(divNode) {
	$("#searchField").val(divNode.attr("suggestion"));
	$('#search_suggest').fadeOut(500);
	suggestvisible = false;
}

//this is so that the suggest div fades out if your mouse leaves it.
//mouseoffsearch will cancel the timeout if it is false.
$(document).ready(function(){
var mouseoffsearch = true;


$("#search_suggest").mouseleave(function () {
		mouseoffsearch = true;
      	 setTimeout( function() {
      		if (mouseoffsearch == true) {
      			$("#search_suggest").fadeOut(500);
      			suggestvisible = false;
      		}
      	}, 1000);
      });
	$("#search_suggest").mouseenter(function() {
		mouseoffsearch = false;
	});     
	}
 )
