// jQuery script to handle dynamic location dropdowns

var printCountryDropdown = {}; // declare these function outside of jQuery so we can use it elsewhere.
var printStateDropdown = {};
var printCityDropdown = {};

jQuery(document).ready(function($){

	// print a new country dropdown with the given country selected.
	printCountryDropdown = function(country_name) {
		$('#country_dropdown').load('/ajax/printCountryDropdownByName.php?country_name=' + country_name, function() {
			$('#addCity').hide();
			bindChangeCountry();
			if (typeof(pageTracker) !== 'undefined') {
				pageTracker._trackPageview('/ajax/printCountryDropdownByName.php'); // track this AJAX call in google analytics
			}
		});
	};

	// print a new state dropdown with the given state selected.
	printStateDropdown = function(country_name, state_name) {
		$('#state_dropdown').load('/ajax/printStateDropdownByName.php?country_name=' + country_name + '&state_name=' + state_name, function() {
			$('#addCity').hide();
			bindChangeState();
			if (typeof(pageTracker) !== 'undefined') {
				pageTracker._trackPageview('/ajax/printStateDropdownByName.php'); // track this AJAX call in google analytics
			}
		});
	};

	// print a new city dropdown with the given city selected.
	printCityDropdown = function(country_name, state_name, city_name) {
		$('#city_dropdown').load('/ajax/printCityDropdownByName.php?country_name=' + country_name +	'&state_name=' + state_name + '&city_name=' + city_name, function() {
			$('#addCity').hide();
			if (typeof(pageTracker) !== 'undefined') {
				pageTracker._trackPageview('/ajax/printCityDropdownByName.php'); // track this AJAX call in google analytics
			}
		});
	};
	
	// when user changes the country dropdown, populate the state dropdown accordingly
	// also, disable city dropdown
	var bindChangeCountry = function() {
		$('#country_iso').change(function() {
			var country_iso = $(this).val(); // get selected country ISO
			$('#state_dropdown').load('/ajax/printStateDropdown.php?country_iso=' + country_iso, function() {
				if (country_iso == '') {
					$('#state_id').attr('disabled', true); // if no country is passed in, disable state dropdown
				}
				$('#city_id').attr('disabled', true);
				$('#addCity').hide();
				$('#city_id').val('');
				bindChangeState();
				if (typeof(pageTracker) !== 'undefined') {
					pageTracker._trackPageview('/ajax/printStateDropdown.php'); // track this AJAX call in google analytics
				}
			});
		});
	};
	
	// when user changes the state dropdown, populate the city dropdown accordingly
	var bindChangeState = function() {
		$('#state_id').change(function() {
			var country_iso 	= $('#country_iso').val(); 		// get selected country ISO
			var state_id 		= $(this).val(); 				// get selected state_id
			var allow_add_city	= $('#allow_add_city').val(); 	// whether or not to show 'add city' link
			if (!allow_add_city) {
				allow_add_city = 'true'; // allow adding new city by default
			}
			$('#city_dropdown').load('/ajax/printCityDropdown.php?country_iso=' + country_iso + '&state_id=' + state_id + '&allow_add_city=' + allow_add_city, function() {
				bindShowAddCityForm();
				$('#addCity').show(); // show the 'add city' link
				if (typeof(pageTracker) !== 'undefined') {
					pageTracker._trackPageview('/ajax/printCityDropdown.php'); // track this AJAX call in google analytics
				}
			});
		});
	};
	
	// when user clicks 'add new city', present them with a textbox to enter the city name
	var bindShowAddCityForm = function() {
		$('#addCity').bind (
			'click',
			function(){
			
				$('#addCityForm').slideToggle();
				$('#new_city_name').focus();
				
				return false; // prevent loading of page after clicking anchor

			}
		);
	};
	
	// when user submits new city, insert it into DB and re-populate city dropdown with new city selected.
	var bindAddCitySubmit = function() {
		$('#addCitySubmit').bind (
			'click',
			function(){
			
				var country_iso 	= $('#country_iso').val(); // get selected country ISO
				var state_id 		= $('#state_id').val(); // get selected state_id
				var new_city_name 	= $('#new_city_name').val(); // get new city name

				new_city_name		= encodeURIComponent(new_city_name); // URI encode to handle spaces
				
				$('#city_dropdown').load('/ajax/insertCity.php?country_iso=' + country_iso + '&state_id=' + state_id + '&city_name=' + new_city_name, function() {
					bindShowAddCityForm();
					$('#new_city_name').val('');
					$('#addCityForm').hide();
					if (typeof(pageTracker) !== 'undefined') {
						pageTracker._trackPageview('/ajax/insertCity.php'); // track this AJAX call in google analytics
					}
				});
				return false; // prevent loading of page after clicking anchor

			}
		);
	};
	
	bindChangeCountry();
	bindChangeState();
	bindAddCitySubmit();
	bindShowAddCityForm();
	
});