
function property_type_change(e)
{
	var select, selected;
	
	select = $(e.currentTarget);
	selected = select.val();
	select[0].blur();
	
	if (empty(selected)) return;
	
	search_show_applicable(selected);
	search_show_options(selected);
}

function search_show_applicable(selected)
{
	var rows, row, i, na;
	
	if (typeof(g_na) == 'undefined') return;
	na = g_na[selected];
	rows = $('.search_tbody tr');
	for (i = 0; i < rows.length; ++i)
	{
		row = $(rows[i]);
		if (in_array(na, row.attr('id').replace(/_row$/, '')))
		{
			row.hide();
		}
		else
		{
			row.show();
		}
	}
}

function search_show_options(selected)
{
	search_set_option_ml(selected, 'locations');
	search_set_option_ml(selected, 'features');
}

function search_set_option_ml(selected, type)
{
	var options, option, option_name, i, row_index, cell_index, cell, row_count, cell_count, count, ml_cell, ml_checked;
	
	if (typeof(window['g_'+type]) == 'undefined') return;
	options = window['g_'+type][selected];
	
	row_count = $('#'+type+'_tbody tr').length;
	cell_count = $('#'+type+'_tbody tr:first td').length;
	count = row_count * cell_count;
	
	for (i = 0; i < count; ++i)
	{
		row_index = (i % row_count);
		cell_index = Math.floor(i / row_count);
		
		cell = $('#'+type+'_tbody tr:eq('+row_index+') td:eq('+cell_index+')');
		option = (i < options.length) ? options[i] : '';
		if (option == 'Ocean access by boat') continue;
		option_name = simple_text(option);
		ml_checked = (g_checked_options && g_checked_options[option_name]) ? ' checked' : '';
		ml_cell = (empty(option)) ? '' : '<input type="checkbox" name="'+option_name+'" value="'+option+'"'+ml_checked+' /> '+option;
		cell.html(ml_cell);
	}
}

function search_toggle_visible()
{
	var a;
	
	a = $('a#show_search');
	if (a.text().indexOf('Show') != -1)
	{
		$('#search_form').show();
		a.text('Hide Search Options');
		$dom('#show_search').blur();
	}
	else
	{
		$('#search_form').hide();
		a.text('Show Search Options');
		$dom('#show_search').blur();
	}
}

function search_prev_next_click(e)
{
	var a, options, i, option;
	
	a = $(e.currentTarget);
	options = $('select#top_data_start_select option');
	for (i = 0; i < options.length; ++i)
	{
		option = $(options[i]);
		if (option.attr('selected'))
		{
			search_ignore_unused_fields();
			window.location = query_replace('data_start', $(options[((a.attr('id').indexOf('prev_link') != -1) ? (i - 1) : (i + 1))]).val());
		}
	}
}

function search_set_data_start(e)
{
	search_ignore_unused_fields();
	window.location = query_replace('data_start', $(e.currentTarget).val());
}

function search_set_sort(e)
{
	var q;
	
	search_ignore_unused_fields();
	q = query_replace('sort', $(e.currentTarget).val());
	q = query_replace('data_start', 0, q);
	window.location = q;
}

function search_show_advanced()
{
	$('.search_tbody tr.advanced').show();
	$('.search_tbody tr.basic_submit_row').hide();
	$f('advanced', 1);
}

function search_hide_advanced()
{
	$('.search_tbody tr.advanced').hide();
	$('.search_tbody tr.basic_submit_row').show();
	$f('advanced', 0);
}

function search_init()
{
	$('#property_type').change(function(event) { property_type_change(event); });
	$('#search_form input[type="text"]').addClass('text_input');
	$('#search_form input[type="submit"]').click(function() { search_submit(); });
	search_show_applicable($f('property_type'));
	search_show_options($f('property_type'));
	
	// if results are on the page, hide the search form to start off
	if (!$empty('#search_results'))
	{
		$('div#search_form').hide();
		$('a#show_search').click(function() { search_toggle_visible(); return false; });
		$('div.prev_next_link a').click(function(event) { search_prev_next_click(event); return false; });
		$('div.slice_select select').change(function(e){search_set_data_start(e);});
		$('div.sort_select select').change(function(e){search_set_sort(e);});
	}
	$('#wrapper').focus();
}

$(document).ready(search_init);
