// JQUERY plug-ins
/*
Description:

	Adds options to a select box (or series of select boxes)

Usage:

	$(window).load(
		function()
		{
			$("#myselect").addOption("Value", "Text"); // add single value (will be selected)
			$("#myselect").addOption("Value 2", "Text 2", false); // add single value (won't be selected)
			$("#myselect").addOption({"foo":"bar","bar":"baz"}, false); // add multiple values, but don't select
		}
	);
*/
$.fn.addOption = function()
{
	if(arguments.length == 0) return this;
	// select option when added? default is true
	var selectOption = true;
	// multiple items
	var multiple = false;
	if(typeof arguments[0] == "object")
	{
		multiple = true;
		var items = arguments[0];
	}
	if(arguments.length >= 2)
	{
		if(typeof arguments[1] == "boolean") selectOption = arguments[1];
		else if(typeof arguments[2] == "boolean") selectOption = arguments[2];
		if(!multiple)
		{
			var value = arguments[0];
			var text = arguments[1];
		}
	}
	this.each(
		function()
		{
			if(this.nodeName.toLowerCase() != "select") return;
			if(multiple)
			{
				for(v in items)
				{
					$(this).addOption(v, items[v], selectOption);
				}
			}
			else
			{
				var option = document.createElement("option");
				option.value = value;
				option.text = text;
				this.options.add(option);
			}
			if(selectOption)
			{
				this.options[this.options.length-1].selected = true;
			}
		}
	)
	return this;
}

/*
Description:

	Removes options from a select box (or series of select boxes)

Usage:

	$(window).load(
		function()
		{
			$("#myselect").removeOption("Value"); // remove by value
			$("#myselect").removeOption(0); // remove by index
			$("#myselect").removeOption(); // remove all
		}
	);
	
*/
$.fn.removeOption = function(){
  var value,index,all;
  if(arguments.length == 0) all=true;
	else if(typeof arguments[0] == "string") value = arguments[0];
	else if(typeof arguments[0] == "number") index = arguments[0];
	else return this;
	this.each(
		function(){
			if(this.nodeName.toLowerCase() != "select") return;
			if(value){
				var optionsLength = this.options.length;
				for(var i=optionsLength-1; i>=0; i--){
					if(this.options[i].value == value) this.options[i] = null;			
				}
			}
			else if (all) {
      	var optionsLength = this.options.length;
				for(var i=optionsLength-1; i>=0; i--) this.options[i] = null;			
      }
      else this.remove(index);
		
		}
	)
	return this;
}

/**
*  retourne une référence sur l'objet DOM correspondant à l'objet JQUERY
*
*
*
*/
	 
$.domObject = function(jqobject){
	return document.getElementById(jqobject.id());
}



//-->
