/*
 * Copyright (c) 2006 Sam Collett (http://www.texotela.co.uk)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 * 
 * Addepted to select an option by Mathias Bank (http://www.mathias-bank.de)
 */
 
(function ($) {
	/*
	 * Get a value of an option by index
	 *
	 * @name     getOptions
	 * @author   Daoud Bahaz (http://notyet)
	 * @param    index specifies, which options value should be return
	 * @example  jQuery("#myselect").getOptionValue(0);
	 *
	 */
	$.fn.getOptionValue = function(index) {
			if (this.length > 1) return;
			if (this[0].nodeName.toLowerCase() != "select") return;
			
			// get number of options
			var optionsLength = this[0].options.length;
			if (index >= optionsLength) return;
			else
				return this[0].options[index].value;
	}

    $.fn.existeOption = function(value) {
				if (this[0].nodeName.toLowerCase() != "select") return false;
				var optionsLength = this[0].options.length;
				for  (i=0;i< optionsLength;i++){
					if (this[0].options[i].value==value) return true;
				}
				return false;
	}
/*
	 * Get a text of an option by index
	 *
	 * @name     getOptions
	 * @author   Daoud Bahaz (http://notyet)
	 * @param    index specifies, which options value should be return
	 * @example  jQuery("#myselect").getOptionText(0);
	 *
	 */
	$.fn.getOptionText = function(index) {
			if (this.length > 1) return;
			if (this[0].nodeName.toLowerCase() != "select") return;
			
			// get number of options
			var optionsLength = this[0].options.length;
			if (index >= optionsLength) return;
			else
				return this[0].options[index].text;
	}
	
	$.fn.Nbre_Selected = function() {
		if (this[0].nodeName.toLowerCase() != "select") return false;
		var optionsLength = this[0].options.length;
		var nbre=0;
		for  (i=0;i< optionsLength;i++){
			if (this[0].options[i].selected) nbre++;
		}
		return nbre;
	}
	
	 $.fn.selectedAll = function() {
		if (this[0].nodeName.toLowerCase() != "select") return false;
		var optionsLength = this[0].options.length;
		for  (i=0;i< optionsLength;i++){
			this[0].options[i].selected= true;
		}
		return true;
	}
	/*
	 * Set a value of an option by index
	 *
	 * @name     setOption
	 * @author   Daoud Bahaz (http://notyet)
	 * @param    index specifies, which options value should be set
	 * @example  jQuery("#myselect").setOption(0, 1, "One", false);
	 *
	 */
	$.fn.setOption = function(index, value, text, opt) {
				if (this.length > 1) return;
				if (this[0].nodeName.toLowerCase() != "select") return;
				
				// get number of options
				var optionsLength = this[0].options.length;
				if (index >= optionsLength) return;
				else{
					if (opt){
						this[0].options[index].value = (this[0].options[index].value).substr(0, 1) + value.substr(-7);
						//this[0].options[index].style.backgroundColor = text;  // Seulement pour le cas oł text contient le code d'un couleur
					}
					else
						this[0].options[index].value = value;
					this[0].options[index].text = text;
				}
				return this[0];
	}
	 
	/*
	 * Search for an option by value
	 *
	 * @name     existOption
	 * @author   Mounir Boukaftane (http://notyet)
	 * @param    value specifies, the value to find
	 * @example  jQuery("#myselect").existOption("One");
	 *
	 */
	$.fn.existOption = function(value) {
			if (this[0].nodeName.toLowerCase() != "select") return false;
			var optionsLength = this[0].options.length;
			for  (i = 0; i < optionsLength; i++){
				if (this[0].options[i].value == value) return true;
			}
			return false;
	} 

	/*
	 * Adds (single/multiple) options to a select box (or series of select boxes)
	 *
	 * @name     addOption
	 * @author   Sam Collett (http://www.texotela.co.uk)
	 * @example  $("#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(var v in items)
					{
						jQuery(this).addOption(v, items[v], selectOption);
					}
				}
				else
				{
					var option = document.createElement("option");
					option.value = value;
					option.text = text;
					//option.style.backgroundColor = text;  // Seulement pour le cas oł text contient le code d'un couleur
					this.options.add(option);
				}
				if(selectOption)
				{
					this.options[this.options.length-1].selected = true;
				}
			}
		)
		return this;
	}
	
	/*
	 * Removes an option (by value or index) from a select box (or series of select boxes)
	 *
	 * @name     removeOption
	 * @author   Sam Collett (http://www.texotela.co.uk)
	 * @update   Daoud Bahaz (http://notyet)
	 * @example  jQuery("#myselect").removeOptions("Value"); // remove by value
	 *           jQuery("#myselect").removeOptions(0); // remove by index
	 *
	 */
	$.fn.removeOptions = function()
	{
		if(arguments.length == 0) return this;
		if(typeof arguments[0] == "string") var value = arguments[0];
		else if(typeof arguments[0] == "number") var index = arguments[0];
		else if(typeof arguments[0] == "object") var indexes = 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;
						}
					}
				}
				if(index)
				{
					this.remove(index);
				}
				if (indexes){
					len = indexes.length;
					for(var i=len-1; i>=0; i--) {
						this.remove(indexes[i]);
					}
				}
			}
		)
		return this;
	}
	
	/*
	 * Sort options (ascending or descending) in a select box (or series of select boxes)
	 *
	 * @name     sortOptions
	 * @author   Sam Collett (http://www.texotela.co.uk)
	 * @param    ascending   Sort ascending (true/undefined), or descending (false)
	 * @example  // ascending
	 *           jQuery("#myselect").sortOptions(); // or jQuery("#myselect").sortOptions(true);
	 *           // descending
	 *           jQuery("#myselect").sortOptions(false);
	 *
	 */
	$.fn.sortOptions = function(ascending)
	{
		this.each(
			function()
			{
				if(this.nodeName.toLowerCase() != "select") return;
				// default sort is ascending if parameter is undefined
				ascending = typeof ascending == "undefined" ? true : ascending;
				// get number of options
				var optionsLength = this.options.length;
				// create an array for sorting
				var sortArray = [];
				// loop through options, adding to sort array
				for(var i = 0; i<optionsLength; i++)
				{
					sortArray[i] =
					{
						value: this.options[i].value,
						text: this.options[i].text
					};
				}
				// sort items in array
				sortArray.sort(
					function(option1, option2)
					{
						// option text is made lowercase for case insensitive sorting
						option1text = option1.text.toLowerCase();
						option2text = option2.text.toLowerCase();
						// if options are the same, no sorting is needed
						if(option1text == option2text) return 0;
						if(ascending)
						{
							return option1text < option2text ? -1 : 1;
						}
						else
						{
							return option1text > option2text ? -1 : 1;
						}
					}
				);
				// change the options to match the sort array
				for(var i = 0; i<optionsLength; i++)
				{
					this.options[i].text = sortArray[i].text;
					this.options[i].value = sortArray[i].value;
				}
			}
		)
		return this;
	}
	
	/*
	 * Selects an option by value
	 *
	 * @name     selectOptions
	 * @author   Mathias Bank (http://www.mathias-bank.de)
	 * @author   Daoud Bahaz (http://notyet)
	 * @param    value specifies, which options should be selected
	 * @example  jQuery("#myselect").selectOptions("val1");
	 * @example  jQuery("#myselect").selectOptions([]); // Select all options
	 * @example  jQuery("#myselect").selectOptions(-1); // Unselect all options
	 *
	 */
	$.fn.selectOptions = function() {
		if(arguments.length == 0) return this;
		if(typeof arguments[0] == "string") var value = arguments[0];
		else if(typeof arguments[0] == "number") var index = arguments[0];
		else if(typeof arguments[0] == "object") var indexes = arguments[0];
		else return this;
		this.each(
			function()	{
				if(this.nodeName.toLowerCase() != "select") return;
				
				// get number of options
				var optionsLength = this.options.length;
				
				//if (value){
					for(var i = 0; i<optionsLength; i++) {
						if (this.options[i].value == value) {
							this.options[i].selected = true;
						};
					}
				//}
				if (index){
					if (index == -1)
						// unselect all options
						for(var i = 0; i<optionsLength; i++) {
							this.options[i].selected = false;
						}
					else
						this.options[index].selected = true;
				}
				if (indexes){
					len = indexes.length;
					if (len == 0)
						// select all options
						for(var i = 0; i<optionsLength; i++) {
							this.options[i].selected = true;
						}
					else
						// select indicated options
						for(var i = 0; i<len; i++) {
							this.options[indexes[i]].selected = true;
						}
				}
			}
		)
		return this;
	}
	
	/*
	 * Return values of selected options as Array
	 *
	 * @name     selectedOptions
	 * @author   Daoud Bahaz (http://notyet)
	 * @param    Empty Array for catching results.
	 * @example  jQuery("#myselect").selectedOptions(array);
	 *
	 */
	$.fn.selectedOptions = function(selectedArray) {
				if (this.length > 1) return;
				if (this[0].nodeName.toLowerCase() != "select") return;
				
				// get number of options
				var optionsLength = this[0].options.length;

				for (i=0; i<optionsLength; i++){
					if (this[0].options[i].selected){
						selectedArray.push(i);
					}
				}
				
				return selectedArray;
	}
})(jQuery)
