
function exposeQuestion(parent, text){

	var newText = document.createElement("span");
	newText.innerHTML = text.nodeValue.replace(this.regEx, this.options.markup);
	
	parent.replaceChild(newText, text);
	
	Element.addClassName(parent.parentNode, "highlight");
	this.eleArr.push({ parent : parent, newNode : newText, oldNode : text});
	
}
function onClear(obj){
	
	obj.parent.replaceChild(obj.oldNode, obj.newNode);
	
	Element.removeClassName(obj.parent.parentNode, "highlight");
	
}
var curHighlight = {};

function searchFAQ(searchTerm){
	if(searchTerm == ""){
		alert("please enter a value to search for");
		return false;
	}
		
	
	try{
		curHighlight.clearAll();
	}
	catch(e){
		
	}
		
	try{
		curHighlight = new HighLight(searchTerm, 
						 { 
							src : "feed",
							tag : "p",
							clear : onClear,
							foundElement : exposeQuestion
						 }
					  );
		}
	catch(e){
		console.log("Failed to create new highlight %o", e);
	}
	
	notify(searchTerm, curHighlight);
	
}
function notify(term, highLightObj){
	var results = highLightObj.eleArr.length;
	var str = "";
	
	if(results == 0)
		str = "Your search for '"+term+"' was not found in any of the answers.";
	else if(results == 1)
		str = "Your search for '" +term+"' has been located in " + results + " answer. Please scroll down to see the the answer that has been opened below.";
	else
		str = "Your search for '" +term+"' has been located in " +results+ " answers. Please scroll down to see the answers that have been opened below.";
	
	Element.setOpacity("notify", 0);
	Effect.Appear("notify",
							{
								afterSetup : function(fx){
									fx.element.innerHTML = str;
								},
								queue : "end"
							
							}
					);

}


function clearSearch(){
	try{
		curHighlight.clearAll();
	}
	catch(e){
		
	}
	$("searchInput").value = "";
	$("notify").innerHTML = "";
}
function checkEnter(e){
	
	if((e.keyCode || e.which) == Event.KEY_RETURN)
		searchFAQ(Event.element(e).value);

}
