/**
 * enableInlineSearch: will only be set to true if the summary panel is displayed
 */
var enableInlineSearch = false;


/**
 *  Initialise the Inline Search upon load of the page
 */
window.addEvent("domready", function() {
	if (enableInlineSearch) {
		initInlineSearch();
	}
});


/**
 * Function to display the inline search widget
 */
function showInlineSearch() {
	if (enableInlineSearch && $("inlineSearch")) {
		$("inlineSearch").fade("in");
	}
}

/**
 * Function to hide the inline search widget
 */
function hideInlineSearch() {
	if (enableInlineSearch && $("inlineSearch")) {
		$("inlineSearch").fade("out");
		$("searchTip").fade("out");
		$("searchField").set("value","");
		$("searchResults").set("html","&nbsp;")
	}
}

/**
 * Function to initialise the InlineSearch widget
 */
function initInlineSearch() {
	// add a onMouseOver event to the searchToggle div
	$("searchToggle").addEvent("mouseover", function() {
		$("searchToggle").addClass("hovered");
		if (!$('searchTip').closed) {
			$("searchTip").fade("in");	//display the search tips
		}
	});
	
	// add a onMouseOut event to the searchToggle div
	$("searchToggle").addEvent("mouseout", function() {
		$("searchToggle").removeClass("hovered");
		if (!$("searchPanelContainer").slideFx.open) {
			$("searchTip").fade("out");  //hide the search tips if the search widget is closed
		}
	});
	
	// Create a Mootools Slide effect and store on the container element for reference
	$("searchPanelContainer").slideFx = new Fx.Slide('searchPanel',{mode: 'horizontal'}).hide();;
	$("searchPanelContainer").setStyle("visibility","visible");
	
	//Display the main inline search widget element as visible.
	$("inlineSearch").setStyle("visibility","visible");
	
	$("searchField").set("value",""); //Init the search input to be empty
	
	//Create an instance of the InlineSearch controller
	//passing the any .summary-text elements as the search targets
	var searcher = new InlineSearch(".summary-text", "searchField", true, false);
	
	searcher.minLength = 2; //Set the minLength to be 2 characters	
	searcher.onSearchComplete = updateResult.bind(searcher); // set updateResult() to be run upon search completion
	
	// add onClick event to the search toggle element to open/close the inline search widget
	$("searchToggle").addEvent("click", function() {toggleSearchPanel(this)}.bind(searcher));
	
	//add onClick event to the searchResults element to allow iteration through results via mouse
	$("searchResults").addEvent("click", function() {
		this.focusNextResult();
	}.bind(searcher));
}


/**
 * Open/Close the Inline Search widget 
 */
function toggleSearchPanel(searcher) {
	if (!$("searchPanelContainer").slideFx.open) {
		$("searchField").focus();
		$("searchToggleLabel").fade("out");
		(function() {$("searchToggle").tween('width', '30px')}).delay(50);
	} else {
		$("searchToggle").tween('width', '66px');
		(function() {$("searchToggleLabel").fade("in").setStyle("display","inline")}).delay(500);
		if (searcher._enableBlockout) {
			searcher._blockout.hideBlockout();
			$$(".focused").each(function(e) {e.removeClass("focused")});
		}
	}
	$("searchPanelContainer").slideFx.toggle();
}	

/**
 *  Update the search results with the number of results and resume focus to the search input field
 */
function updateResult() {
	$("searchResults").set("html", this.numberOfMatches + "x "+ (this.numberOfMatches==1?"match":"matches"));
	if (this.numberOfMatches>0) {
		//$$(".highlighted")[0].focus();
		$("searchField").focus();
		this._currentlyFocused = null;
	}
}
