jQuery(function() {
	
	var timeout;
	
	function activate_search() {
		jQuery("#header-search-form").addClass("searchform-active");
	}
	
	function deactivate_search() {
		jQuery("#header-search-form").removeClass("searchform-active");
	}
	
	jQuery("#header-search-box").autocomplete("/applications/site_search/search_autocomplete.rm", {
		width: 190,
		minChars: 2,
		max: 5,
		autoFill: false,
		mustMatch: false,
		matchContains: true,
		resultsContainer:"#header-search-form",
		selectFirst: false
	}).focus(function() {
		activate_search();
	}).blur(function() {
		clearTimeout(timeout);
		timeout = setTimeout(deactivate_search, 200);
	}).result(function(event, item) {
		//var tempstr = jQuery('<p>'+item[0]+'<p>');
		//jQuery("#header-search-box").val(jQuery(tempstr).text());
		location.href = item[1];
	});
	
	jQuery("#header-search-form").mouseover( function() {
		activate_search();
		//clear nav drop down, as there is a bug with it being stucked there when mouseover form from nav
		jQuery('ul#header-links li.toplevel').removeClass('over');
	});
	
	jQuery("#header-search-form").mouseout( function() {
		if (jQuery("div.ac_results").eq(0).is(":hidden") || jQuery("div.ac_results").length < 1) {
			deactivate_search();
		}
	});
	
});
