/* NICE SELECT Plugin */
/*
* jQuery NICE SELECT Plugin v1.0
*
* transforms regular "select" elements into totally customizable elements
*/
(function($){
	// plugin definition
	$.fn.niceSelect = function (options){
		var options = $.extend({}, $.fn.niceSelect.defaults, options);
		var nice_selects_num = 0;
		return this.each(function(){
			//init component
			var $this = $(this);
			var idu = nice_selects_num++;
			var sel_val = $this[0].options.length ? $this[0].options[$this[0].selectedIndex].text : '';
			//build wrapper
			var wrapper = document.createElement("DIV");
			wrapper = $(wrapper);
			wrapper.addClass(options.wrapperClass);
			//build virtual select display field
			var display_field = document.createElement("INPUT");
			display_field = $(display_field);
			display_field.attr("type","text");
			display_field.addClass(options.virtualInputClass);
			display_field.attr("id","select"+idu);
			display_field.attr("rel", $this.val());
			display_field.val(sel_val);
			display_field.attr("readonly", "readonly");
			//$this.before(display_field);
			wrapper.append(display_field);
			//created options list
			var opts_obj = document.createElement("UL");
			$(opts_obj).addClass(options.virtualSelectClass);
			var option = null;
			for(var i=0;i<$this[0].options.length;i++){
				//create options
				option = document.createElement("LI");
				option = $(option);
				option.attr("rel",$this[0].options[i].value);
				option.text($this[0].options[i].text);
				if(i==$this[0].options.length-1)
					option.addClass(options.lastClass);
				if(option.attr("rel")==$this.val())
					option.addClass(options.selectedClass);
				$(opts_obj).append(option);
			}
			//$("#select"+idu).after(opts_obj);
			wrapper.append($(opts_obj));
			//build the hidden field which retains the selected value
			var hidden_field = document.createElement("INPUT");
			hidden_field = $(hidden_field);
			hidden_field.attr("type","hidden");
			hidden_field.attr("name",$this[0].name);
			hidden_field.addClass($this[0].className.replace('nice', ''));
			hidden_field.val($this.val());
			//$this.after(hidden_field);
			wrapper.append(hidden_field);
			$this.after(wrapper);
			if(options.initCallback!=null){
				var arguments = new Array({text: $this.text(), value: $this.attr("rel")},$this.parent());
			}
			$this.remove();
			if(options.wrap!=null){
				hidden_field.after(options.wrap[1]);
				display_field.before(options.wrap[0]);
			}
			if(options.initCallback!=null){
				options.initCallback.apply(this, arguments);
			}
			$("li", opts_obj).hover(
				function(){$(this).addClass(options.overClass);},
				function(){$(this).removeClass(options.overClass);}
			).click(function(){
				var $this = $(this);
				$this.parent().hide();
				$this.parent().prev().val($this.text());
				$this.parent().next().val($this.attr("rel"));
				$this.siblings("li").removeClass(options.selectedClass);
				$this.addClass(options.selectedClass);
				if(options.selectCallback!=null){
					var arguments = new Array({text: $this.text(), value: $this.attr("rel")},$this.parent());
					options.selectCallback.apply(this, arguments);
				}
				return false;
			});
			$("#select"+idu).click(function(){
				var $this = $(this);
				//$this.removeAttr("readonly");
				$("."+options.virtualSelectClass).not($this.next()).hide();
				if($this.next().css("display")=="none"){
					$this.next().show();
					if(options.openCallback!=null){
						var arg = new Array($this, $this.next("ul"));
						options.openCallback.apply(this, arg);
					}
				}else{
					$this.next().hide();
				}
				/*$("body").keydown(function(event){//autocomplete
					var keycode = (document.layers) ? keyStroke.which : event.keyCode;
					var theChar = String.fromCharCode(keycode).toLowerCase();
					if(options.acceptedChars.indexOf(theChar)!=-1){//is accepted char
						var temp_val = $("#select"+idu).val()+theChar;
						var found_val = false;
						var temp_arr = null;
						$("#select"+idu).next().children("li").each(function(){
							if(found_val===false){
								temp_arr = $(this).text().split(temp_val);
								if(temp_arr.length > 1){
									found_val = $(this).attr("rel");
									temp_val = $(this).text();
								}
							}
						});
						console.log(found_val);
						if(found_val!==false){
							$("#select"+idu).val(temp_val);
							$("#select"+idu).next().next().val(found_val);
						}
						return false;
					}
				});*/
				return false;
			});
			$("body").click(function(){
				$("."+options.virtualSelectClass).hide();
				//$("#select"+idu).attr("readonly","readonly");
				if(options.closeCallback!=null){
					options.closeCallback.apply(this, new Array());
				}
			});
		});
	}
	// plugin defaults
	$.fn.niceSelect.defaults = {
		wrapperClass: "nice_select_wrapper", //class for the wrapper div
		selectedClass: "selected", //class for the selected item
		overClass: "over", //class for hover items
		virtualSelectClass: "virtual_select", //class for the virtual select (ul)
		virtualInputClass: "nice_select", //class for the textfield item that displays the selected value
		lastClass: "last", //class for the last element of the list
		acceptedChars: "qwertyuiopasdfghjklzxcvbnm0123456789<>", //accepted chars for autocomplete
		selectCallback: null, //callback function | equivalend to "onchange" in text | @params: 1 - JSON obj containing value and text 2 - the UL jquery object targeted
		openCallback: null, //callback function | executed when the user clicks on the virtual select and the list of options are opened  | @params: 1 - The textbox placeholder for select 2 - the UL jquery object targeted
		closeCallback: null, //callback function | executed when the virtual select is closed without changing the value | @params: none
		initCallback: null //callback function | executed when the component is instanciated | @params: 1 - The textbox placeholder for select 2 - the UL jquery object targeted
	};
})(jQuery);