/*
 Color picker
 Author: Stefan Petre www.eyecon.ro

 Dual licensed under the MIT and GPL licenses
*/
(function(d){var r=function(){var B=65,M={eventName:"click",onShow:function(){},onBeforeShow:function(){},onHide:function(){},onChange:function(){},onSubmit:function(){},color:"ff0000",livePreview:!0,flat:!1},m=function(a,b){a=h(a);d(b).data("colorpicker").fields.eq(1).val(a.r).end().eq(2).val(a.g).end().eq(3).val(a.b).end()},t=function(a,b){d(b).data("colorpicker").fields.eq(4).val(a.h).end().eq(5).val(a.s).end().eq(6).val(a.b).end()},n=function(a,b){d(b).data("colorpicker").fields.eq(0).val(k(h(a))).end()},
u=function(a,b){d(b).data("colorpicker").selector.css("backgroundColor","#"+k(h({h:a.h,s:100,b:100})));d(b).data("colorpicker").selectorIndic.css({left:parseInt(150*a.s/100,10),top:parseInt(150*(100-a.b)/100,10)})},v=function(a,b){d(b).data("colorpicker").hue.css("top",parseInt(150-150*a.h/360,10))},x=function(a,b){d(b).data("colorpicker").currentColor.css("backgroundColor","#"+k(h(a)))},w=function(a,b){d(b).data("colorpicker").newColor.css("backgroundColor","#"+k(h(a)))},N=function(a){a=a.charCode||
a.keyCode||-1;if(a>B&&90>=a||32==a)return!1;!0===d(this).parent().parent().data("colorpicker").livePreview&&p.apply(this)},p=function(a){var b=d(this).parent().parent();if(0<this.parentNode.className.indexOf("_hex")){var c=b.data("colorpicker");var e=this.value,f=6-e.length;if(0<f){for(var g=[],q=0;q<f;q++)g.push("0");g.push(e);e=g.join("")}c.color=c=l(y(e))}else 0<this.parentNode.className.indexOf("_hsb")?b.data("colorpicker").color=c=z({h:parseInt(b.data("colorpicker").fields.eq(4).val(),10),s:parseInt(b.data("colorpicker").fields.eq(5).val(),
10),b:parseInt(b.data("colorpicker").fields.eq(6).val(),10)}):(c=b.data("colorpicker"),e=parseInt(b.data("colorpicker").fields.eq(1).val(),10),f=parseInt(b.data("colorpicker").fields.eq(2).val(),10),g=parseInt(b.data("colorpicker").fields.eq(3).val(),10),c.color=c=l({r:Math.min(255,Math.max(0,e)),g:Math.min(255,Math.max(0,f)),b:Math.min(255,Math.max(0,g))}));a&&(m(c,b.get(0)),n(c,b.get(0)),t(c,b.get(0)));u(c,b.get(0));v(c,b.get(0));w(c,b.get(0));b.data("colorpicker").onChange.apply(b,[c,k(h(c)),h(c)])},
O=function(a){d(this).parent().parent().data("colorpicker").fields.parent().removeClass("colorpicker_focus")},P=function(){B=0<this.parentNode.className.indexOf("_hex")?70:65;d(this).parent().parent().data("colorpicker").fields.parent().removeClass("colorpicker_focus");d(this).parent().addClass("colorpicker_focus")},Q=function(a){var b=d(this).parent().find("input").trigger("focus");a={el:d(this).parent().addClass("colorpicker_slider"),max:0<this.parentNode.className.indexOf("_hsb_h")?360:0<this.parentNode.className.indexOf("_hsb")?
100:255,y:a.pageY,field:b,val:parseInt(b.val(),10),preview:d(this).parent().parent().data("colorpicker").livePreview};d(document).on("mouseup",a,C);d(document).on("mousemove",a,D)},D=function(a){a.data.field.val(Math.max(0,Math.min(a.data.max,parseInt(a.data.val+a.pageY-a.data.y,10))));a.data.preview&&p.apply(a.data.field.get(0),[!0]);return!1},C=function(a){p.apply(a.data.field.get(0),[!0]);a.data.el.removeClass("colorpicker_slider").find("input").trigger("focus");d(document).off("mouseup",C);d(document).off("mousemove",
D);return!1},R=function(a){a={cal:d(this).parent(),y:d(this).offset().top};a.preview=a.cal.data("colorpicker").livePreview;d(document).on("mouseup",a,E);d(document).on("mousemove",a,F)},F=function(a){p.apply(a.data.cal.data("colorpicker").fields.eq(4).val(parseInt(360*(150-Math.max(0,Math.min(150,a.pageY-a.data.y)))/150,10)).get(0),[a.data.preview]);return!1},E=function(a){m(a.data.cal.data("colorpicker").color,a.data.cal.get(0));n(a.data.cal.data("colorpicker").color,a.data.cal.get(0));d(document).off("mouseup",
E);d(document).off("mousemove",F);return!1},S=function(a){var b={cal:d(this).parent(),pos:d(this).offset()};b.preview=b.cal.data("colorpicker").livePreview;d(document).on("mouseup",b,G);d(document).on("mousemove",b,A);a.data=b;A(a)},H=-1,I=-1,A=function(a){if(H!=a.pageX||I!=a.pageY)return H=a.pageX,I=a.pageY,p.apply(a.data.cal.data("colorpicker").fields.eq(6).val(parseInt(100*(150-Math.max(0,Math.min(150,a.pageY-a.data.pos.top)))/150,10)).end().eq(5).val(parseInt(100*Math.max(0,Math.min(150,a.pageX-
a.data.pos.left))/150,10)).get(0),[a.data.preview]),!1},G=function(a){m(a.data.cal.data("colorpicker").color,a.data.cal.get(0));n(a.data.cal.data("colorpicker").color,a.data.cal.get(0));d(document).off("mouseup",G);d(document).off("mousemove",A);return!1},T=function(a){d(this).addClass("colorpicker_focus")},U=function(a){d(this).removeClass("colorpicker_focus")},V=function(a){d(this).addClass("colorpicker_focus")},W=function(a){d(this).removeClass("colorpicker_focus")},X=function(a){a=d(this).parent();
var b=a.data("colorpicker").color;a.data("colorpicker").origColor=b;x(b,a.get(0));a.data("colorpicker").onSubmit(b,k(h(b)),h(b),a.data("colorpicker").el);d(a.data("colorpicker").el).ColorPickerHide()},K=function(a){var b=d("#"+d(this).data("colorpickerId"));b.data("colorpicker").onBeforeShow.apply(this,[b.get(0)]);var c=d(this).offset(),e="CSS1Compat"==document.compatMode;a=window.pageXOffset||(e?document.documentElement.scrollLeft:document.body.scrollLeft);var f=window.pageYOffset||(e?document.documentElement.scrollTop:
document.body.scrollTop);var g=window.innerWidth||(e?document.documentElement.clientWidth:document.body.clientWidth);var q=c.top+this.offsetHeight;c=c.left;q+176>f+(window.innerHeight||(e?document.documentElement.clientHeight:document.body.clientHeight))&&(q-=this.offsetHeight+176);c+356>a+g&&(c-=356);b.css({left:c+"px",top:q+"px"});0!=b.data("colorpicker").onShow.apply(this,[b.get(0)])&&b.show();d(document).on("mousedown",{cal:b},J);return!1},J=function(a){Y(a.data.cal.get(0),a.target,a.data.cal.get(0))||
(0!=a.data.cal.data("colorpicker").onHide.apply(this,[a.data.cal.get(0)])&&a.data.cal.hide(),d(document).off("mousedown",J))},Y=function(a,b,c){if(a==b)return!0;if(a.contains)return a.contains(b);if(a.compareDocumentPosition)return!!(a.compareDocumentPosition(b)&16);for(b=b.parentNode;b&&b!=c;){if(b==a)return!0;b=b.parentNode}return!1},z=function(a){return{h:Math.min(360,Math.max(0,a.h)),s:Math.min(100,Math.max(0,a.s)),b:Math.min(100,Math.max(0,a.b))}},y=function(a){a=parseInt(-1<a.indexOf("#")?a.substring(1):
a,16);return{r:a>>16,g:(a&65280)>>8,b:a&255}},l=function(a){var b={h:0,s:0,b:0},c=Math.max(a.r,a.g,a.b),e=c-Math.min(a.r,a.g,a.b);b.b=c;b.s=0!=c?255*e/c:0;b.h=0!=b.s?a.r==c?(a.g-a.b)/e:a.g==c?2+(a.b-a.r)/e:4+(a.r-a.g)/e:-1;b.h*=60;0>b.h&&(b.h+=360);b.s*=100/255;b.b*=100/255;return b},h=function(a){var b,c;var e=Math.round(a.h);var f=Math.round(255*a.s/100);a=Math.round(255*a.b/100);if(0==f)e=b=c=a;else{f=(255-f)*a/255;var g=e%60*(a-f)/60;360==e&&(e=0);60>e?(e=a,c=f,b=f+g):120>e?(b=a,c=f,e=a-g):180>
e?(b=a,e=f,c=f+g):240>e?(c=a,e=f,b=a-g):300>e?(c=a,b=f,e=f+g):360>e?(e=a,b=f,c=a-g):c=b=e=0}return{r:Math.round(e),g:Math.round(b),b:Math.round(c)}},k=function(a){var b=[a.r.toString(16),a.g.toString(16),a.b.toString(16)];d.each(b,function(c,e){1==e.length&&(b[c]="0"+e)});return b.join("")},L=function(){var a=d(this).parent(),b=a.data("colorpicker").origColor;a.data("colorpicker").color=b;m(b,a.get(0));n(b,a.get(0));t(b,a.get(0));u(b,a.get(0));v(b,a.get(0));w(b,a.get(0));a.data("colorpicker").onChange.apply(a,
[b,k(h(b)),h(b)])};return{init:function(a){a=d.extend({},M,a||{});if("string"==typeof a.color)if(-1!=a.color.indexOf("rgb(")){var b=a.color.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);a.color=l({r:parseInt(b[1]),g:parseInt(b[2]),b:parseInt(b[3])})}else a.color=l(y(a.color));else if(void 0!=a.color.r&&void 0!=a.color.g&&void 0!=a.color.b)a.color=l(a.color);else if(void 0!=a.color.h&&void 0!=a.color.s&&void 0!=a.color.b)a.color=z(a.color);else return this;return this.each(function(){if(!d(this).data("colorpickerId")){var c=
d.extend({},a);c.origColor=a.color;var e="collorpicker_"+parseInt(1E3*Math.random());d(this).data("colorpickerId",e);e=d('<div class="colorpicker"><div class="colorpicker_color"><div><div></div></div></div><div class="colorpicker_hue"><div></div></div><div class="colorpicker_new_color"></div><div class="colorpicker_current_color"></div><div class="colorpicker_hex"><input type="text" maxlength="6" size="6" /></div><div class="colorpicker_rgb_r colorpicker_field"><input type="text" maxlength="3" size="3" /><span></span></div><div class="colorpicker_rgb_g colorpicker_field"><input type="text" maxlength="3" size="3" /><span></span></div><div class="colorpicker_rgb_b colorpicker_field"><input type="text" maxlength="3" size="3" /><span></span></div><div class="colorpicker_hsb_h colorpicker_field"><input type="text" maxlength="3" size="3" /><span></span></div><div class="colorpicker_hsb_s colorpicker_field"><input type="text" maxlength="3" size="3" /><span></span></div><div class="colorpicker_hsb_b colorpicker_field"><input type="text" maxlength="3" size="3" /><span></span></div><div class="colorpicker_cancel"></div><div class="colorpicker_submit"></div></div>').attr("id",
e);c.flat?e.appendTo(this).show():e.appendTo(document.body);c.fields=e.find("input").on("keyup",N).on("change",p).on("blur",O).on("focus",P);e.find("span").on("mousedown",Q).end().find(">div.colorpicker_current_color").on("click",L);c.selector=e.find("div.colorpicker_color").on("mousedown",S);c.selectorIndic=c.selector.find("div div");c.el=this;c.hue=e.find("div.colorpicker_hue div");e.find("div.colorpicker_hue").on("mousedown",R);c.newColor=e.find("div.colorpicker_new_color");c.currentColor=e.find("div.colorpicker_current_color");
e.data("colorpicker",c);e.find("div.colorpicker_submit").on("mouseenter",T).on("mouseleave",U).on("click",X);e.find("div.colorpicker_cancel").on("mouseenter",V).on("mouseleave",W).on("click",function(){var f=d(this).parent();L.apply(this);d(f.data("colorpicker").el).ColorPickerHide()});m(c.color,e.get(0));t(c.color,e.get(0));n(c.color,e.get(0));v(c.color,e.get(0));u(c.color,e.get(0));x(c.color,e.get(0));w(c.color,e.get(0));if(c.flat)e.css({position:"relative",display:"block"});else d(this).on(c.eventName,
K)}})},showPicker:function(){return this.each(function(){d(this).data("colorpickerId")&&K.apply(this)})},hidePicker:function(){return this.each(function(){d(this).data("colorpickerId")&&d("#"+d(this).data("colorpickerId")).hide()})},setColor:function(a){if("string"==typeof a)if(-1!=a.indexOf("rgb(")){var b=a.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);a=l({r:parseInt(b[1]),g:parseInt(b[2]),b:parseInt(b[3])})}else a=l(y(a));else if(void 0!=a.r&&void 0!=a.g&&void 0!=a.b)a=l(a);else if(void 0!=a.h&&void 0!=
a.s&&void 0!=a.b)a=z(a);else return this;return this.each(function(){if(d(this).data("colorpickerId")){var c=d("#"+d(this).data("colorpickerId"));c.data("colorpicker").color=a;c.data("colorpicker").origColor=a;m(a,c.get(0));t(a,c.get(0));n(a,c.get(0));v(a,c.get(0));u(a,c.get(0));x(a,c.get(0));w(a,c.get(0))}})},getColor:function(){if(d(this).data("colorpickerId")){var a=d("#"+d(this).data("colorpickerId")).data("colorpicker").color,b=h(a);return{rgb:b,hex:k(b),hsb:a}}}}}();d.fn.extend({ColorPicker:r.init,
ColorPickerHide:r.hidePicker,ColorPickerShow:r.showPicker,ColorPickerSetColor:r.setColor,ColorPickerGetColor:r.getColor})})(jQuery);
