function prepare_dropdown(country_selector, city_selector) {

	if(!$(country_selector).val()) {
		$(city_selector).attr('disabled', 'disabled');
	}

	$(country_selector).change(function() {
		if($(this).val() != '') {
			$(city_selector).attr('disabled', 'disabled').empty();

		    $("<option/>").val('').text('-- Select --').appendTo(city_selector);

			populate_dropdown(city_selector, $(this).val());
		}
	});
}

function populate_dropdown(selector, country, selected) {

	$.getJSON("/index.php/geolocation/ajax/cities/" + country, function(data, status){

         if(status == 'success') {

			$.each(data, function(i, city) {
				option = $('<option/>').val(city.id).text(city.name);

				// If the current one is the supplied selected city id
				if(city.id == selected) option.attr("selected", "selected");
		        option.appendTo(selector);
			});

			// Re-enable dropdown
			$(selector).removeAttr('disabled');

	     } else {
	     	alert('There was an error retrieving this countries cities.');
	     }
    });
}

function populate_city_list(selector, country, wrap_tag) {

	$.getJSON("/index.php/geolocation/ajax/cities/" + country + "/menu", function(data, status){

         if(status == 'success') {

			$.each(data, function(i, city) {

				if(city.profile_count > 0) {
					li = $('<li/>');
					a = $('<a/>').text(city.name + ' (' + city.profile_count + ')').attr('id', 'city-' + city.id).attr('href', BASE_URL + 'profiles/browse/city/' + city.id + '.html');

					$(selector).append(li.append(a));
				}
			});

	     } else {
	     	alert('There was an error retrieving this countries cities.');
	     }
    });
}
