/** 
 * SearchbarController
 * Provides any input event handling required for SearchBar
 */
 

function SearchBarController() {
} 

/**
* It's a singleton
*/
SearchBarController.instance=null;
/**
 *  singleton, call this function to get search bar controller instance, do not new SearchBarController() plz.
 *  @return searchbarController instance
 */
SearchBarController.getInstance = function(){
	if(SearchBarController.instance==null)
		SearchBarController.instance = new SearchBarController();
	return SearchBarController.instance;
}


/** 
* Initialize
* Register for events of interest
* 
*/
SearchBarController.prototype.init = function(kw) {
	TSWIDGET.log("Initializing searchBarController");
	var self = this;
	e = document.getElementById("searchBarLink");
	YAHOO.util.Event.addListener(e, "click", this.searchSubmit, e, self);
	form = document.forms['SearchBarForm'];
	YAHOO.util.Event.addListener(form, "submit", this.searchSubmit, form, self);
	YAHOO.util.Event.on("turnOff", "click", this.turnOffParsing);		
}
SearchBarController.prototype.turnOffParsing = function(e){
	YAHOO.util.Event.preventDefault(e);
	var filterCookie = new FilterCookie();
	filterCookie.SetCookie("PREVENTPARSINGKEYWORD","1");
	//filterCookie.SetCookie("INITPAGE","1");
	document.location.href = document.location.href;
}

SearchBarController.prototype.searchSubmit=function(e, elem) {
	YAHOO.util.Event.preventDefault(e);
	TSWIDGET.log("SearchBarController::searchSubmit()");
	/*if(get("originalKW").value!=get("txtKeyword").value){
		this.removeFilters();
	}*/
	Analytics.prototype.recordEvent("Search_Filter_SearchBar","Search_Filter_SearchBar:search_keyword",get("txtKeyword").value);
	document.forms['SearchBarForm'].submit();
}

SearchBarController.prototype.removeFilters = function(){
	var constKeys = "keyword,page_size,display_format,display_size,sort_column,sort_order,sort";
	var hiddens = YAHOO.util.Dom.getElementsBy(function(obj){if(obj.type=="hidden" && constKeys.indexOf(obj.name.toLowerCase())==-1)return true;else return false}, "input", "searchBarForm",null);
	var len = hiddens.length;
	for(var i=len-1;i>=0;i--){
		$(hiddens[i]).remove();
	}
}