function makeSublist(parent,child,isSubselectOptional,childVal)
{
	$("body").append("<select style='display:none' id='"+parent+child+"'></select>");
	$('#'+parent+child).html($("#"+child+" option"));

	var parentValue = $('#'+parent).attr('value');
	$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());

	childVal = (typeof childVal == "undefined")? "" : childVal ;
	$("#"+child).val(childVal).attr('selected','selected');
	$('#'+child).prepend("<option value='' selected='selected'>In any town</option>");

	$('#'+parent).change(function(){
		var parentValue = $('#'+parent).attr('value');
		$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
		if(isSubselectOptional) $('#'+child).prepend("<option value='' selected='selected'>In any town</option>");
		$('#'+child).trigger("change");
		$('#'+child).focus();
	});
}


var gst = {};

gst = {
	basePath: '',

	init: function() {
		// Search form
		if ( $('#keys').val() == 'Keywords' ) { $('#keys').addClass('blur'); }

		$('#search').submit(function() {
			if ( $('#keys').val() == 'Keywords' ) { $('#keys').val(''); }
		});

		$('#keys').focus(function() {
			if ( $(this).val() == 'Keywords' ) { $(this).val(''); }

			$(this).removeClass('blur').select();
		});

		$('#keys').blur(function() {
			if ( !$(this).val() || $(this).val() == 'Keywords' ) { $(this).addClass('blur').val('Keywords'); }
		});

		$('#member-search button').click(function() {
			if ( $('#member-search-type').val() ) {
				location = gst.basePath + 'list/' + $('#member-search-type').val() + '/' + ( $('#member-search-town').val() ? $('#member-search-town').val() : $('#member-search-region').val() );
			}
			else
			{
				alert('Please select what you want to search for.');
			}
			return false;
		});
	}
};

$(function() {
	gst.init();
	makeSublist('member-search-region','member-search-town', true, '');
});
