	(function($){
		$.fn.B86_ajaxSelect = function(options) {
			
			var defaults = {
				url: "json_example.php",
				selectResult: "#select2",
				textLoading: "Aguarde, carregando...",
				textNoResult: "Nenhum resultado encontrado"
			};
			var options = $.extend(defaults, options);

			$(this).change(function () {
				
				/* before loading (antes de carregar) */
				$(options['selectResult']).html('<option>'+ options['textLoading'] +'</option>');
				$(options['selectResult']).attr('disabled','disabled');
				
				/* ajax query (consulta ajax) */
				$.getJSON(options['url'], {id: $(this).val()},function(data){
					
					var result = '';
					$.each(data.items, function(i,item){
						result = result + '<option value="'+ item.value +'">'+item.text+'</option>';
					});

				/* after ajax, enabled select and append result (depois do ajax, habilita o select e anexa o resultado) */
					$(options['selectResult']).removeAttr('disabled');
					$(options['selectResult']).html(result);

				});
/*				
				// if not result (se não houver resultado)
				if($(options['selectResult']).text()==options['textLoading']){
					$(options['selectResult']).removeAttr('disabled');
					$(options['selectResult']).html('<option>'+options['textNoResult']+'</option>');
				}
*/

			});
		};
	})(jQuery);
