(function($){"use strict";var SimpleColorPicker=function(element,options){this.select=$(element);this.options=$.extend({},$.fn.simplecolorpicker.defaults,options);this.select.hide();var fakeText=" ";var colorList="";$("option",this.select).each(function(){var option=$(this);var color=option.val();var title=option.text();var selected="";if(option.attr("selected")){selected='class="selected"'}colorList+="
'+fakeText+"
"});if(this.options.picker){var selectText=this.select.find("option:selected").text();var selectValue=this.select.val();this.icon=$(''+fakeText+"").insertAfter(this.select);this.icon.on("click",$.proxy(this.show,this));this.picker=$('').appendTo(document.body);this.picker.html(colorList);this.picker.on("click",$.proxy(this.click,this));$(document).on("mousedown",$.proxy(this.hide,this));this.picker.on("mousedown",$.proxy(this.mousedown,this))}else{this.inline=$('').insertAfter(this.select);this.inline.html(colorList);this.inline.on("click",$.proxy(this.click,this))}};SimpleColorPicker.prototype={constructor:SimpleColorPicker,show:function(){var bootstrapArrowWidth=16;var pos=this.icon.offset();this.picker.css({left:pos.left+this.icon.width()/2-bootstrapArrowWidth,top:pos.top+this.icon.outerHeight()});this.picker.show(this.options.delay)},hide:function(){this.picker.hide(this.options.delay)},click:function(e){var target=$(e.target);if(target.length===1){if(target[0].nodeName.toLowerCase()==="div"){var color=target.css("background-color");var title=target.attr("title");target.siblings().removeClass("selected");target.addClass("selected");if(this.options.picker){this.icon.css("background-color",color);this.icon.attr("title",title);this.hide()}this.select.val(this.rgb2hex(color)).change()}}},mousedown:function(e){e.stopPropagation();e.preventDefault()},rgb2hex:function(rgb){function hex(x){return("0"+parseInt(x,10).toString(16)).slice(-2)}var matches=rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);if(matches===null){return rgb}else{return"#"+hex(matches[1])+hex(matches[2])+hex(matches[3])}}};$.fn.simplecolorpicker=function(option){return this.each(function(){var $this=$(this),data=$this.data("simplecolorpicker"),options=typeof option==="object"&&option;if(!data){$this.data("simplecolorpicker",data=new SimpleColorPicker(this,options))}if(typeof option==="string"){data[option]()}})};$.fn.simplecolorpicker.Constructor=SimpleColorPicker;$.fn.simplecolorpicker.defaults={delay:0,picker:false}})(jQuery);