/*
 *
 * Copyright (c) 2006-2008 Sam Collett (http://www.texotela.co.uk)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Version 2.2.3
 * Demo: http://www.texotela.co.uk/code/jquery/select/
 *
 * $LastChangedDate$
 * $Rev$
 *
 */
 
;(function($) {

$.fn.loadingStatus = function(status)
{
	$(this).removeOption();
	if(status)
		$(this).addOption('ANY','Loading...');
}

$.fn.defaultStatus  = function(value, text)
{
	$(this).removeOption();
	$(this).addOption(value, text);
}

$.fn.addOption = function()
{
	var add = function(el, v, t, sO)
	{
		var option = document.createElement("option");
		option.value = v, option.text = t;
		// get options
		var o = el.options;
		// get number of options
		var oL = o.length;

		el.options[oL] = option;
		if(sO)
		{
			option.selected = true;
		}
	};
	
	var a = arguments;
	if(a.length == 0) return this;
	// select option when added? default is true
	var sO = true;
	// multiple items
	var m = false;
	// other variables
	var items, v, t;
	if(typeof(a[0]) == "object")
	{
		m = true;
		items = a[0];
	}
	if(a.length >= 2)
	{
		if(typeof(a[1]) == "boolean") sO = a[1];
		else if(typeof(a[2]) == "boolean") sO = a[2];
		if(!m)
		{
			v = a[0];
			t = a[1];
		}
	}
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
			if(m)
			{
				for(var item in items)
				{
					//alert(items[item].name + ' item=' +item);
					if(items[item].name == 'selected')
					{
						$(this).selectOptions(items[item].value);
					}
					else if(items[item].name == 'disabled')
					{
						$(this).attr("disabled", true);
					}
					else if(items[item].name != '' && items[item].name != 'selected' && items[item].name != 'disabled')
					{
						add(this, items[item].name, items[item].value, sO);
						$(this).attr("disabled", false);
					}
				} 
			}
			else
			{
				add(this, v, t, sO);
			}
		}
	);
	return this;
};

$.fn.ajaxAddOption = function(url, params, select, fn, args)
{
	if(typeof(url) != "string") return this;
	if(typeof(params) != "object") params = {};
	if(typeof(select) != "boolean") select = true;
	
	var el = this;
	var sel = select;
	var texts = new Array();
	var vals = new Array();
	this.each(function(){
		texts[$(this).attr("id")] = $("option:first", this).text();
		vals[$(this).attr("id")] = $("option:first", this).val();
	})

	$(el).loadingStatus(true);

	$.getJSON(url + '?rand=' + Math.random(),
		params,
		function(r){

			el.each(function(){
				if(sel)
				{
					for(var item in r)
					{
						if($(this).attr("id") == item)
						{
							var rr = r[item];
						}
					}
				}
				else
				{
					var rr = r;
				}
				$(this).loadingStatus(false);
				$(this).addOption(vals[$(this).attr("id")], texts[$(this).attr("id")]);
				$(this).addOption(rr, false);

				if(typeof fn == "function")
				{
					if(typeof args == "object")
					{
						fn.apply(this, args);
					} 
					else
					{
						fn.call(this, r);
					}
				}
			});
		}
	);
	return this;
};
$.fn.removeOption = function()
{
	$(this).empty();
};

$.fn.selectOptions = function(value, clear)
{
	var v = value;
	var vT = typeof(value);
	var c = clear || false;
	// has to be a string or regular expression (object in IE, function in Firefox)
	if(vT != "string" && vT != "function" && vT != "object") return this;
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return this;
			// get options
			var o = this.options;
			// get number of options
			var oL = o.length;
			for(var i = 0; i<oL; i++)
			{
				if(v.constructor == RegExp)
				{
					if(o[i].value.match(v))
					{
						o[i].selected = true;
					}
					else if(c)
					{
						o[i].selected = false;
					}
				}
				else
				{
					if(o[i].value == v)
					{
						o[i].selected = true;
					}
					else if(c)
					{
						o[i].selected = false;
					}
				}
			}
		}
	);
	return this;
};

$.fn.selectedValues = function()
{
	var v = [];
	this.find("option:selected").each(
		function()
		{
			v[v.length] = this.value;
		}
	);
	return v;
};

$.fn.selectedOptions = function()
{
	return this.find("option:selected");
};

})(jQuery);