/*  Funcgtions for Listings Real Estate Site
 *  (c) Tim Nilson 2008
/*--------------------------------------------------------------------------*/

$(document).ready(function(){
	
	$('#CityAjax').autocomplete('/cities.js', {
		minChars: 3,
		matchContains: true,
		autoFill: false,
		max: 15,
		formatItem: function(data, i, n, value) {
			return value.split("|")[0];
		}
	}).result(function(event, item) {
		getSelectionId(item[1]);
	});

	$('#search_city_id').change(function() { submitSearchForm(); });
	$('#search_sale_rent').change(function() { submitSearchForm(); });
	$('#search_listing_type_id').change(function() { submitSearchForm(); });
	$('#search_bedrooms').change(function() { submitSearchForm(); });
	$('#search_bathrooms').change(function() { submitSearchForm(); });
	$('#search_age_in_days').change(function() { submitSearchForm(); });
	$('#search_source').change(function() { submitSearchForm(); });

	// remove the borders for listing row elements that have a 'featured' listing below them.
	$('div.row').each(function (i) {
		if ($(this).hasClass("featured")) {
			if (i != 0 && !$($('div.row')[i-1]).hasClass("featured")) {
				$('div.row')[i-1].style.border = 'none'
			}
		}
	});
	
});



function checkListingType(value) {
	switch(value) {
		case '0':
		  $('#rent').hide();
		  //Element.hide('rent');  
		  break
		case '1':
		  $('#rent').show();
		  //Element.show('rent');
		  break
	}
}

function checkPropertyType(value) {
	switch(value) {
		case '1':
		  // Element.show('property_size'); 
		  // Element.show('lot_size');   
		  $('#property_size').show();
		  $('#lot_size').show();
		  break
		case '2':
		  // Element.show('property_size'); 
		  // Element.hide('lot_size');   
		  $('#property_size').show();
		  $('#lot_size').hide();
		  break
		case '3':
	      $('#property_size').hide();
		  $('#lot_size').show();
		  // Element.hide('property_size'); 
		  // Element.show('lot_size');   
		  break
		case '4':
	 	  $('#property_size').show();
		  $('#lot_size').show();
		  // Element.show('property_size'); 
		  // Element.show('lot_size');   
		  break
	}
}

// Need this to make the autoloader work on the various pages: search and listing edit
function getCityHiddenId() {
	var obj;
	if ($('#search_city_id').length > 0) {
		obj = $('#search_city_id');
	} else if ($('#listing_city_id').length > 0) {
		obj = $('#listing_city_id')
	} else if ($('#broker_city_id').length > 0) {
		obj = $('#broker_city_id')
	}
	return obj;
}

function getSelectionId(value) {
	$('#li_x').show();
	getCityHiddenId().val(value).change();
}

function clearCityId() {
	$('#li_x').hide();
	$('#CityAjax').val('');
	getCityHiddenId().val('').change();
}

function show_loading() {
	$('#loading_notice').show();	
}

function hide_loading() {
	$('#loading_notice').hide();
}

function submitSearchForm() {
	// do not do the ajax submit if there is a 'search' submit button on the page (like on the home page)
	if ($('#search_submit').length == 0) {
		$('#listings_list').addClass('loading');
		var params = $('#search_form_values').serialize();
		show_loading();
	
		$.get('/searches/ajax_update/'+ $('#search_id').val(), params, function(data){
			$('#listings_list').removeClass('loading');
			hide_loading();
			$('#listings_list').html(data);
		});
	}
}

function sortSearchResults(s,o) {
	show_loading();
	var opt = {
		'o' : o,
		's' : s
	}
	$('#listings_list').addClass('loading');
	
	// pull the current location from the browser location
	var path = jQuery.url.attr("path");
	var pid = '/index';
	opt['response'] = 'js';
	
	if ($('#search_id').length > 0 ) {
		pid = '.js';
	}
	//console.info("PATH: "+ path + pid);
	$.get(path + pid, opt, function(data){
		$('#listings_list').removeClass('loading');
		hide_loading();
		$('#listings_list').html(data);
	});
}

