/*

	FilteredSearch class
	
 */

function FilteredSearchApp() {
	console.log("FilteredSearch App constructor");
	this.flashID="elasticlists";
	FilteredSearchApp.baseConstructor.call(this, $("#searchComponent"), null, true);
}

FilteredSearchApp.extend(PagedSearch);

// overwritten to trigger preload chain
// 1. taxonomy
// 2. flash movie
// 3. onAppready in super class -> initHistory -> first query

FilteredSearchApp.prototype.startUp = function(){
	console.log("FilteredSearchApp.startUp");
	this.componentController.init();
	this.initDOMElems();
	this.embedFlashMovie();
}

FilteredSearchApp.prototype.embedFlashMovie = function(type, args) {
	$("#filtersLoadingMessage").hide();
	
	var flashvars = {
	};

	var params = {
		"name":	this.flashID,
		"id":	this.flashID		
	};

	var attributes = {
		"menu": 	"false",
		"salign":	"TL",
		"scale":	"noscale",
		"wmode":	"opaque",
		"allowScriptAccess":"always"
	};
	
	var swfURL=MACEConstants.toolsURL+"filteredSearch/swf/elasticlists.swf";
	swfobject.embedSWF(swfURL, this.flashID, "914", "224", "9.0.0", MACEConstants.rootURL+"js/flashInstall/expressInstall.swf" ,flashvars, params, attributes);	
}

// flash movie calls this when ready
	
FilteredSearchApp.prototype.onElasticListsReady = function() {
	console.log("App.onElasticListsReady");
	this.elasticLists=Utils.getFlashMovie(this.flashID);
	this.onAppReady();
}

FilteredSearchApp.prototype.initDOMElems = function(){
	FilteredSearchApp.superClass.initDOMElems.call(this);
	
	var _this = this;

	this.$searchForm=$("#keywordSearchForm");
	this.$keywordInputField=$("#searchBox");
	this.$resetButton=$("#resetButton");	
	// search form submit action
	this.$searchForm.submit(
		function(){
			//_this.query.reset();
			_this.doTermSearch(_this.$keywordInputField.val());
			return false;
		}
	);

	this.$resetButton.click(
		function(){
			_this.query.reset();
			_this.onQueryChanged();
			return false;
		}
	);
	
	// autocompletion on search fields
	// this.$keywordInputField.autocomplete(MACEConstants.componentsURL+"autocomplete/php/autocomplete.php");
	// this.$keywordInputField.setOptions({selectFirst:false});
}

// callback when new query is started
FilteredSearchApp.prototype.onStateChange=function(event){	
	// superclass will trigger new search
	console.log("FilteredSearchApp.onStateChange");
	FilteredSearchApp.superClass.onStateChange.call(this, event);
	//
	try{
		this.elasticLists = Utils.getFlashMovie(this.flashID);
		this.elasticLists.displayQuery(this.query);
	} catch(e){}
	
	
	// fill search field
	var searchTerms=this.query.constraints.searchTerm;
	
	if(searchTerms.length){
		this.$keywordInputField.val(searchTerms.join(" "));
	} else {
		this.$keywordInputField.val("");
	}	
	
	if(!this.query || this.query.isEmpty()){
	 // Disable resetButton
	 	this.$resetButton.attr("disabled","disabled");
	} else {
	 // Enable resetButton
	 	this.$resetButton.removeAttr("disabled");
	}
}

// replaces existing classification terms
FilteredSearchApp.prototype.onElasticListsFiltersChanged = function(filters){
	console.log("PagedSearch.onElasticListsFiltersChanged", filters);
	
	this.query.clearConstraints("LOMconstraints");
	
	for (var i in filters){
		var filter=filters[i]; 
		this.query.addConstraint("LOMconstraint", filter);
	}
	
	this.onQueryChanged();
}

FilteredSearchApp.prototype.displayFacetResults = function() {
	FilteredSearchApp.superClass.displayFacetResults.call(this);
	try{
		this.elasticLists = Utils.getFlashMovie(this.flashID);
		this.elasticLists.displayStats(this.facetCounts);
		this.elasticLists.displayQuery(this.query);
	} catch(e){
		console.log("error displaying facet results");
	}
}


