$(function() {

	// Click a country 
	$("ul#submenu li a[id^='country-']").toggle(function() {
		
		// Hide all other populated lists
		$(this).parent('li').siblings('li').children('ul.populated').slideUp();
		
		// If this country already has a list of cities, then show it
		if($(this).siblings('ul').children('li').length > 0) {
			$(this).siblings('ul').slideDown();
		
		// Not yet populated, get the data
		} else {

			// Get UK from country-UK
			country = this.id.replace('country-', '')
		
			// Set a new list element. Eg, country-UK clicked sets new ul as cities-UK
			city_list = $('<ul/>').attr('id', 'cities-' + country).addClass('populated');
			
			populate_city_list(city_list, country, '<a/>');
			
			$(this).parent().append(city_list);
			
		}
		
		return false;
	
	// Click a second time, slide the cities list back up
	}, function(){
		$(this).siblings("ul li a[id^='city-']").slideUp();
	});
	
	$("ul#submenu li a[id^='country-']").bind('dblclick', function()
	{
		$(this).unbind('click');
		$(this).click();
	});
	
	// Populate activities
	$("ul#submenu li ul li a[id^='city-']").livequery('click', function() {
		
		// Set all other populated lists to be hidden
		$(this).parent('li').siblings('li').children('ul.populated').slideUp();
		
		// If this country already has a list of cities, then show it
		if($(this).siblings('ul').children('li').length > 0) {
			$(this).siblings('ul').slideDown();

		// Not yet populated, get the data
		} else {
		
			// Get 2342 from city-2342
			city = this.id.replace('city-', '');

			// Set a new list element. Eg, city-2342 clicked sets new ul as activities-2342
			activity_list = $('<ul/>').attr('id', 'activities-' + city).addClass('populated');
			
			populate_activities_list(activity_list, city, '<a/>');
			
			$(this).parent().append(activity_list);
			
		}
		
		return false;
	});
	
	$("ul#submenu li ul li a[id^='city-']").livequery('dblclick', function()
	{
		$(this).unbind('click');
		$(this).click();
	});
	
});
