(function($)
{
	$.fn.selectCombo = function(url, target, settings)
	{
		var defaults = {hidetarget: true, indicator: false, pageload: false};

		$.extend(defaults, settings);

		return this.each(function()
		{
			var qobj = this;
			var targetlabel = target.replace(/#/, '');
				targetlabel = "label[@for='" + targetlabel + "']";

			if (defaults.indicator != false)
				$(defaults.indicator).hide();

			hidetargetinfo = function()
			{
				if ($(qobj).attr('id') != $(target).attr('id'))
				{
					$(targetlabel).hide();
					$(target).hide();
				}
			}

			if (defaults.hidetarget && $(target).val() == '')
				hidetargetinfo();
			
			loadOptions = function()
			{
				qval = $(qobj).val();

				if (defaults.indicator != false)
					$(defaults.indicator).show();

				if (defaults.hidetarget)
					hidetargetinfo();

				if ($(qobj).attr('id') != $(target).attr('id'))
					$(target).empty();
				
				$.getJSON(url, {q: qval}, function(j)
				{
//					console.log(j.length);
					if (j.length > 0)
					{
						var setoptions = '';
						for (var i = 0; i < j.length; i++)
						{
							setoptions += '<option value="' + j[i].oV + '">' + j[i].oT + '</option>';
						}

						$(target).html(setoptions);
						
						$("option:first", target).attr("selected","selected");

						if (defaults.indicator != false)
							$(defaults.indicator).hide();

						$(targetlabel).show();
						$(target).show();
					}
				});//end JSON
			}
			
			$(this).change(loadOptions);//end change fn
			
			if (defaults.pageload && $(qobj).val() != '')
				loadOptions();
		
		});//end return for each
	}
})(jQuery);
