/*
 * XenForo color_picker.min.js
 * Copyright 2010-2014 XenForo Ltd.
 * Released under the XenForo License Agreement: http://xenforo.com/license-agreement
 */
(function(d,r,k){XenForo.ColorPicker=function(a){this.__construct(a)};XenForo.ColorPicker._formInit={};XenForo.ColorPicker.prototype={__construct:function(a){var b=a.closest("form"),c=b.attr("id");c||(c=XenForo.uniqueId(),b.attr("id",c));!XenForo.ColorPicker._formInit[c]&&!b.data("XenForo.ColorPickerForm")&&(b.bind("reset",function(){setTimeout(function(){b.find("input.ColorPicker").each(function(a,b){d(b).data("XenForo.ColorPicker").updateValue(b.value)})},100)}),b.data("XenForo.ColorPickerForm",
!0),XenForo.ColorPicker._formInit[c]=!0);this.$form=b;this.$input=a;this.$placeholder=d("<span />").addClass("colorPickerPlaceholder textCtrl").prop("unselectable",!0).insertAfter(a);this.$placeholder.append("<span>&nbsp;</span>");a.hide();this.$placeholder.click(d.context(this,"openColorPicker"));this.setPlaceholderColor()},openColorPicker:function(){this.$placeholder.blur();m||(m=new o);m.load(d.context(this,"updateValue"),this.$input.val(),this.$input.hasClass("DisablePalette")?!1:!0)},updateValue:function(a){this.$input.val(a);
this.setPlaceholderColor()},updateRgbaValue:function(a,b,c,h){h===void 0||h==1?this.updateValue("rgb("+a+","+b+","+c+")"):this.updateValue("rgba("+a+","+b+","+c+","+h+")")},setPlaceholderColor:function(){var a=this.$input.val(),b=g(a);b?(this.$placeholder.css("border-style",""),b.unknown?this.$placeholder.find("span").css("background-color","").addClass("undefined"):this.setPlaceholderRgb(b.r,b.g,b.b).removeClass("undefined"),this.currentHsva=p(b.r,b.g,b.b),this.currentHsva.a=b.a):(this.$placeholder.css("border-style",
"dashed"),this.$placeholder.find("span").css("background-color","transparent").removeClass("undefined"),this.currentHsva=null);this.$placeholder.attr("title",a)},setPlaceholderRgb:function(a,b,c){return this.$placeholder.find("span").css("background-color","rgb("+a+", "+b+", "+c+")")}};XenForo.ColorInvert=function(a){a.click(function(){d(a.data("target")).find("input.ColorPicker").each(function(a,c){var h=g(c.value);d(c).data("XenForo.ColorPicker").updateRgbaValue(255-h.r,255-h.g,255-h.b,h.a)})})};
XenForo.HueShift=function(a){var b=function(b,c){var e=c-a.data("hueshift");a.data("hueshift",c);d(a.data("target")).find("input.ColorPicker").each(function(a,c){var f=d(c).data("XenForo.ColorPicker"),i,g;if(i=f.currentHsva)i.h=(i.h+e)%360,i.h<0&&(i.h+=360),g=l(i.h,i.s,i.v),b.type=="change"?f.updateRgbaValue(g.r,g.g,g.b,i.a):f.setPlaceholderRgb(g.r,g.g,g.b)})};a.data("hueshift",a.val());a.rangeinput({change:b,onSlide:b,css:{input:"range textCtrl",handle:"handle"}});var c=a.closest("form");c.data("XenForo.HueShiftForm")||
(c.bind("reset",function(){setTimeout(function(){c.find("input.HueShift").each(function(a,b){d(b).data("rangeinput").setValue(b.value)})},50)}),c.data("XenForo.HueShiftForm",!0));this.$form=c;a.data("rangeinput").getHandle().attr("title",a.attr("title"))};XenForo.ColorPicker.namedColors={aliceblue:"f0f8ff",antiquewhite:"faebd7",aqua:"00ffff",aquamarine:"7fffd4",azure:"f0ffff",beige:"f5f5dc",bisque:"ffe4c4",black:"000000",blanchedalmond:"ffebcd",blue:"0000ff",blueviolet:"8a2be2",brown:"a52a2a",burlywood:"deb887",
cadetblue:"5f9ea0",chartreuse:"7fff00",chocolate:"d2691e",coral:"ff7f50",cornflowerblue:"6495ed",cornsilk:"fff8dc",crimson:"dc143c",cyan:"00ffff",darkblue:"00008b",darkcyan:"008b8b",darkgoldenrod:"b8860b",darkgray:"a9a9a9",darkgreen:"006400",darkkhaki:"bdb76b",darkmagenta:"8b008b",darkolivegreen:"556b2f",darkorange:"ff8c00",darkorchid:"9932cc",darkred:"8b0000",darksalmon:"e9967a",darkseagreen:"8fbc8f",darkslateblue:"483d8b",darkslategray:"2f4f4f",darkturquoise:"00ced1",darkviolet:"9400d3",deeppink:"ff1493",
deepskyblue:"00bfff",dimgray:"696969",dodgerblue:"1e90ff",firebrick:"b22222",floralwhite:"fffaf0",forestgreen:"228b22",fuchsia:"ff00ff",gainsboro:"dcdcdc",ghostwhite:"f8f8ff",gold:"ffd700",goldenrod:"daa520",gray:"808080",green:"008000",greenyellow:"adff2f",honeydew:"f0fff0",hotpink:"ff69b4",indianred:"cd5c5c",indigo:"4b0082",ivory:"fffff0",khaki:"f0e68c",lavender:"e6e6fa",lavenderblush:"fff0f5",lawngreen:"7cfc00",lemonchiffon:"fffacd",lightblue:"add8e6",lightcoral:"f08080",lightcyan:"e0ffff",lightgoldenrodyellow:"fafad2",
lightgrey:"d3d3d3",lightgreen:"90ee90",lightpink:"ffb6c1",lightsalmon:"ffa07a",lightseagreen:"20b2aa",lightskyblue:"87cefa",lightslategray:"778899",lightsteelblue:"b0c4de",lightyellow:"ffffe0",lime:"00ff00",limegreen:"32cd32",linen:"faf0e6",magenta:"ff00ff",maroon:"800000",mediumaquamarine:"66cdaa",mediumblue:"0000cd",mediumorchid:"ba55d3",mediumpurple:"9370d8",mediumseagreen:"3cb371",mediumslateblue:"7b68ee",mediumspringgreen:"00fa9a",mediumturquoise:"48d1cc",mediumvioletred:"c71585",midnightblue:"191970",
mintcream:"f5fffa",mistyrose:"ffe4e1",moccasin:"ffe4b5",navajowhite:"ffdead",navy:"000080",oldlace:"fdf5e6",olive:"808000",olivedrab:"6b8e23",orange:"ffa500",orangered:"ff4500",orchid:"da70d6",palegoldenrod:"eee8aa",palegreen:"98fb98",paleturquoise:"afeeee",palevioletred:"d87093",papayawhip:"ffefd5",peachpuff:"ffdab9",peru:"cd853f",pink:"ffc0cb",plum:"dda0dd",powderblue:"b0e0e6",purple:"800080",red:"ff0000",rosybrown:"bc8f8f",royalblue:"4169e1",saddlebrown:"8b4513",salmon:"fa8072",sandybrown:"f4a460",
seagreen:"2e8b57",seashell:"fff5ee",sienna:"a0522d",silver:"c0c0c0",skyblue:"87ceeb",slateblue:"6a5acd",slategray:"708090",snow:"fffafa",springgreen:"00ff7f",steelblue:"4682b4",tan:"d2b48c",teal:"008080",thistle:"d8bfd8",tomato:"ff6347",turquoise:"40e0d0",violet:"ee82ee",wheat:"f5deb3",white:"ffffff",whitesmoke:"f5f5f5",yellow:"ffff00",yellowgreen:"9acd32"};var m,g=function(a){var b=0,c=0,h=0,f=1,e="",q=!1,a=d.trim(a);if(a=="")return!1;if(e=a.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i))b=
parseInt(e[1],16),c=parseInt(e[2],16),h=parseInt(e[3],16),e=e[0];else if(e=a.match(/^#([0-9a-f])([0-9a-f])([0-9a-f])$/i))b=parseInt(e[1]+e[1],16),c=parseInt(e[2]+e[2],16),h=parseInt(e[3]+e[3],16),e=e[0];else if(e=a.match(/^rgb\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*\)$/i))b=e[1],c=e[2],h=e[3],e=e[0];else if(e=a.match(/^rgba\(\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9]+)\s*,\s*([0-9.]+)\s*\)$/i))b=e[1],c=e[2],h=e[3],f=e[4],e=e[0];else if(e=a.match(/^@(([a-z0-9_-]+)(\.[a-z0-9_-]+)?)$/i)){if(a=n()[e[1]]){if(b=
a.r,c=a.g,h=a.b,a.a!==void 0)f=a.a}else q=!0;e=e[0]}else return XenForo.ColorPicker.namedColors[a.toLowerCase()]?(e=g("#"+XenForo.ColorPicker.namedColors[a.toLowerCase()]),e.value=a,e):!1;return{r:b,g:c,b:h,a:f,value:e,unknown:q}},j=!1,n=function(){if(j!==!1)return j;j={};d("#ColorPickerInstance").find(".PaletteTab li").each(function(){var a=d(this),b=g(a.data("colorcss")),c=a.data("colorname"),h=d("<div />").addClass("swatch");b&&(h.css("background-color","rgb("+b.r+", "+b.g+", "+b.b+")").prependTo(a),
j[c]=b)});return j},p=function(a,b,c){var d,f,e;if(typeof a=="object")b=a.g,c=a.b,a=a.r;a/=255;b/=255;c/=255;d=Math.max(a,b,c);f=Math.min(a,b,c);f=d-f;if(f==0)a=e=0;else{switch(d){case a:e=(b-c)/f%6;break;case b:e=(c-a)/f+2;break;case c:e=(a-b)/f+4}e=Math.round(60*e);e<0&&(e+=360);a=f/d}return{h:e,s:a,v:d}},l=function(a,b,c){var d,f,e,g;if(typeof a=="object")b=a.s,c=a.v,a=a.h;b*=c;a/=60;d=b*(1-Math.abs(a%2-1));a<1?(f=b,e=d,g=0):a<2?(f=d,e=b,g=0):a<3?(f=0,e=b,g=d):a<4?(f=0,e=d,g=b):a<5?(f=d,e=0,g=
b):a<6&&(f=b,e=0,g=d);c-=b;return{r:Math.round(255*(f+c)),g:Math.round(255*(e+c)),b:Math.round(255*(g+c))}},o=function(){this.__construct()};o.prototype={__construct:function(){this.$picker=d("#ColorPickerInstance").appendTo(k.body);this.$picker.overlay({close:".OverlayCloser",speed:0,closeSpeed:0,mask:{color:"white",opacity:0.6,loadSpeed:0,closeSpeed:0}});this.api=this.$picker.data("overlay");this.hsv={h:0,s:0,v:0};this.tempEvents={};n();this.$paletteItems=this.$picker.find(".PaletteTab li");this.$paletteItems.each(d.context(this,
"initPaletteItem"));this.$gradient=this.$picker.find(".gradient");this.$gradientCircle=this.$gradient.find(".circle");this.$bar=this.$picker.find(".bar");this.$barArrow=this.$bar.find(".arrow");this.$preview=this.$picker.find(".preview");this.$finalValue=this.$picker.find("input.textCtrl.finalValue");this.$remove=this.$picker.find(".remove input");var a=this.$picker.find(".inputs");this.inputs={r:a.find('input[name="r"]'),g:a.find('input[name="g"]'),b:a.find('input[name="b"]'),a:a.find('input[name="a"]')};
this.$hex=a.find('input[name="hex"]');this.$gradient.mousedown(d.context(this,"eGradientMouseDown"));this.$bar.mousedown(d.context(this,"eBarMouseDown"));this.inputs.a.change(d.context(this,"normalizeAlpha"));a.find('input[name="r"], input[name="g"], input[name="b"], input[name="a"]').change(d.context(this,"eChangeIndividualInput"));this.$hex.change(d.context(this,"eChangeHexInput"));this.$finalValue.change(d.context(this,"eChangeFinalInput"));this.$remove.click(d.context(this,"eRemoveClick"));this.$picker.find(".save").click(d.context(this,
"save"));this.$picker.find("input[type=text]").keydown(d.context(function(a){a.keyCode==13&&(a.preventDefault(),a.target.blur(),this.save())},this));this.tabs=new XenForo.Tabs(this.$picker.find(".ColorPickerTabs .tabs"))},initPaletteItem:function(a,b){d(b).click(d.context(this,"ePaletteItemClick"))},load:function(a,b,c){this.saveCallback=a;this.updateFromString(b);this.updateCurrentColor(b);this.updatePreview(!0);!c||!this.$paletteItems.length?(this.tabs.click(1),this.$picker.find(".ColorPickerTabs li:eq(0)").hide()):
(this.$picker.find(".ColorPickerTabs li:eq(0)").show(),b&&b.substr(0,1)!=="@"?this.tabs.click(1):this.tabs.click(0));this.api.load()},updateFromString:function(a){a=d.trim(a);if(a=="")this.$remove.prop("checked",!0),this.updateInputs("","","","");else if(a=g(a))a.unknown?this.updateInputs("","","",""):(this.updateInputs(a),this.normalizeAlpha()),this.$finalValue.val(a.value),a.value.substr(0,1)=="@"&&this.selectPaletteItem(a.value.substr(1))},selectPaletteItem:function(a){this.$paletteItems.each(function(){var b=
d(this);b.data("colorname")==a?(b.addClass("selected"),selected=a):b.removeClass("selected")})},matchColorsFromString:g,getRgbFromInputs:function(){var a=parseInt(this.inputs.r.val(),10),b=parseInt(this.inputs.g.val(),10),c=parseInt(this.inputs.b.val(),10),d=!1;isNaN(a)&&isNaN(b)&&isNaN(c)&&(d=!0);a=isNaN(a)?0:a;b=isNaN(b)?0:b;c=isNaN(c)?0:c;return{r:a,g:b,b:c,invalid:d}},getCssColorFromInputs:function(a){var b=this.getRgbFromInputs(),c=this.inputs.a.val();return b.invalid?"":a&&parseFloat(c)<1?"rgba("+
b.r+", "+b.g+", "+b.b+", "+c+")":"rgb("+b.r+", "+b.g+", "+b.b+")"},normalizeAlpha:function(){alpha=parseFloat(this.inputs.a.val());if(alpha>1)alpha=String(alpha/255).substr(0,4);else if(alpha<0)alpha=0;else return!1;this.inputs.a.val(alpha);return!0},ePaletteItemClick:function(a){var a=d(a.currentTarget).data("colorname"),b;if(a&&(b=n()[a]))this.updateInputs(b),this.$finalValue.val("@"+a),this.selectPaletteItem(a),this.updatePreview(!0)},eGradientMouseDown:function(a){a.preventDefault();this.handleGradientEventUpdate(a);
this.tempEvents={mousemove:d.context(this,"eGradientMouseMove"),mouseup:d.context(this,"eGradientMouseUp")};d(k).bind(this.tempEvents)},eGradientMouseMove:function(a){this.handleGradientEventUpdate(a)},eGradientMouseUp:function(){this.unbindTempEvents()},handleGradientEventUpdate:function(a){var b=this.$gradient.offset(),c;c=a.pageX-b.left-1;a=a.pageY-b.top-1;this.hsv.s=Math.max(0,Math.min(1,c/255));this.hsv.v=Math.max(0,Math.min(1,(255-a)/255));this.updateInputs(l(this.hsv),!1);this.updatePreview(!0)},
unbindTempEvents:function(){for(var a in this.tempEvents)d(k).unbind(a,this.tempEvents[a]);this.tempEvents={}},eBarMouseDown:function(a){a.preventDefault();this.handleBarEventUpdate(a);this.tempEvents={mousemove:d.context(this,"eBarMouseMove"),mouseup:d.context(this,"eBarMouseUp")};d(k).bind(this.tempEvents)},eBarMouseMove:function(a){this.handleBarEventUpdate(a)},eBarMouseUp:function(){this.unbindTempEvents()},handleBarEventUpdate:function(a){var b=this.$bar.offset();this.hsv.h=Math.max(0,Math.min(359,
Math.round((255-(a.pageY-b.top-1))/255*360)));this.updateInputs(l(this.hsv),!1);this.updatePreview(!0)},eChangeFinalInput:function(){this.$remove.prop("checked",!1);this.updateFromString(this.$finalValue.val());this.updatePreview(!0)},eChangeIndividualInput:function(a){a=d(a.currentTarget);a.val()>255?a.val(255):a.val()<0&&a.val(0);this.updateHexFromInputs();this.updateFinalValueFromInputs();this.updateHsv();this.updatePreview(!0)},eChangeHexInput:function(){this.updateInputsFromHex();this.updateFinalValueFromInputs();
this.updateHsv();this.updatePreview(!0)},eRemoveClick:function(){this.$remove.prop("checked")?(this.$finalValue.val(""),this.selectPaletteItem(!1)):(this.updateFinalValueFromInputs(),this.updateHsv());this.updatePreview(!0)},updateCurrentColor:function(a){var a=g(a),b=this.$picker.find(".currentPreview");!a||a.unknown?b.css("background","transparent"):b.css("background","rgb("+a.r+", "+a.g+", "+a.b+")")},updatePreview:function(a){var b=this.getCssColorFromInputs(!1);b==""||this.$remove.prop("checked")?
this.$preview.css("background","transparent"):this.$preview.css("background",b);a&&this.updatePicker()},updatePicker:function(){var a,b,c;a=Math.round(this.hsv.s*255+1);b=Math.round(255-this.hsv.v*255+1);c=Math.round((359-this.hsv.h)/359*255+1);this.$gradientCircle.css({top:b,left:a});this.$barArrow.css("top",c);a=l({h:this.hsv.h,s:1,v:1});this.$gradient.css("background-color","rgb("+a.r+", "+a.g+", "+a.b+")")},updateInputs:function(a,b,c,d,f){f===void 0&&(f=!0);if(typeof a=="object"){b!==void 0&&
(f=b);b=a.g;c=a.b;if(a.a!==void 0)d=a.a;a=a.r}this.inputs.r.val(a);this.inputs.g.val(b);this.inputs.b.val(c);a!==""&&b!==""&&c!==""?this.$hex.val(this._toHexComponent(a)+this._toHexComponent(b)+this._toHexComponent(c)):this.$hex.val("");d!==void 0&&(this.inputs.a.val(d),this.normalizeAlpha());this.updateFinalValueFromInputs();f&&this.updateHsv()},_toHexComponent:function(a){a=parseInt(a,10);return isNaN(a)?"":(a=a.toString(16),a.length==1&&(a="0"+a),a)},updateFinalValueFromInputs:function(){this.$finalValue.val(this.getCssColorFromInputs(!0));
this.$finalValue.val()&&this.$remove.prop("checked",!1);this.selectPaletteItem(!1)},updateInputsFromHex:function(){var a=this.$hex.val();a===""?this.updateInputs("","","",""):(a=g(a.substr(0,1)=="#"?a:"#"+a))?this.updateInputs(a):(this.$hex.val(""),this.updateInputs("","","",""))},updateHexFromInputs:function(){var a=this.inputs.r.val(),b=this.inputs.g.val(),c=this.inputs.b.val();a!==""&&b!==""&&c!==""?this.$hex.val(this._toHexComponent(a)+this._toHexComponent(b)+this._toHexComponent(c)):this.$hex.val("")},
updateHsv:function(){this.hsv=p(this.getRgbFromInputs())},save:function(){var a=d.trim(this.$finalValue.val());this.$remove.prop("checked")&&(a="");this.saveCallback&&this.saveCallback(a);this.api.close()}};XenForo.register("input.ColorPicker","XenForo.ColorPicker");XenForo.register("input.ColorInvert","XenForo.ColorInvert");XenForo.register("input.HueShift","XenForo.HueShift")})(jQuery,this,document);
