function BrowseByClassificationApp() {
	this.appState={};
	this.appState.id = "";
	this.appState.locale = "en";

	// ID of div and identifier for flash movie	
	this.flashID = "classificationBrowser";	
	
	BrowseByClassificationApp.baseConstructor.call(this, $("#searchComponent"));
}

BrowseByClassificationApp.extend(PagedSearch);

/*
BrowseByClassificationApp.prototype.getSubscriptionEvents = function() {
	return [new SubscriptionEvent("taxonomyLoaded", this.onTaxonomyLoaded)];
}

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


// circumvented for now -> IE problems with passing xml string to Flash

BrowseByClassificationApp.prototype.startUp = function(){
	console.log("BrowseByClassificationApp.startUp");
	
	//this.taxonomyComponent.loadTaxonomy(true);
}


BrowseByClassificationApp.prototype.onTaxonomyLoaded = function(type, args) {
	console.log("onTaxonomyLoaded");
	this.taxonomyComponent = args[0];
	this.onAppReady();
}


// trigger when everything is ready
BrowseByClassificationApp.prototype.onAppReady = function(){
	console.log("BrowseByClassificationApp.onAppReady");
	BrowseByClassificationApp.superClass.onAppReady.call(this);
	this.embedFlashMovie();	
}
*/
BrowseByClassificationApp.prototype.initDOMElems = function(){
	BrowseByClassificationApp.superClass.initDOMElems.call(this);
	
	var _this=this;
	this.$tabs=$("#languageTabs").tabs();

	this.$tabs.bind('tabsselect', function(event, ui) {
	  	console.log("tabClicked "+ui.tab.toString().split("locale_")[1]);
	   	_this.setLocale(ui.tab.toString().split("locale_")[1]);
	    return true;
	});
}

BrowseByClassificationApp.prototype.onStateChange=function(event){	
	
	console.log("BrowseByClassificationApp.onStateChange", event);
	
	if(unescape(SWFAddress.getValue()) == this.lastSWFAddressValue){
		console.warn("Executing the same query twice?");
		return;
	}	

	this.lastSWFAddressValue = unescape(SWFAddress.getValue());
	this.query.constructFromURLString(SWFAddress.getParameter("query"));

	this.appState.id = this.query.constraints.classification.length ? this.query.constraints.classification[0].value : "root";

	var pageNum = Number(SWFAddress.getParameter("page"));
	this.currentPage = (Utils.isUndefined(pageNum) || isNaN(pageNum)==-1 || isNaN(pageNum) || pageNum < 1) ? 1 : pageNum;

	var locale = SWFAddress.getParameter("locale");
	this.appState.locale = (Utils.isUndefined(locale)) ? "en" : locale;
	
	if(!this.flashEmbedded){
		this.flashEmbedded = true;
		this.embedFlashMovie();	
	} 
	
	this.displaySelection(this.appState.id);
	this.displayLocale(this.appState.locale);
	this.loadResults(this.currentPage);
}

// store application state in URL
// -> will call onStateChange via SWFAddress listener
BrowseByClassificationApp.prototype.storeStateInURL=function(){
	SWFAddress.setValue("?locale="+this.appState.locale+"&page="+this.currentPage+"&query="+this.query.toURLString());
}

BrowseByClassificationApp.prototype.embedFlashMovie=function(){
	
	console.log("BrowseByClassificationApp.embedFlashMovie");
	
	var swfURL = MACEConstants.toolsURL+"browseByClassification/swf/Classification_Browse.swf";
	var countsUrl = encodeURIComponent(MACEConstants.componentsURL+"search/php/SQIServiceConnector.php?PLQLstring=lom.solr%3D%22all%22&resultsFormat=solr");

	// testing
	//var countsUrl = MACEConstants.toolsURL+"browseByClassification/data/classificationCountsSolr.xml";

	/*
	// pass xml String
	var xml = encodeURIComponent(this.taxonomyComponent.xml);		
	var flashvars = {
		"locale":					l,
	  	"startTerm":				startTerm,
	  	"vocabularyXMLString":		xml,
	  	"classificationCountURL": 	countsUrl
	};
	*/
	
	// pass URL  
	
	// not compressed
	// taxurl = MACEConstants.rootURL + "components/taxonomy/vocabulary.xml";
	// compressed
	var taxurl = MACEConstants.rootURL + "components/taxonomy/php/vocabulary.xml.php";
	
	var flashvars = {
		"locale":					this.appState.locale,
	  	"startTerm":				this.appState.id,
		"vocabularyURL":			taxurl,
	  	"classificationCountURL": 	countsUrl
	};
		
	var params = {
		"id":		this.flashID,
		"name":		this.flashID
	};

	var attributes = {
		"menu": 	"false",
		"salign":	"TL",
		"scale":	"noscale",
		"wmode":	"opaque",
		"allowScriptAccess":"always"		
	};	
	
	swfobject.embedSWF(swfURL, this.flashID, "100%", "360", "9.0.0", MACEConstants.rootURL+"js/flashInstall/expressInstall.swf", flashvars, params, attributes);	
	console.log("end embed", swfURL, this.flashID, "100%", "360", "9.0.0", MACEConstants.rootURL+"js/flashInstall/expressInstall.swf", flashvars, params, attributes);
}

BrowseByClassificationApp.prototype.setLocale=function(locale){
	this.appState.locale=locale;
	this.storeStateInURL();
}

BrowseByClassificationApp.prototype.displayLocale=function(locale){
	this.$tabs.tabs('select', '#locale_' + locale);
	try{
		var f = Utils.getFlashMovie(this.flashID);	
		f.setLocale(locale);
	} catch(e){
		console.warn("error setting locale. Flashfilm not ready?")
	}	
}

BrowseByClassificationApp.prototype.displaySelection=function(id){
	try{
		var f = Utils.getFlashMovie(this.flashID);	
		f.setFocus(id);	
	} catch(e){
		console.warn("error displaying selection. Flashfilm not ready?")
	}	
	this.updateWindowTitle();
}

/** FLASH EVENT handler **/

BrowseByClassificationApp.prototype.triggerSearchByID=function (id, label){
	this.doClassificationSearch(id, label);
}

