(function($){$.fn.closeColorSelector=function(){return this.each(function(){if(this._lwsColorSelector!=undefined)
this._lwsColorSelector.trigEdited();}).remove()};function LwsColorSelector(){this.updated=null;this.edited=null;this.newColor="#000000";this.oldColor="#000000";this._main=null;this.trigEdited=function(){if(this.edited!=null)
this.edited(this.newColor);};this._html=function(){str="<div id='lwss-color-selector-container' class='lwss-color-selector-relative'>";str+="<div class='lwss-color-selector-dialog'>";str+="<div class='lwss-color-column lwss-color-sliders'>";str+="<div class='lwss-color-line'>";str+="<div class='lwss-color-slide-container lwss-color-slide-xy'>";str+="<div class='lwss-color-slide lwss-color-bg-s'></div>";str+="<div class='lwss-color-slide lwss-color-bg-b'></div>";str+="<div class='lwss-color-cursor lwss-color-cursor-sb'></div>";str+="<div class='lwss-color-slide lwss-color-interact'></div>";str+="</div>";str+="<div class='lwss-color-slide-container lwss-color-slide-y'>";str+="<div class='lwss-color-slide lwss-color-bg-h'></div>";str+="<div class='lwss-color-cursor lwss-color-cursor-h'></div>";str+="<div class='lwss-color-slide lwss-color-interact'></div>";str+="</div></div>";str+="<div class='lwss-color-line'>";str+="<div class='lwss-color-slide-container lwss-color-slide-x'>";str+="<div class='lwss-color-slide lwss-color-damier'></div>";str+="<div class='lwss-color-slide lwss-color-bg-a'></div>";str+="<div class='lwss-color-cursor lwss-color-cursor-a'></div>";str+="<div class='lwss-color-slide lwss-color-interact'></div>";str+="</div>";str+="<div class='lwss-color-slide-container'>";str+="<div class='lwss-color-demo-small lwss-color-damier'></div>";str+="<div class='lwss-color-demo-small lwss-color-demo-next'></div>";str+="</div>";str+="</div>";str+="</div>";str+="<div class='lwss-color-column lwss-color-details' >";str+="<div class='lwss-color-composition'>";str+="<div class='lwss-color-parts lwss-color-hsl'>";str+="<label><div>H</div><input type='text' pattern='(3[0-6]\\d)|([0-2]?\\d?\\d)' data-ref='hsl' data-index='0' size='2'></label>";str+="<label><div>S</div><input type='text' pattern='([01]?\\d?\\d)' data-ref='hsl' data-index='1' size='2'></label>";str+="<label><div>B</div><input type='text' pattern='([01]?\\d?\\d)' data-ref='hsl' data-index='2' size='2'></label>";str+="<label><div>A</div><input type='text' pattern='([01]?\\d?\\d)' data-ref='hsl' data-index='3' size='2'></label>";str+="</div>";str+="<div class='lwss-color-parts lwss-color-rgb'>";str+="<label><div>R</div><input type='text' pattern='(25[0-5])|(2[0-4]\\d)|([01]?\\d?\\d)' data-ref='rgb' data-index='0' size='2'></label>";str+="<label><div>G</div><input type='text' pattern='(25[0-5])|(2[0-4]\\d)|([01]?\\d?\\d)' data-ref='rgb' data-index='1' size='2'></label>";str+="<label><div>B</div><input type='text' pattern='(25[0-5])|(2[0-4]\\d)|([01]?\\d?\\d)' data-ref='rgb' data-index='2' size='2'></label>";str+="<label><div>A</div><input type='text' pattern='(25[0-5])|(2[0-4]\\d)|([01]?\\d?\\d)' data-ref='rgb' data-index='3' size='2'></label>";str+="</div></div>";str+="<div class='lwss-new-color'><input type='text' class='lwss-color-input-next' pattern='((rgba?|hsla?)\\(\\d+(\\.\\d+)?%?(,\\d+(\\.\\d+)?%?)*\\))|(#[0-9a-fA-F]{3,8})' size='8'>";str+="<div tabindex=0 class='lwss-new-color-submit'></div>";str+="</div></div></div></div></div></div>";return str};this.clampRGBA=function(rgb){for(var i=0;i<rgb.length;++i)rgb[i]=Math.max(0,Math.min(255,rgb[i]));return rgb};this.clampHSLA=function(hsl){hsl[0]=Math.max(0,Math.min(360,hsl[0]));for(var i=1;i<hsl.length;++i)hsl[i]=Math.max(0,Math.min(100,hsl[i]));return hsl};this.toRGBA=function(hsl){var h=6.0*hsl[0]/360.0,s=hsl[1]/100.0,v=hsl[2]/100.0;var i=Math.floor(h),f=h-i,p=v*(1-s),q=v*(1-f*s),t=v*(1-(1-f)*s),mod=i%6,r=[v,q,p,p,t,v][mod],g=[t,v,v,q,p,p][mod],b=[p,p,t,v,v,q][mod];return this.clampRGBA([r*255,g*255,b*255,255*hsl[3]/100])};this.toHSLA=function(rgb){var r=rgb[0]/255.0,g=rgb[1]/255.0,b=rgb[2]/255.0;var max=Math.max(r,g,b),min=Math.min(r,g,b);var h,s,v=max;var d=max-min;s=max===0?0:d/max;if(max==min){h=0}else{switch(max){case r:h=(g-b)/d+(g<b?6:0);break;case g:h=(b-r)/d+2;break;case b:h=(r-g)/d+4;break}
h/=6}
return this.clampHSLA([h*360,s*100,v*100,100*rgb[3]/255.0])};this.fromColorCursor=function(cursor,horizontal){var p=cursor.parent();if(horizontal)
return(cursor.offset().left-p.offset().left)/(p.innerWidth()-cursor.outerWidth());else return(cursor.offset().top-p.offset().top)/(p.innerHeight()-cursor.outerHeight())};this.toColorCursor=function(cursor,horizontal,value){var p=cursor.parent();if(horizontal)
cursor.offset({'left':((p.innerWidth()-cursor.outerWidth())*value)+p.offset().left});else cursor.offset({'top':((p.innerHeight()-cursor.outerHeight())*value)+p.offset().top})};this.moveColorCursor=function(e){var cursor=$(e.target).parent().find(".lwss-color-cursor");var x=e.pageX-e.target.offsetLeft-(cursor.outerWidth()/2);var y=e.pageY-e.target.offsetTop-(cursor.outerHeight()/2);if(cursor.parent().hasClass('lwss-color-slide-xy'))cursor.offset({"top":y,"left":x});else if(cursor.parent().hasClass('lwss-color-slide-x'))cursor.offset({"left":x});else if(cursor.parent().hasClass('lwss-color-slide-y'))cursor.offset({"top":y})};this.colorSelect=function(e){if(e.buttons==1){this.moveColorCursor(e);var master=$(e.target).closest(".lwss-color-sliders");var h=master.find(".lwss-color-cursor-h");var sl=master.find(".lwss-color-cursor-sb");var a=master.find(".lwss-color-cursor-a");var hsl=[360*this.fromColorCursor(h,h.parent().hasClass('lwss-color-slide-x')),100*this.fromColorCursor(sl,!0),100-(100*this.fromColorCursor(sl,!1)),100*this.fromColorCursor(a,a.parent().hasClass('lwss-color-slide-x'))];this.applyColor(undefined,this.clampHSLA(hsl))}};this.litteralColor=function(rgb,forceHexa){var str='';if((Math.abs(rgb[3]-255.0)<0.00001)||(forceHexa===!0)){str="#";for(var i=0;i<3;++i)
str+=(rgb[i]<16?'0':'')+Math.round(rgb[i]).toString(16);}else{str="rgba("+Math.round(rgb[0])+","+Math.round(rgb[1])+","+Math.round(rgb[2])+","+(rgb[3]/255.0).toFixed(3)+")"}
return str};this.applyGradient=function(elt,horizontal,h0,s0,l0,a0,h1,s1,l1,a1){var c0=this.litteralColor(this.toRGBA([h0,s0,l0,a0]),!1);var c1=this.litteralColor(this.toRGBA([h1,s1,l1,a1]),!1);var str="background: -webkit-linear-gradient("+(horizontal?"left,":"top,")+c0+","+c1+");";str+="background: -o-linear-gradient("+(horizontal?"left,":"top,")+c0+","+c1+");";str+="background: -moz-linear-gradient("+(horizontal?"left,":"top,")+c0+","+c1+");";str+="background: linear-gradient("+(horizontal?"to right,":"to bottom,")+c0+","+c1+");";elt.attr("style",str)};this.applyColor=function(rgb,hsl){if(rgb===undefined)rgb=this.toRGBA(hsl);else if(hsl===undefined)hsl=this.toHSLA(rgb);this.newColor=this.litteralColor(rgb,!1);this._main.find("input.lwss-color-input-next").val(this.newColor);this._main.find(".lwss-color-demo-next").css("background",this.newColor);this._main.find(".lwss-color-parts input").each(function(){$(this).val(Math.round(($(this).data('ref')=='rgb'?rgb:hsl)[$(this).data('index')]))});var master=this._main.find(".lwss-color-sliders");var h=master.find(".lwss-color-cursor-h");h.css('background-color',this.litteralColor(this.toRGBA([hsl[0],100,100,100]),!0));var sl=master.find(".lwss-color-cursor-sb");var a=master.find(".lwss-color-cursor-a");this.toColorCursor(h,h.parent().hasClass('lwss-color-slide-x'),hsl[0]/360.0);this.toColorCursor(sl,!0,hsl[1]/100.0);this.toColorCursor(sl,!1,1.0-(hsl[2]/100.0));this.toColorCursor(a,a.parent().hasClass('lwss-color-slide-x'),hsl[3]/100.0);var a=master.find(".lwss-color-bg-a");this.applyGradient(a,a.parent().hasClass("lwss-color-slide-x"),hsl[0],hsl[1],hsl[2],0,hsl[0],hsl[1],hsl[2],100);this.applyGradient(master.find(".lwss-color-bg-s"),!0,hsl[0],0,100,100,hsl[0],100,100,100);if(this.updated!=null)this.updated(this.newColor);};this.reset=function(){this.setColor(this.oldColor)};this.composeColor=function(grp){var a=[0,0,0,0];grp.find("input").each(function(){a[$(this).data('index')]=parseInt($(this).val())});return a};this.composeFromRGB=function(event){var rgb=this.composeColor($(event.target).closest(".lwss-color-parts"));this.applyColor(this.clampRGBA(rgb),undefined);return!1};this.composeFromHSL=function(event){var hsl=this.composeColor($(event.target).closest(".lwss-color-parts"));this.applyColor(undefined,this.clampHSLA(hsl));return!1};this.demoShowNew=function(event){$(event.target).css('background',this.newColor)};this.demoShowOld=function(event){$(event.target).css('background',this.oldColor)};this.changeColor=function(event){this.setColor($(event.target).val(),!1)};this.textRgb=function(val){var rgb=val.match(/\d+(\.\d+)?%?/g);for(var i=0;i<rgb.length;++i)rgb[i]=(rgb[i].endsWith("%")?(255.0*rgb[i].split("%")[0]/100.0):rgb[i]);if(rgb.length<4)rgb.push(1);rgb[3]=rgb[3]*255;return rgb}
this.setColor=function(val,setOldColor){try{val=val.trim();var rgb=[0,0,0,255],hsl=[0,0,0,100];if(val.length>0){if(val.startsWith("#")){if(val.length>5)rgb=val.match(/[0-9A-Fa-f]{2}/g);else{rgb=val.match(/[0-9A-Fa-f]/g);for(var i=0;i<rgb.length;++i)rgb[i]=rgb[i].concat(rgb[i]);}
for(var i=0;i<rgb.length;++i)rgb[i]=parseInt(rgb[i],16);if(rgb.length<4)rgb.push(255);hsl=this.toHSLA(rgb)}else if(val.startsWith("hsl")){hsl=val.match(/\d+(\.\d+)?/g);if(hsl.length<4)hsl.push(1);hsl[3]=hsl[3]*100;rgb=this.toRGBA(hsl)}else if(val.startsWith("rgb")){rgb=this.textRgb(val);hsl=this.toHSLA(rgb)}else{try{var div=$('<div />').css('background-color',val);val=window.getComputedStyle(div[0]).backgroundColor}catch(e){}
rgb=this.textRgb(val);hsl=this.toHSLA(rgb)}}
rgb=this.clampRGBA(rgb);if(setOldColor!=!1)
this.oldColor=this.litteralColor(rgb,!1);this.applyColor(rgb,this.clampHSLA(hsl))}catch(e){console.log("Invalid value.")}
return!1};this.show=function(parent){if(this.offset!=undefined){this._main=$(this._html()).appendTo($("body"));this._main.offset({"top":(this.offset.top+14),"left":this.offset.left})}else{this._main=$(this._html()).appendTo(parent)}
this._main[0]._lwsColorSelector=this;var me=this;var input=this._main.find("input.lwss-color-input-next");input.on('change',function(e){me.changeColor(e)});input.on('keydown',function(e){if((e.keyCode||e.which||e.charCode||0)===9){me._main.find(".lwss-color-composition input:"+(e.shiftKey?"last":"first")).trigger('focus');return!1}});this._main.find(".lwss-new-color-submit").on('click',function(){$("#lwss-color-selector-container").closeColorSelector()});this._main.find("input").on('focus',function(){$(this).select()});this._main.find(".lwss-color-interact").on('mousedown',function(e){me.colorSelect(e)});this._main.find(".lwss-color-interact").on('mousemove',function(e){me.colorSelect(e)});this._main.find(".lwss-color-hsl input").on('change',function(e){me.composeFromHSL(e)});this._main.find(".lwss-color-rgb input").on('change',function(e){me.composeFromRGB(e)});this._main.find(".lwss-color-demo-small.lwss-color-demo-next").on('mouseenter',function(e){me.demoShowOld(e)});this._main.find(".lwss-color-demo-small.lwss-color-demo-next").on('mouseleave',function(e){me.demoShowNew(e)});this._main.find(".lwss-color-demo-small.lwss-color-demo-next").on('click',function(e){me.reset(e)});this.reset();input.trigger('focus')}}
$.fn.colorSelector=function(param){if(this.length>0){$("#lwss-color-selector-container").remove();var sc=new LwsColorSelector();if(param!=undefined){if(param.color!=undefined)
sc.oldColor=param.color;if(param.updated!=undefined)
sc.updated=param.updated;if(param.edited!=undefined)
sc.edited=param.edited;if(param.offset!=undefined)
sc.offset=param.offset}
sc.show(this);return sc}
return this};$.widget("lws.lwss_colorButton",{options:{color:"",showvalue:!0,icon:"lws-icon lws-icon-dropper",cssColor:"lwss-colorpicker-default-color",property:""},_create:function(){this.wrapper=this.element.addClass("lwss-colorpicker-wrapper");this.shadow=$("<div>").addClass("lwss-colorpicker-shadow").appendTo(this.wrapper);this.left=$("<div>").addClass("lwss-colorpicker-left").appendTo(this.wrapper);this.right=$("<div>").addClass("lwss-colorpicker-right").appendTo(this.wrapper);this.icon=$("<div>").addClass("lwss-colorpicker-icon "+this.options.icon+" "+this.options.cssColor).appendTo(this.left);this.demo=$("<div>").addClass("lwss-colorpicker-demo").appendTo(this.left);this.value=$("<input>").addClass("lwss-colorpicker-value lwss_value").data("css",this.options.property).appendTo(this.right);this.label=$("<div>").addClass("lwss-colorpicker-label").html(this.options.property).appendTo(this.right)
this.wrapper.on('mouseenter',this._bind(this._showShadow,this));this.wrapper.on('mouseleave',this._bind(this._hideShadow,this));if(this.value.val().length>0){this.options.color=this.value.val()}
this._applyColor(this.options.color);var me=this;this.left.on('click',this._bind(this.open,this));this.value.on('change',this._bind(this._changeVal,this));this.value.on('focus',function(){this.select()})},_bind:function(fn,me){return function(){return fn.apply(me,arguments)}},_showShadow:function(){this.shadow.show()},_hideShadow:function(){this.shadow.hide()},_applyColor:function(value){this.demo.css('background-color',value);this.value.val(value)},_changeVal:function(){this.options.color=this.value.val();this.demo.css('background-color',this.options.color)},open:function(){var me=this;var shadow=$(this.element).closest(".lwss_editor").find(".lwss-popup-shadow").show();var data={color:me.options.color,edited:function(c){me.color.call(me,c)},offset:this.element.offset(),};me.element.colorSelector(data);return!1},color:function(value){if(value===undefined){return this.options.color}
var oldVal=this.options.color;this.options.color=value;this._applyColor(this.options.color);if(oldVal!=this.options.color){this._trigger("changed",null,this.options.color);this.value.trigger("change")}},_destroy:function(){this.wrapper.remove()}});$.widget("lws.lws_color_picker",{options:{color:"#FFFFFF",title:""},_create:function(){this._setOptions();this._createStructure();this.demo=this.element.find('.lws-cp-demo');this.input=this.element.find('.lws-cp-input');this._applyColor(this.options.color);this.demo.on('click',this._bind(this.open,this));this.input.on('change',this._bind(this._changeVal,this));this.input.on('focus',function(){this.select()})},_bind:function(fn,me){return function(){return fn.apply(me,arguments)}},_setOptions:function(){if(this.element.data('title')!=undefined)
this.options.title=this.element.data('title');if(this.element.data('color')!=undefined)
this.options.color=this.element.data('color');},_createStructure:function(){$("<div>",{'class':'lws-cp-left',}).append($("<div>",{'class':'lws-cp-demo',})).appendTo(this.element);$("<div>",{'class':'lws-cp-right',}).append($("<input>",{'class':'lws-cp-input',})).append($("<div>",{'class':'lws-cp-title','html':this.options.title,})).appendTo(this.element)},_applyColor:function(value){this.demo.css('background-color',value);this.input.val(value)},_changeVal:function(){this.options.color=this.input.val();this.demo.css('background-color',this.options.color)},open:function(){var me=this;var data={color:me.options.color,edited:function(c){me.color.call(me,c)},offset:this.element.offset(),};me.element.colorSelector(data);return!1},color:function(value){if(value===undefined){return this.options.color}
var oldVal=this.options.color;this.options.color=value;this._applyColor(this.options.color);if(oldVal!=this.options.color){this._trigger("changed",null,this.options.color);this.input.trigger("change")}},_destroy:function(){this.element.remove()}});$.fn.colorBtn=function(){return this.each(function(){var src=$(this);var demo=src.find(".lwss-btn-color-demo");var value=src.find(".lwss-btn-color-value");var input=src.find("input");src.on('click',function(){var data={color:value.html(),edited:function(c){demo.css('backgroundColor',c);value.html(c);if(input.hasClass('lwss-merge-css'))demo.mergeCSS();else input.val(c)}};src.parent().colorSelector(data);return!1})})}})(jQuery)
jQuery(function($){$(document).on('click',function(e){if($(e.target).closest(".lwss-color-selector-relative").length==0)
$("#lwss-color-selector-container").remove();});$(document).on('keyup',function(e){if((e.keyCode||e.which||e.charCode||0)===27){$("#lwss-color-selector-container").remove()}else if(($(e.target).closest(".lwss-color-selector-relative").length>0)&&((e.keyCode||e.which||e.charCode||0)===13)){$("#lwss-color-selector-container").closeColorSelector()}});$(".lwss-btn-color-selector").colorBtn();$(".lws-color-button").lwss_colorButton();$(".lws-color-picker").lws_color_picker()})