jq(function() {
	// autocomplete search
	jq('#auto_search').blur(function() {
		set_categories();
	}).autocomplete('/housing/auto_search', { minChars: 2, max: 150 }).result(function() {
		set_categories();
	});	
	set_categories();
	
	// directory pages
	// toggle counties
	jq('dt a').click(function() {
		jq(this).parents('dl').children('dd').toggle();
		return false;
	});
	// toggle states
	jq('#states_toggle').click(function() {
		jq('#states, #states_toggle').toggle();
		return false;
	});
});

function set_categories() {
	var search = jq('#auto_search').val();
	if (search == "" || search == null) {
		// jq('.type label').fadeTo("fast", 0.33);
		jq('.type label').css({color: '#ccc'});
		jq('.type a').hide();
		jq('.type input').attr('disabled', true);
		jq('(.type input):checked').attr('checked', false);
	} else {
		// Need to check for real if it's one of our area's
		area_finder(search);
	}
}

function area_finder(term) {
	// zipcode
	if ( term.match(/^\d{5}/) ) {
		if ( jq.inArray(term, $housing_zipcodes) > -1 )
			local();
		else
			national();
	// county
	} else if ( term.indexOf(' County') > -1 ) {
		local();
	// state
	} else {
		term = term.split(', ')[1];
		var regex = new RegExp('(' + $housing_states.join('|') + ')');
		if ( term && term.toUpperCase().match(regex) )
			local();
		else
			national();
	}
}

function local() {
	jq('.type label').css({color: '#000'});
	jq('.type a, .type').show();
	jq('#nat_cats').hide();
	jq('.type input').attr('disabled', false);
}

function national() {
	jq('.type a, .type, .options').hide();
	jq('#nat_cats').show();
}

// ============================================================================================= //
// ========================================= PROTOTYPE ========================================= //

document.observe('dom:loaded', reset_values);

function reset_values(){
	if ( location.pathname.endsWith('/search') ) {
		var params = location.search.toQueryParams();
		var state = params['housing_listing[state]'], county = params['housing_listing[county]'], city = params['housing_listing[city]'];
		
		if (params['housing_types[]']){
			params['housing_types[]'].each(function(ht) {
				ht = get_housing_type(ht);
				$(ht).toggleClassName('selected');
				$(ht + '_options').show();
			});
		}

		if (state)
			observe_state_on_error(controller, state, county, city);	
	}
}

function observe_state_on_error(controller, state, county, city) {
	new Ajax.Updater('county', '/' + controller + '/update_counties', {
		parameters: { state: state, authenticity_token: AUTH_TOKEN },
		onComplete: function(){
		  updateFlash(state);
		
			if (county){			  
				new Ajax.Updater('city', '/' + controller + '/update_cities', {
					parameters: { county: county, authenticity_token: AUTH_TOKEN },
					onComplete: function(){
					  $('county').value = county;
						if (city)
							$('city').value = city;
					}
				});
			}
		}
	});
}

function get_housing_type(ht) {
	switch (ht) {
	  case 'active_adult':
	    return 'aa';
		case 'continuing_care':
			return 'ccrc';
		case 'independent_living':
			return 'il';
		case 'assisted_living':
			return 'al';
		case 'nursing_homes':
			return 'nh';
	}
}
