/* NICE SELECT Plugin */
/*
* jQuery NICE SELECT Plugin
* v1.0
* Author: Dragos Badea (bedeabza@gmail.com)
*
* replaces regular "select" elements with totally customizable html ui components
*/
(function(jQuery){
// plugin definition
jQuery.fn.niceSelect = function (options){
var options = jQuery.extend({}, jQuery.fn.niceSelect.defaults, options);
return this.each(function(){
var $this = jQuery(this);
var idu = $this.attr("rel");
$this.hide();
$this.after('');
$this.before('');
var opts_obj = document.createElement("UL");
jQuery(opts_obj).addClass(options.virtualSelectClass);
var option = null;
for(var i=0;i<$this[0].options.length;i++){
option = document.createElement("LI");
jQuery(option).attr("rel",$this[0].options[i].value);
jQuery(option).html($this[0].options[i].text);
if(i==$this[0].options.length-1)
jQuery(option).addClass(options.lastClass);
if(jQuery(option).attr("rel")==$this.val())
jQuery(option).addClass(options.selectedClass);
opts_obj.appendChild(option);
}
jQuery("#select"+idu).after(opts_obj);
$this.remove();
jQuery("li", opts_obj).hover(
function(){jQuery(this).addClass(options.overClass);},
function(){jQuery(this).removeClass(options.overClass);}
).click(function(){
var $this = jQuery(this);
$this.parent().hide();
$this.parent().prev().val($this.html());
$this.parent().next().val($this.attr("rel"));
$this.siblings("li").removeClass(options.selectedClass);
$this.addClass(options.selectedClass);
if(options.selectCallback!=null){
var arguments = [{text: $this.html(), value: $this.attr("rel")},$this.parent()];
options.selectCallback.apply(this, arguments);
}
return false;
});
jQuery("#select"+idu).click(function(){
var $this = jQuery(this);
jQuery("."+options.virtualSelectClass).not($this.next()).hide();
$this.next("ul").toggle();
if($this.next("ul").css("display")!="none" && options.openCallback!=null){
var arguments = [$this, $this.next("ul")];
options.openCallback.apply(this, arguments);
}
return false;
});
jQuery("body").click(function(){jQuery("."+options.virtualSelectClass).hide();});
});
}
// plugin defaults
jQuery.fn.niceSelect.defaults = {
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
selectCallback: null, //callback function | equivalend to "onchange" in html | @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
};
})(jQuery);