$(document).ready(function()
{
	var current_selection = '';
	var first_load = true;

	$.fn.addItems = function(data, header)
	{
		return this.each(function()
		{
			var list = $(this);

			if (header != '' ) {
				$(this).append('<option value="">- select ' + header + ' -</option>');
			}

			$.each(data, function(index, text)
			{
				list.append('<option value="' + index + '">' + text +  '</option>');
			});
		});
	};

	$("#state").change(function(event)
	{

		$("#county").empty();
		$("#cities").empty();
		$("#zipcodes").empty();

		if ($(this).val() == '') {
		}
		else {
			// get county information by ajax
			$.getJSON("/city/county/state/" + $(this).val(), null, function(data){
				$("#county").addItems(data, 'county');
				if( first_load )
				{
					first_load = false;
					$("#county").val("214");
					$("#county").change();
				}
			});
		}
	});


	$("#county").change(function(event)
	{
		$("#cities").empty();
		$("#zipcodes").empty();
		if ($(this).val() == '') {
		}
		else {
			// get county information by ajax
			$.getJSON("city/cityzip/county/" + $(this).val(), null, function(data){
				$("#cities").addItems(data.cities, '');
				$("#zipcodes").addItems(data.zipcodes, '');
			});
		}
	});


	$("#cities").focus(function(event)
	{
		current_selection = 'cities';
	});
	

	$("#zipcodes").focus(function(event)
	{
		current_selection = 'zipcodes';
	});
	

	$("#add").click(function(event)
	{
		if( current_selection != '' )
		{
			$('#selection').append($('#' + current_selection + ' option:selected').clone());
			$('#' + current_selection).blur();
			current_selection = '';
		}
	});


	$("#del").click(function(event)
	{
		$('#selection option:selected').remove();
	});


	$("#go").click(function(event)
	{
		$("#search_multiple_form").addClass('hidden');
		if( $("#selection").children().length == 0 )
		{
			$("#form-element-city").removeClass('hidden');
			$("#multi_selection").addClass('hidden');
			$("#search_multiples").text('search muiltple locations');
		}
		else
		{
			$("#form-element-city").addClass('hidden');
			var var_html = '';
			var multi_city = '';
			$("#selection").children().each(function(i, a)
			{
				var_html += $(this).text() + "<br />";
				multi_city += $(this).text() + ':' + $(this).val() + ';';
			});
			$("#multi_selection").html(var_html);
			$("#multi_selection").removeClass('hidden');
			$("#multi_city").val(multi_city);
			$("#search_multiples").text('modify locations');
		}
	});


	$("#search_multiples").click(function(event)
	{
		$("#search_multiple_form").removeClass('hidden');
		return false;
	});

	$("#city").jSuggest(
	{
		url: "city",
		type: "GET",
		data: "c" ,
		autoChange: true,
//		loadingImg: 'img/zurple/icon_ajax-loader.gif',
		loadingImg: '',
		minchar: 1,
		delay: 100
	});

	$('#address').focus(function(event)
	{
		if ($('#city').val() == '') {
			$("#address").jSuggest({
				url: "city/address/city/0/",
				type: "GET",
				data: "a",
				autoChange: true,
				loadingImg: '',
				minchar: 3,
				delay: 100
			});
		}
		else {
			$("#address").jSuggest({
				url: "city/address/city/" + $('#city').val() + '/',
				type: "GET",
				data: "a",
				autoChange: true,
				loadingImg: '',
				minchar: 1,
				delay: 100
			});
		}
	});

	$("#close_multi_form").click(function(event)
	{
		$("#go").click();
		return false;
	});

	if( $("#multi_city").val() != '' )
	{

		if ($("#multi_city").val() != undefined) {
			var items = $("#multi_city").val().split(';');
			$.each(items, function(){
				if (this != '') {
					var elems = this.split(':');
					$('#selection').append('<option value="' + elems[1] + '">' + elems[0] + '</option>');
				}
			});
		}

		$("#go").click();
		if( location.pathname == '/search/m' )
		{
			$("#search_multiples").click();
		}
	}

	// select CA as default
	$("#state").val('CA');
//  $("#state").change();
});