(function ($) {
	$.fn.mecmesinCountryList = function (options) {

		if (!options.countyElement) return this;
		if (!options.defaultCountyText) return this;
		if (!options.postUrl) return this;
		if (!options.pleaseSelectText) return this;

		this.change(function () {
			var countryId = $(this).val();
			if (!countryId) {
				options.countyElement.html("<option value='0'>" + options.defaultCountyText + "</option>");
			} else {
				$.post(options.postUrl,
						{ countryId: countryId },
						function (data) {
							options.countyElement.html('');
							if (data.length > 1) options.countyElement.prepend($('<option value="">' + options.pleaseSelectText + '</option>'));
							for (var i = 0; i < data.length; i++) {
								var option = $('<option value="' + data[i].Id + '">' + data[i].Name + '</option>');
								if (!contains(data[i].Name)) {
									options.countyElement.append(option);
								}
							}
						}, 'json');
			}
		});

		var contains = function (text) {
			options.countyElement.each(function () {
				if ($(this).text() == text) return true;
			});
			return false;
		};

		return this;
	};
})(jQuery);
