(function($){
	$.fn.getCountryCodes = function() {
		var container = $(this);
		if($(container).data("value") !== undefined || $(container).data("value") !== "") {
			var val = $(container).data("value");
		} else var val = false;
		$.ajax({
			type: "post",
			url: "app/includes/ajax.php",
			data: {action: "get_country_codes"}
		}).done(function(data){
			try {
				data = JSON.parse(data);
				var country = data.country.trim();
				var e = $("<select class=\"standardSelect\" name=\"country_code\" id=\"country_code\"></select>");
				data = JSON.parse(data.all);
				val = val ? val : country;
				for(var key in data) {
					var options = {
						element: "option",
						content: data[key].dial_code+" ("+data[key].code+")",
						value: data[key].dial_code,
						title: data[key].name
					};
					if((val == data[key].dial_code) || data[key].code.trim() == val) {
						options.selected = "selected";
					}
					
					$(e).addElement(options);
				}
				$(container).append(e);
				$(".standardSelect").chosen({
	                disable_search_threshold: 10,
	                no_results_text: "Oops, nothing found!",
	                width: "100%"
	            });
			}
			catch(err) {

			}
			finally {}
		}).fail(function(){
			$(container).getCountryCodes();
		})
	}
})(jQuery);

jQuery(document).ready(function($){
	$(".country-code").each(function(){
		$(this).getCountryCodes();
	})
})