(function($){
	var options;

	$.fn.extend({
		cityLoader: function(opt) {
			options = $.extend({selectCountry: this}, opt);

			if (!options.selectCity) {
				return;
			}

			options.selectCountry.change(function(event) {
				markProcess(true);
				sendRequest($(this).val());
			});

		}
	});

	function markProcess(busy) {
		options.selectCountry.attr('disabled', busy);
		options.selectCity.attr('disabled', busy);
	}

	function sendRequest(countryId) {
		$.getJSON(options.path, {country: countryId}, function(cityList) {
			if (cityList) {
				options.selectCity.find('option:gt(0)').remove();
				$.each(cityList, function(index, city) {
					options.selectCity.append('<option value="' + city.id + '">' + city.name + '</option>');
				})
			}
			markProcess(false);
		});
	}


})(jQuery);