$(document).ready(function()
{

	//$('#searchlist').hide();
	// each key press
	$('#search-location').keyup(function()
	{

		var criteria = $(this).val();

		if(criteria.length > 2)
		{

			criteria = criteria.replace(/[^\w\d\s ]/g, '');
			criteria = criteria.replace(/(\s)/g, '%20');

			$('#searchlist').load("/ajax/locality?locale=" + criteria);
/*
			if (($('#searchlist li').length) >= 1){
				$('#searchlist').slideDown('1000');
			} else {
			$('#searchlist').slideUp('1000');
			}
*/
		} else {
//			$('#searchlist').slideUp('1000');
		}

	});
/*
	// check to see if they have moused out of the select and the suburb list
	$('#searchlist').bind('mouseenter', function()
	{
	// don't initiate unless they've mouseovered first
	}).bind('mouseleave', function()
	{
			$('#searchlist').slideUp('5000');
			if (($('#search-location').val().length) < 1)
			{
				$('#search-location').val($('#search-location').attr('placeholder'));
			}
	});
*/
/*
	// and since we are mouseout detecting, we need to see if they've left anything in the box and not clicked it
	$('#search-location').bind('mouseenter', function()
	{

		if ($('#search-location').val())
		{
			if (($('#searchlist li').length) >= 1)
			{
				$('#searchlist').slideDown('1000');
			}
			else
			{
				$('#searchlist').slideUp('1000');
			}
		}

	});
*/
	$('input.autocomplete').each(function()
	{

		var container = $(this).parent();
		var initialText = 'Start typing your ' + container.find('label').text() + '.';

		container.append('<div class="popup">' + initialText + '</div>');

		$(this).keyup(function()
		{

			var criteria = $(this).val();

			if(criteria.length)
			{

				criteria = criteria.replace(/[^\w\d\s ]/g, '').replace(/(\s)/g, '%20');

				container.find('.popup').load($(this).attr('autocompleteurl') + criteria);

			}
			else
			{
				container.find('.popup').text(initialText);
			}

		});

	});

});



function selectAgency(li, value)
{

	li = $(li);

	var input = li.parent().parent().parent().find('input.autocomplete');

	input.val(li.text());

	$('[name=' + input.attr('target') + ']').val(value);

};



function removesub(what, suburb)
{

	// loop through the select for anything of this value
	$('#selected-suburbs option').each(function()
	{

	  if ($(this).val() == what)
	  {
			// if we find it, remove it				
			$(this).remove();
	  }

	});

	// fade out the visible suburb
	$(suburb).fadeOut();

};



function fill(what, suburb)
{

	// make it so we can't select the same suburb twice in each string input
	//$(suburb).fadeOut();
	$(suburb).remove();

	var insert = true;

	// don't add a suburb that's already been added
	$('#selected-suburbs option').each(function()
	{
	  if ($(this).val() == what)
		{
			insert = false;
	  }
	});

	// if suburb is unique
	if (insert)
	{

	  $('#selected-suburbs').append(
			// append it to the hidden list
			$('<option selected="selected"></option').val(what).html(what)
		);

	  // add the visible suburb
	  $('#display-suburbs').html($('#display-suburbs').html()
			+ "<span class=\"sublist\" onclick=\"removesub('" + what + "',this);\">"
			+ what + " <em>x</em></span>");

	  // clear the input	
	  $('#search-location').val('');	

	}

};
