var Finder = function(){
};

Finder.prototype.CSS_LOADING = "loading";
Finder.prototype.CSS_SELECTED = "selected";
Finder.prototype.CSS_HIDDEN = "wsodHidden";

Finder.prototype.init = function() {
	this.container = Element.get("marketFinderContainer");
	this.regionSelect = Element.get("marketFinderRegionSelect");
	this.countrySelect = Element.get("marketFinderCountrySelect");
	this.query = Element.get("marketFinderField");
	var form = Element.parseSelector("form", this.container, "first");
	var button = Element.parseSelector("div.basicButton", this.container, "first");
	
	Events.add(form, "submit", this.handleSubmit, this);
	Events.add(button, "click", function() {
		form.submit();
	});
	Events.add(this.query, "focus", function(e, el) {
		el.select();
	});
	
	if (this.regionSelect) {
		Events.add({
			element: this.regionSelect,
			type: "change",
			handler: this.changeCountrySelect,
			context:this	
		});
	}
	
	this.initResultEvents();
};

Finder.prototype.initResultEvents = function() {
	this.results = Element.get("searchResults");
	this.resultContainers = Element.parseSelector("div.resultContainer", this.results);
	
	SortableTable.initAllSortableTables(GroupedSortableTable);
	
	this.tabs = Element.parseSelector("ul.wsod-tabs-main a", this.results);
	if (this.tabs) {
		this.getTabSwitchEvent().removeAllElements();
		this.getTabSwitchEvent().addElement(this.tabs);
	}
};

Finder.prototype.getTabSwitchEvent = function() {
	var e = Events.add(null, "click", this.switchResultType, this);
	
	this.getTabSwitchEvent = function() {
		return e;
	};
	
	return this.getTabSwitchEvent();
};

Finder.prototype.getBuffer = function() {
	var cb = new ContentBuffer();
	
	this.getBuffer = function() {
		return cb;
	};
	
	return this.getBuffer();
};

Finder.prototype.getFormSerializer = function() {
	var fs = new FormSerializerLite();
	
	this.getFormSerializer = function() {
		return fs;
	};
	
	return this.getFormSerializer();
};

Finder.prototype.switchResultType = function(e, el) {
	e.cancel();
	el.blur();
	if (!Element.hasClass(el.parentNode, this.CSS_SELECTED)) {
		
		for (var i=0;i<this.tabs.length;i++) {
			Element.removeClass(this.tabs[i].parentNode, this.CSS_SELECTED);
		}
		Element.addClass(el.parentNode, this.CSS_SELECTED);
		
		var resultType = el.getAttribute("type");
		Element.addClass(this.resultContainers, this.CSS_HIDDEN);
		Element.removeClass(Element.get("results-" + resultType), this.CSS_HIDDEN);
		var groupField = Element.get("selectedResultGroup");
		if(groupField) groupField.value = resultType;
	}
};

Finder.prototype.setCountries = function(countries) {
	this.countries = countries;
};

Finder.prototype.changeCountrySelect = function() {
	if (this.regionSelect && this.countrySelect) {
		var regionKey = this.regionSelect.value;

		if(this.countrySelect.childNodes) {
			Element.removeChildNodes(this.countrySelect);
		}
		
		if("GBL" != regionKey) {
			//this.countrySelect.disabled = false;
			for(var key in this.countries) {
				if (regionKey == this.countries[key].regionCode){
					Element.create("option",{value:key},this.countries[key].name, this.countrySelect);
				}
			}
		} else {
			Element.create("option", {value:""}, " - Select Region - ", this.countrySelect);
			//this.countrySelect.disabled = true;
		}
		
	}	
};

Finder.prototype.handleSubmit = function(e, el) {
	e.cancel();
	
	if (trim(this.query.value)) {
		this.showLoading();
		this.getBuffer().abortRequests();
		
		this.getBuffer().load({
			url: "/ft/markets/data/getFinderResults.asp",
			data: {
				params: this.getFormSerializer().serialize(el)
			},
			onload: this.drawResults,
			onerror: this.drawError,
			context: this
		});
	}
	else {
		// this.error()
	}
};

Finder.prototype.drawResults = function(cb) {
	Element.removeChildNodes(this.results);
	Element.setHTML(this.results, cb.getResult());
	this.initResultEvents();
	this.hideLoading();
};
Finder.prototype.drawError = function(cb) {
	Element.removeChildNodes(this.results);
	Element.setHTML(this.results, "<p>Sorry, no results were found for your query \"" + this.query.value + "\"</p>");
	this.initResultEvents();
	this.hideLoading();
};

Finder.prototype.showLoading = function() {
	Element.addClass(this.results, this.CSS_LOADING);
};
Finder.prototype.hideLoading = function() {
	Element.removeClass(this.results, this.CSS_LOADING);
};

// just the header form as a standalone
var FinderModule = function() {
	FinderModule.Super(this);
};
FinderModule.Extend(Finder);

FinderModule.prototype.handleSubmit = function(e, el) {
	e.cancel();
	
	if (trim(this.query.value)) {
		el.submit();
	}
};

// mini side module
var MiniFinderModule = function() {
	MiniFinderModule.Super(this);
	
	this.advancedOptionsLink = Element.parseSelector("a.advancedOptions",this.container,"first");
	this.advancedOptionsContainer = Element.parseSelector("div.advancedOptionsContainer",this.container,"first");
	
	this.isAdvancedOptionsOpened = false;
	
	this.advancedOptLinkExpand = "+";
	this.advancedOptLinkCollapse = "-";
	this.advancedOptLinkText = "Advanced Options";
	
};

MiniFinderModule.Extend(Finder);

MiniFinderModule.prototype.init = function() {
	MiniFinderModule.Super(this, "init");
	if (this.advancedOptionsLink) {
		Events.add({
			element: this.advancedOptionsLink,
			type: "click",
			handler: this.toggleAdvancedOptions,
			context:this	
		});
	}
	//this.regionSelect.options[0].selected = true;
};

MiniFinderModule.prototype.initResultEvents = function() {
	// do nothing please
};

MiniFinderModule.prototype.toggleAdvancedOptions = function (e,el,args) {
	e.cancel();
	el.blur();
	if (this.advancedOptionsLink && this.advancedOptionsContainer) {
		if(!this.isAdvancedOptionsOpened){
			Element.removeClass(this.advancedOptionsContainer,"wsodHidden");
			this.isAdvancedOptionsOpened = true;
			var display = this.advancedOptLinkCollapse + ' '+ this.advancedOptLinkText;
			Element.setHTML(this.advancedOptionsLink, display);
		} else {
			Element.addClass(this.advancedOptionsContainer,"wsodHidden");
			this.isAdvancedOptionsOpened = false;
			var display = this.advancedOptLinkExpand + ' '+ this.advancedOptLinkText;
			Element.setHTML(this.advancedOptionsLink, display);
		}
	}
};

MiniFinderModule.prototype.handleSubmit = function(e, el) {
	e.cancel();
	
	if (trim(this.query.value)) {
		el.submit();
	}
};