var hasSearched = false;
var sb = $("#searchBox");
var rb = $("#searchResults");
var hideDelay = 800;
var searchDelay = 300;

sb.click(function(){
	if (hasSearched) {
		showSearch();
	}
});

var searchTimeout;
sb.keyup(function(){
	clearTimeout(searchTimeout);
	if ($(this).val().length > 1) {
		hasSearched = true;
		searchTimeout=setTimeout("doSearch()",searchDelay);
	} 
});

sb.mouseenter(function(){ sb.addClass("hasFocus");resetFocus(); });
sb.mouseleave(function(){ sb.removeClass("hasFocus");checkFocus(); });
rb.mouseenter(function(){ rb.addClass("hasFocus");resetFocus(); });
rb.mouseleave(function(){ rb.removeClass("hasFocus");checkFocus(); });

var hideTimeout;
resetFocus = function(){
	clearTimeout(hideTimeout);
}

checkFocus = function(){
	hideTimeout=setTimeout("hasFocus()",hideDelay);
}

hasFocus = function(){
	if (!($(".hasFocus").length)) {
		hideSearch();
	}
}

showSearch = function(){
	rb.fadeIn("slow");
}
hideSearch = function(){
	rb.fadeOut("slow");
}

doSearch = function(){
	criteria = sb.val();
	$.ajax({
		type: "POST",
		url: "/data/inc_zoeken.cfm",
		data: { 
			search: criteria
		},
		cache: false,
		success: function(html){
			rb.html(html);
			if (!rb.is(":visible")) {
				showSearch();
			}
		}
	});
}
