var SearchPage = Class.create( {
	initialize : function() {
		if ($('search-form')) {
			this.attachAutoCompleteEventHandler();
			
			var autocomplete = $('autocomplete');
			
			autocomplete.onfocus = function(){
				if(this.value == 'Enter suburb, postcode or street address'){
					this.value = '';
				}else{
					var choices = $('autocomplete_choices');
					if(!choices.visible() && autocomplete.value.length > 2){
						if(choices.down().down()){
							choices.show();
						}
					}
				}
			}
			autocomplete.onblur = function(){
				if(this.value == ''){ 
					this.value = 'Enter suburb, postcode or street address';
				}
			}
		}
		
	},

	attachAutoCompleteEventHandler : function() {
		new Ajax.Autocompleter("autocomplete", "autocomplete_choices",
				"/ajax/suburb/show", {
					method :"get",
					paramName :"suburbOrPostcode",
					minChars :2,
					indicator :'indicator1'
				});
	},

	initialiseStyles : function() {
		if (!NiftyCheck())
			return;
		Rounded("form#search-form", "#FFF", "#9DD4FF");
	}
})


var SearchOption = {}

SearchOption.filter = function(sliderType, formField, replacePoint) {
	var slider = sliderType, form_field = formField, replace_point = replacePoint;
	var sliderValue = ListingUtils.GetValue(form_field.value, 0);
	replace_point.update(FilterUtil.GetFormattedValue(sliderValue));
	new Control.Slider(slider.select('.handle'), slider, {
		range :$R(0, 5),
		sliderValue : [ sliderValue ],
		restricted :true,
		values : [ 0, 1, 2, 3, 4, 5 ],
		onSlide : function(value) {
			replace_point.update("(" + value + "+)");
		},
		onChange : function(value) {
			form_field.setValue(value);
			replace_point.update(FilterUtil.GetFormattedValue(value));
		}
	});
}

SearchOption.filterByBedrooms = function() {
	SearchOption.filter($('bedrooms-slider'), $('bedrooms-num'),
			$('bedrooms-replace'));
}

SearchOption.filterByBathrooms = function() {
	SearchOption.filter($('bathrooms-slider'), $('bathrooms-num'),
			$('bathrooms-replace'));
}

SearchOption.filterByCarparks = function() {
	SearchOption.filter($('carparks-slider'), $('carparks-num'),
			$('carparks-replace'));
}

SearchOption.filterByLandSize = function() {
	var slider = $('landsize-slider'), form_field = $('landsize-num'), replace_point = $('landsize-replace');
	var sliderValue = ListingUtils.GetValue(form_field.value, 0);
	replace_point.update(FilterUtil.GetFormattedLandSize(sliderValue));

	new Control.Slider(slider.select('.handle'), slider, {
		range :$R(0, 2000),
		sliderValue : [ sliderValue ],
		restricted :true,
		values : [ 0, 200, 400, 600, 800, 1000, 1200, 1400, 1600, 1800, 2000 ],
		onSlide : function(value) {
			replace_point.update(FilterUtil.GetFormattedLandSize(value));
		},
		onChange : function(value) {
			form_field.setValue(value);
		}
	});
}

SearchOption.filterByPriceRange = function(listingType) {

	var slider = $('price_slider_' + listingType), price_from = $('price-from-' + listingType), price_to = $('price-to-' + listingType), price_range_replace = $('price-range-replace-' + listingType);
	var startPrice;
	var endPrice;
	var values;
	var range;
	var suffix = "";
	if (listingType == "rent") {
		startPrice = ListingUtils.GetValue(price_from.value, 0);
		endPrice = ListingUtils.GetValue(price_to.value, 2000);
		values = [ 0, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100,
				1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000 ];
		range = $R(0, 2000);
		suffix = " pr/wk"
	} else {
		startPrice = ListingUtils.GetValue(price_from.value, 0);
		endPrice = ListingUtils.GetValue(price_to.value, 3000000);
		values = [ 0, 100000, 150000, 200000, 250000, 300000, 350000, 400000,
				450000, 500000, 550000, 600000, 650000, 700000, 750000, 800000,
				850000, 900000, 950000, 1000000, 2000000, 3000000 ];
		range = $R(0, 3000000);
	}

	price_range_replace.update(FilterUtil.GetFormattedPrice(startPrice,
			endPrice)
			+ suffix);

	new Control.Slider(slider.select('.handle'), slider, {
		range :range,
		sliderValue : [ startPrice, endPrice ],
		restricted :true,
		values :values,
		onSlide : function(values) {
			price_range_replace.update(FilterUtil.GetFormattedPrice(values[0],
					values[1])
					+ suffix);
		},
		onChange : function(values) {
			price_from.setValue(Math.round(values[0]));
			price_to.setValue(Math.round(values[1]));
		}
	});
}

SearchOption.saveSearchOptions = function() {
	var priceFromBuy = ($F('price-from-buy') == "0"? null : $F('price-from-buy')); 
	var priceToBuy = ($F('price-to-buy')  == "3000000"? null : $F('price-to-buy'));
	var priceFromRent = ($F('price-from-rent')== "0"? null :$F('price-from-rent'));
	var priceToRent = ($F('price-to-rent') == "2000"? null : $F('price-to-rent'));
	var sortBy = $F('select-sortby');
	var landSize = ($F('landsize-num') == "0"? null: $F('landsize-num'));
	var bedRoomCount = ($F('bedrooms-num') == "0"? null: $F('bedrooms-num'));
	var carParkCount = ($F('carparks-num') == "0"? null: $F('carparks-num'));
	var bathRoomCount = ($F('bathrooms-num') == "0"? null: $F('bathrooms-num'));
	
	var houses = $('house').checked;
	var townhouses= $('townhouse').checked;
	var units= $('unit').checked;
	var apartments= $('apartment').checked;
	var land= $('land').checked;

	new Ajax.Updater('rvrp','/ajax/user/search-options/save', {
		parameters : {
			"priceFromBuy" :priceFromBuy,
			"priceToBuy" :priceToBuy,
			"priceFromRent" :priceFromRent,
			"priceToRent" :priceToRent,
			"sortBy" :sortBy,
			"landSize" :landSize,
			"bedRoomCount" :bedRoomCount,
			"carParkCount" :carParkCount,
			"bathRoomCount" :bathRoomCount,
			"houses":houses,
			"townhouses":townhouses,
			"units":units,
			"apartments":apartments,
			"land":land
		},
		asynchronous :true,
		evalScripts :true,
		method :'post',
		onLoading : function() {
			PageStatusManager.registerStatus(" ...saving search options");
		},
		onComplete : function(response) {
			$("indicator1").hide();
			PageStatusManager.deregisterStatus();
		}
	});
}

SearchOption.toggleOptionBox = function(){
	var searchOptions = $('search-options');
	searchOptions.toggleClassName('hide');
	
	if(!searchOptions.hasClassName('hide')){
		SearchOption.filterByBedrooms();
		SearchOption.filterByBathrooms();
		SearchOption.filterByCarparks();
		SearchOption.filterByLandSize();
		SearchOption.filterByPriceRange("buy");
		SearchOption.filterByPriceRange("rent");
	}
}

function init() {
	new SearchPage();
}
Event.observe(window, 'load', init);

