var ExpList = Class.create();
ExpList.prototype = {
	initialize : function (cntId, regId, searchOnly) {

		if (searchOnly !== true) {

			this._map = new ExpMap(
				'gmap',
				{
					mode : 'move'
				}
			);

			this._markPoints();
		}
		this._regId = regId;
		this._cntId = cntId;

		this._getCoutries();
		this._getRegions(cntId);
	},

	_markPoints : function() {

		$$('input.hidden-map-params').each(function(el) {

				eval('var pointParams = ' + el.value);

				if (pointParams.mapZoom > 0) {

					this._map.addListPoint(

						pointParams.mapPosX,
						pointParams.mapPosY
					);
				}
			}.bind(this)
		);
	},

	changeCountry : function(selectObj) {

		var cntId = selectObj.options[selectObj.selectedIndex].value;

		this._getRegions(cntId);
	},

	_getCoutries : function() {

		var url = '/houses/getContries';
		new Ajax.Request(
			url,
			{
				onComplete : function(req) {

					eval('var ret=' + req.responseText);

					var ctnId = 0;

					var retLen = ret.length;

					var innerHTML = '';
					var selected = '';

						innerHTML += '<select name="country" onchange="ExpListObj.changeCountry(this);" class="formhouse2" style="margin-top:0;">';
					innerHTML += '<option value="0">Choose</option>';

						for (var i = 0 ; i < retLen ; i++) {

						if (ret[i].CNT_ID == this._cntId) {

							selected = ' selected';
						} else {

							selected = '';
						}

								innerHTML += '<option value="' + ret[i].CNT_ID + '" ' + selected + '>' + ret[i].CNT_NAME + '</option>';
						}
						innerHTML += '</select>';

						 $('country-selector').update(innerHTML);
				}.bind(this)
			}
		);

		return false;
	},

	_getRegions : function(cntId) {

		if (cntId == 0) {

			var innerHTML = '';

			innerHTML += '<select name="region" class="formhouse2" disabled>';
					innerHTML += '<option value="">Choose country</option>';
				innerHTML += '</select>';

				$('region-selector').update(innerHTML);

		} else {
			var url = '/houses/getRegions/cntId/' + cntId;
			new Ajax.Request(
				url,
				{
					onComplete : function(req) {

						eval('var ret=' + req.responseText);

						var retLen = ret.length;

						var innerHTML = '';
						var selected = '';

							innerHTML += '<select name="region" class="formhouse2">';
							innerHTML += '<option value="0">Choose</option>';

							for (var i = 0 ; i < retLen ; i++) {

									if (ret[i].REG_ID == this._regId) {

								selected = ' selected';
							} else {

								selected = '';
							}

									innerHTML += '<option value="' + ret[i].REG_ID + '" ' + selected + '>' + ret[i].REG_NAME + '</option>';
							}
							innerHTML += '</select>';

							 $('region-selector').update(innerHTML);
					}.bind(this)
				}
			);
		}
		return false;
	}
}

var ExpSortBy = Class.create();
ExpSortBy.prototype = {
	initialize: function(element) {
		this.element = element;
		Event.observe(this.element , 'change' , this.changeSort.bindAsEventListener(this));
	} ,
	
	changeSort: function(event) {
		Event.stop(event);
		if(! document.location.pathname.split('/').include('sort')) {
			if (!document.location.pathname.split('/').include('index')) {
				document.location = (document.location.pathname + "/index" + Form.Element.getValue(this.element)).replace(/\/{2,}/ , "/");
	  	}
			else {
				document.location = (document.location.pathname + Form.Element.getValue(this.element)).replace(/\/{2,}/ , "/");
			}
		}
		else {
			var address = document.location.pathname.split('/');
			address[address.indexOf('sort') + 1] = Form.Element.getValue(this.element).split('/').last()
			document.location = (address.join('/')).replace(/\/{2,}/ , "/");
		}
	}
}

Event.observe(window , 'load' , function(event) {
	if($('sortBy')) {
		new ExpSortBy($('sortBy'));
	}
});