(function( $ ){
	$.fn.msgGetRecepients = function(attempt){
		var element = $(this);
		$(element).html("<em>Getting Receivers</em>");
		var btn = $(this).data("btn");
		var data = $(this).data();
		data.action = "get_recepients_form";
		$(btn).attr("disabled", true);
		$.ajax({
			type: "post",
			url: "contents/plugins/messaging/ajax.php",
			data: data
		}).done(function(data){
			try{
				data = JSON.parse(data);
				$(element).html(data);
				$("#receivers").chosen();
				$("#receivers_chosen").css({
					"width": "100%"
				})
				$(btn).attr("disabled", false);
			}
			catch(err) {
				console.log(err);
				console.log(data);
			}
			finally{}
		}).fail(function(){
			if(attempt < 3) {
				attempt++;
				$(element).msgGetRecepients(attempt);
			}
		});
	}
})(jQuery);

jQuery(document).ready(function($){
	$("#display-receivers").msgGetRecepients(0);
})