QuickSearch = {
	URL_Root : "pre-owned-home-search?", 
	Events : {},
	
	StartValues : {
		city: "e.g. \"Dallas\" or \"Frisco\"",
		price_min:"Minimum",
		price_max:"Maximum"
	},
	
	Init: function(){
		var price_min = $('#price_min');
		var price_max = $('#price_max');
		var city = $('#city');
		
		if(price_min && price_max && city){
			city.val(this.StartValues.city);
			price_min.val(this.StartValues.price_min);
			price_max.val(this.StartValues.price_max);
						
			price_min.one('click',function(){ QuickSearch.ClearAndFocus(this) });
			price_max.one('click',function(){ QuickSearch.ClearAndFocus(this) });
			city.one('click',function(){ QuickSearch.ClearAndFocus(this) });
						
			price_min.keydown(this.CheckSearch);
			price_max.keydown( this.CheckSearch);
			city.keydown(this.CheckSearch);
		}
	},
	
	CheckSearch : function(k){
		if(k.keyCode == 13)
			QuickSearch.Search();
	},
	
	ClearAndFocus: function(origin){
		var price_min = $('#price_min');
		var price_max = $('#price_max');
		var city = $('#city');
		
		price_min.val('');
		price_min.css('color','black');
		price_max.val('');
		price_max.css('color','black');
		city.val('');
		city.css('color','black');
		
		origin.focus();
	},
		
	Search : function(showAdvanced){
		
		var price_min = $('#price_min');
		var price_max = $('#price_max');
		var bed_min = $('#bed_min');
		var bath_min = $('#bath_min');
		var city = $('#city');
		var sqft_min = $('#sqft_min');
		var property_types = $('#property_types');
		var communities = $('#communities');
		var mls_numbers = $('#mls_numbers');
		var zipcode = $('#zipcode');
		
		if(city.val() == this.StartValues.city)
			city.val("");
		if(price_min.val() == this.StartValues.price_min)
			price_min.val("");
		if(price_max.val() == this.StartValues.price_max)
			price_max.val("");
		
		var PropertyTypes = "";
		var URLType = "";
		
		var queryString = 
			(property_types.length == 0 || property_types.val() == "" ? "" : "&PropertyTypes=" + property_types.val() ) + 
			(mls_numbers.length == 0 || mls_numbers.val() == "" ? "" : "&MlsNumbers=" + mls_numbers.val() ) + 
			(price_min.length == 0 || price_min.val() == "" ? "" : "&MinPrice=" + price_min.val() ) + 
			(price_max.length == 0 || price_max.val() == "" ? "" : "&MaxPrice=" + price_max.val() ) + 
			(sqft_min.length == 0 || sqft_min.val() == "0" || sqft_min.val() == "" ? "" : "&MinImprovedSqFt=" + encodeURIComponent(sqft_min.val())) + 
			(bed_min.length == 0 || bed_min.val() == "0" ? "" : "&MinBeds=" + bed_min.val() ) + 
			(bath_min.length == 0 || bath_min.val() == "0" ? "" : "&MinBaths=" + bath_min.val() ) + 
			(city.length == 0 || city.val() == "" ? "" : "&Cities=" + encodeURIComponent(city.val())) + 
			(zipcode.length == 0 || zipcode.val() == "" ? "" : "&ZipCodes=" + encodeURIComponent(zipcode.val())) + 
			(communities.length == 0 || communities.val() == "" ? "" : "&Communities=" + encodeURIComponent(communities.val()));
		
		FinalUrl = "";
		if (typeof showAdvanced != 'undefined' && showAdvanced) {
			FinalUrl = "/" + this.URL_Root + '&ShowAdvanced';
		} else {
			FinalUrl = "/" + this.URL_Root + queryString + "&PerformSearch&";
		}
		
		if(typeof window.navigate != "undefined"){
			window.navigate(FinalUrl);
		} else {
			location.href = FinalUrl;
		}
	},
	
	Advanced : function(){
		this.Search(true);
	}
}

//Event.observe(window,'load', QuickSearch.Init.bind(QuickSearch));
$(window).load(function(){
	QuickSearch.Init();
})