var SearchForm = new Class({

	initialize: function() {
		if (!$('search-form')) {
			return false;
		}

		this.tabs = [$('buy'), $('rent')];
		this.tabs.each(function(el) {
			if (el) {
				el.addEvent('click', this.switchTab.bindWithEvent(this));
			}
		}, this);

		this.fields = [$('PropertyStateId'), $('PropertyDistrictId'), $('PropertyPropertyTypeId'), $('PropertyDealType')];
		this.fields.each(function(el) {
			if (el) {
				el.removeEvents('change');
				el.addEvent('change', this.update.bindWithEvent(this));
			}
		}, this);
		this.activate(true);
	},

	update: function(e) {
		data = {};
		this.fields.each(function(el) {
			data[el.name] = el.value;
		});

		new Ajax('/properties/searchForm', {data: data, onComplete: this.updateComplete.bind(this)}).request();
		this.activate(false);
	},

	updateComplete: function(resp) {
		$('ajax-search-form').setHTML(resp);
		this.initialize();
	},

	switchTab: function(e) {
		e.stop();
		tab = e.target.id;
		$ES('#navigation li').each(function(el) {
			el.removeClass('selected');
		});
		$(tab).addClass('selected');
		this.fields.each(function(el) {
		  el.value = '';
		});
		$('PropertyDealType').value = tab;
	},

	activate: function(enable) {
		this.fields.each(function(el) {
			el.disabled = !enable;
		});
	}
});

var SearchController = new Class({

	initialize: function() {
		this.initFilters();
		this.initPaging();
	},

	initFilters: function() {
		$ES('#filter-form input[type=checkbox]').each(function(el) {
			el.addEvent('click', this.updateResults.bindWithEvent(this));
		}, this);
		$ES('#filter-form select').each(function(el) {
			el.addEvent('change', this.updateResults.bindWithEvent(this));
		}, this);
	},

	initPaging: function() {
		if (!$('search-results')) {
			return false;
		}

//		$ES('ul.pages a').each(function(el) {
//			el.addEvent('click', this.switchPage.bindWithEvent(this));
//		}, this);
	},

	switchPage: function(e) {
		el = e.stop().target;
		page = el.href.split(':').getLast();
		$('page').value = page;
		this.updateResults();
	},

	updateResults: function(event) {
		new Ajax($('filter-form').action, {
			'update': 'ajax-content',
			'data': this.getCond(event),
			'onComplete': this.updateResultsComplete.bind(this)
		}).request();
	},

	updateResultsComplete: function(resp) {
		// TODO: re-init state_id field (search form)
		Lightbox.init() ;
		this.initialize();
	},

	getCond: function(event) {
		cond = $('search-form').toQueryString();
		cond += '&' + $('filter-form').toQueryString();

		return cond;
	}
});

function initFakeInputs() {
	$ES('.fake').each(function(el) {
		el.addEvent('change', fakeInputChange.bind(el));
	});
}

function fakeInputChange(e) {
	if ($('Real' + this.id)) {
		$('Real' + this.id).value = this.value;
	}
}

window.addEvent('domready', function() {
	initFakeInputs();
	new SearchForm();
	new SearchController();
});

function updateHidden(e) {
	el = new Event(e).target;
	hidden = $(el.id);
	hidden.value = el.value;
}

function l(data) {
	if (window.console) {
		console.log(data);
	} else {
		alert(data);
	}
}

