$(function()
{
	/* Get a json'ed Session.SearchParams
	 * This eliminates the need for redundant and sloppy cfml code on Quick_Search.cfm
	 */
/*	$.getJSON(
		'/tmp3.cfm', 		//url
		'form_key=sp_json', //data
		function(data) {	//callback
			// Collect the names of input fields. This could be avoided but 'locations' and 'buildingtype' elements needLOCAL.special treatment
			// since their values often happen to be lists.
			var e = $('div.QSearch').find('input:checkbox,input:radio').map(function() { return $(this).attr('name'); } ).get();
			var a = data['locations'].split(',');
			var b = data['buildingtype'].split(',');
			var c = $('div.QSearch').find('input:text').map(function() { return $(this).attr('name'); } ).get();
			// For every input field name, check if the structure contains a corresponding value; if so then set that field as 'checked'
			for ( var i in e ) { if ( v=data[e[i]] ) { $('[name='+e[i]+'][value='+v+']').attr('checked', 'checked'); } }
			// Deal with locations and building types.
			for ( var i in a ) { $('input:checkbox[value='+a[i]+'][name=locations]').attr('checked', 'checked'); }
			for ( var i in b ) { $('input:checkbox[value='+b[i]+'][name=buildingtype]').attr('checked', 'checked'); }
			// Handling text fields. EDIT: let coldfusion handling it for now; the following method doesn't appear to be IE8-friendly
			for ( var i in c ) { $('input:text[name='+c[i]+']').val(data[c[i]]); }
			// The labels deserve attention too.
			for ( var i in data) {
				if ( l=data[i] ) {
					switch(i) {
						case 'locations': 		$('span#locations').text('(expand)'); break;
						case 'buildingtype': 	$('span#buildingtype').text('(expand)'); break;
						case 'pets': 			$('span#amenities').text('pets...'); break;
						case 'bldgdoorman':		$('span#amenities').text('doorman...'); break;
						case 'bldgpool':		$('span#amenities').text('pool...'); break;
						case 'bldgelevator':	$('span#amenities').text('elevator...'); break;
						case 'aptfee':			$('span#aptfee').text('(expand)'); break;
						case 'aptnumbdrm':		$('span#aptnumbdrm').text(l); break;
						case 'aptnumbathrm':	$('span#aptnumbathrm').text(l); break;
					}
				} else { $('span#'+i).text('any'); }
			}
		}
	);*/
		
	// expands/hides elemenets of the search form
	$('div.qs_toggle').toggle(
		function() { $(this).next().slideDown('slow').addClass('clicked'); },
		function() { $(this).next().slideUp('slow').removeClass('clicked');	}
	);

	// Clear input fields
	$('input:reset').click( function() {
		$('input:checkbox').removeAttr('checked');
		$('input:radio').removeAttr('checked');
		$('input:text').removeAttr('value');
	});
	
	// New Search Button
	$('input#new_search').click(function() {
		var a = window.location.href;
		if (a.match( /\brental\b/i ) && !a.match( /furnished/i ))
			window.location.href = '/rentals';
		else if (a.match( /\bsale\b/i ))
			window.location.href = '/sale';
		else if (a.match( /furnished/i ))
			window.location.href = '/furnished-rentals';
		else if (a.match( /\bbuilding\b/i ))
			window.location.href = '/buildings';
	});
	
	// Mask
	//$('#maxprice,#minprice').mask('$99,999');
	// Edit masked values on submit.
	$('form#quick_search').submit(function(){ $('#maxprice,#minprice').val(function(i,v){return v.replace(/^\$0*?([1-9][0-9]*),([0-9]+)$/, '$1$2')})});
	
	// Form validation
/*	$('form#quick_search').validate({
		debug: true,
		rules: {
			minprice: { required: '#maxprice:filled', number: true },
			maxprice: { required: '#minprice:filled', number: true, min: $('#minprice').val() },
			title: { required: function(){ var a=false;$('#ss_title').click(function(){a=true});return a; } },
			propertyid: { digits: true },
			a_date_from: { date: true },
			a_date_to: { date: true },
			a_size_min: { digits: true, required: '#a_size_max:filled', max: $('#a_size_max').val() },
			a_size_max: { digits: true, required: '#a_size_min:filled', min: $('#a_size_min').val() }
		},
		errorPlacement: function(){ return false },
		highlight: function(element, errorClass){
			$(element).css('border','2px solid red')}
	});*/
	// Datepicker
	$('input#a_date_from,input#a_date_to').datepicker({ dateFormat: 'yy-mm-dd' });
	// Save search
	$('input#save_search').click(function(){
		if (title = $('#ss_title').val()) {
			$('#ss_title').css('border', '');
			$.ajax({
				url: '/ajax_handler/ajax_handler.cfm',
				type: 'POST',
				data: 'title='+title+'&form_key=save_search',
				success: function(data){ 
					if(data) 
						$('div#save_search table tr:last').after('<tr ><td colspan="2" style="font-size: 11px; color: #F56FAE; ">Your search has been'+data+'</td></tr>'); },
				error: function(a,b,c){ alert(b+','+c) }
			});
		} else { $('#ss_title').css('border','2px solid red') }
	});
});