/*
 * @author    RocketTheme http://www.rockettheme.com
 * @copyright Copyright (C) 2007 - 2012 RocketTheme, LLC
 * @license   http://www.gnu.org/licenses/gpl-2.0.html GNU/GPLv2 only
 */
((function(){var a=this.MooRainbow=new Class({Implements:[Options,Events],options:{data:"moorainbow",startColor:"#f00",transparency:1,activeForm:"hsb",forms:{rgb:["red","green","blue"],hsb:["hue","saturation","brightness"]},defaults:{relative:null,position:"right",offset:{x:10,y:0}},onChange:function(d,c){if(!c){return;
}var b;if(c.get("data-moorainbow-trigger")){b=c;c=document.id(c.get("data-moorainbow-trigger"));}else{b=document.getElements("[data-moorainbow-trigger="+c.get("id")+"]");
}c.set("value",this.sets.hex);if(this.sets.hex!="transparent"){b.getElement(".overlay").removeClass("overlay-transparent");}b.getElement(".overlay").setStyle("backgroundColor",this.sets.hex);
},onComplete:function(){}},initialize:function(b){this.setOptions(b);this.data="data-"+this.options.data;this.sets={rgb:[],hsb:[],hex:""};this.coords={overlay:{x:0,y:0},slider:0};
this.offsets={overlay:0,slider:0};if(!this.layout){this.build();}this.attach();this.setColor(this.options.startColor.hexToRgb().rgbToHsb(),"force");this.hide();
},attach:function(){var c=this.layout.retrieve("moor:buttontype:click",function(j,i){this.buttonTypeClick.call(this,j,i);}.bind(this)),d=this.layout.retrieve("moor:trigger:click",function(j,i){this.toggle.call(this,j,i);
}.bind(this)),f=document.retrieve("moor:smarttoggle:click",function(j,i){this.smartToggle.call(this,j,i);}.bind(this)),g=this.layout.retrieve("moor:overlay:mousedown",function(j,i){this.overlayMouseDown.call(this,j,i);
}.bind(this)),e=this.layout.retrieve("moor:slider:mousedown",function(j,i){this.sliderMouseDown.call(this,j,i);}.bind(this)),h=this.layout.retrieve("moor:backupcolor:click",function(k,j){var i=j.retrieve("moor:backup:color",this.options.startColor);
if(i!="transparent"){this.setColor.call(this,i,"force");}else{this.triggerTransparent.call(this,k,j);}}.bind(this)),b=this.layout.retrieve("moor:element:keyup",function(j,i){this.keyup.call(this,j,i);
}.bind(this));this.layout.addEvents({"click:relay([data-moorainbow-button-type])":c,"click:relay(.moor-colorsbox-selected)":h,"keyup:relay([data-moorainbow-input] input)":b,"mousedown:relay([data-moorainbow-overlay])":g,"mousedown:relay([data-moorainbow-slider])":e});
document.addEvents({"mousedown:relay([data-moorainbow-trigger])":d,"focus:relay([data-moorainbow])":d,"keyup:relay([data-moorainbow])":b});if(!document.retrieve("moor:document:events")){document.addEvent("click",f);
document.store("moor:document:events",true);}this.attachDrag();},attachDrag:function(){var d=this.layout.getElement("[data-moorainbow-overlay]"),f=this.layout.getElement("[data-moorainbow-slider]"),c=d.getComputedSize(),b=f.getComputedSize(),e;
this.overlayDragInstance=new Drag(this.overlayCursor,{limit:{x:[0-this.offsets.overlay,c.width-this.offsets.overlay],y:[0-this.offsets.overlay,c.height-this.offsets.overlay]},snap:0});
this.sliderDragInstance=new Drag(this.sliderCursor,{limit:{y:[0-this.offsets.slider,b.height-this.offsets.slider]},modifiers:{x:false},snap:0});e={onBeforeStart:this.setColor.bind(this),onStart:this.setColor.bind(this),onDrag:this.setColor.bind(this)};
this.overlayDragInstance.addEvents(e);this.sliderDragInstance.addEvents(e);},overlayMouseDown:function(e,c){var b=this.layout.getElement("[data-moorainbow-overlay]"),d=b.getPosition(),f={x:b.getStyle("border-left").toInt()+b.getStyle("border-right").toInt(),y:b.getStyle("border-top").toInt()+b.getStyle("border-bottom").toInt()};
this.overlayCursor.style.left=e.page.x-d.x-this.offsets.overlay-f.x+"px";this.overlayCursor.style.top=e.page.y-d.y-this.offsets.overlay-f.y+"px";this.overlayDragInstance.start(e);
},sliderMouseDown:function(e,c){var d=this.layout.getElement("[data-moorainbow-slider]"),b=d.getPosition().y;sliderBorders=d.getStyle("border-top").toInt()+d.getStyle("border-bottom").toInt();
this.sliderCursor.style.top=e.page.y-b-this.offsets.slider-sliderBorders+"px";this.sliderDragInstance.start(e);},toggle:function(f,d){var c=null,e=this.active?this.active.get("data-moorainbow-trigger"):null,b=d.get("data-moorainbow-trigger");
if(this.isShown&&this.active==d){return;}if(this.isShown&&e&&e==d.get("id")){return;}if(this.isShown&&this.active&&b&&b==this.active.get("id")){return;
}this.active=d;this.show();if(!d.get("data-moorainbow-trigger")&&document.getElement("[data-moorainbow-trigger="+d.get("id")+"]")){c=d.get("value");d=document.getElement("[data-moorainbow-trigger="+d.get("id")+"]");
}if(!c){c=document.id(d.get("data-moorainbow-trigger")).get("value");}this.backupColor.setStyle("background-color",c).store("moor:backup:color",c);this.reposition({relative:d});
if(c!="transparent"){this.setColor(c,"force");}},smartToggle:function(c,b){if(c){var d=c.target;if(d.get("data-moorainbow")!==null||d.get("data-moorainbow-trigger")!==null||d.get("data-moorainbow-layout")!==null||d.getParent("[data-moorainbow]")||d.getParent("[data-moorainbow-trigger]")||d.getParent("[data-moorainbow-layout]")){return;
}else{this.hide();}}},reposition:function(c){var d,b,g,e={x:0,y:0},f=this.options.defaults;f=Object.merge(f,c);d=f.relative;b=f.position;g=f.offset;if(!d){return;
}switch(b){case"left":e.x=0-this.layout.getSize().x;g.x*=-1;break;case"right":e.x=d.getSize().x;g.x*=1;break;case"top":e.y=0-this.layout.getSize().y;g.x*=-1;
break;case"bottom":e.y=d.getSize().y;g.x*=1;break;}this.layout.style.left=d.getPosition().x+e.x+g.x+"px";this.layout.style.top=d.getPosition().y+e.y+g.y+"px";
},setColor:function(c,f){var d,b,e="hsb";if(typeOf(c)=="element"){if(c.hasClass("moor-slider-cursor")){d=true;}c=this._getHSB();}if(!c){c=this._getHSB();
}if(c[0]=="#"){e="rgb";c=c.hexToRgb(true);}if(f){this._updateCursors(c,e);}this.updateData(c,e);},updateData:function(e,g){g=g||"hsb";var d,c,b,f,h,i;switch(g){case"rgb":f=e;
b=e.rgbToHsb();h=e.rgbToHex();break;case"hsb":b=e||this._getHSB();f=b.hsbToRgb(true);h=this._hsbToHex(b);}i={red:f[0],green:f[1],blue:f[2],hue:b[0],saturation:b[1],brightness:b[2],hex:h};
this.sets={rgb:f,hsb:b,hex:h};c=new Color([this.sets.hsb[0],100,100]).hsbToRgb(true);["red","green","blue","hue","saturation","brightness","hex"].each(function(k,j){d=this.layout.getElement("[data-moorainbow-input-"+k+"]");
d.set("value",i[k]);},this);this.selectedColor.setStyle("backgroundColor",this.sets.hex);this.layout.getElement("[data-moorainbow-overlay]").setStyle("backgroundColor",c);
this.fireEvent("onChange",[this.sets,this.active],1);},hide:function(){this.layout.setStyles({display:"none",visibility:"hidden"});this.isShown=false;},show:function(){this.layout.setStyles({display:"block",visibility:"visible"});
this.isShown=true;},keyup:function(b,e){var j,d,g,f,i=[],h=e.value.match(/^(#[a-f0-9]{6}|[0-9]{1,3})$/ig),c;if(!h){return;}j=e.getParent("[data-moorainbow-input]:not([data-moorainbow-input=hex])");
if(!this._validateData(b,e)&&null===e.get("data-moorainbow")){return false;}c=h[0];if(j){d=j.get("data-moorainbow-input");Object.each(this.options.forms,function(l,k){if(l.contains(d)){g=k;
}},this);}if(g){this.options.forms[g].each(function(k){f=this.layout.getElement("[data-moorainbow-input-"+k+"]");i.push(f.get("value").toInt());},this);
if(g=="rgb"){c=i.rgbToHex();}else{c=i;}}this.setColor(c,"force");},build:function(){var h,g,j;var d=new Element("div.moor-layout[data-moorainbow-layout]").inject(document.body),c=new Element("div.moor-overlay[data-moorainbow-overlay]").adopt(new Element("div.moor-overlay-white"),new Element("div.moor-overlay-black"),new Element("div.moor-overlay-cursor")).inject(d),b=new Element("div.moor-slider[data-moorainbow-slider]").adopt(new Element("div.moor-slider-colors"),new Element("div.moor-slider-cursor")).inject(d),k=new Element("div.moor-colorsbox[data-moorainbow-colorsbox]").adopt(new Element("div.moor-colorsbox-current"),new Element("div.moor-colorsbox-selected")).inject(d),e=new Element("div.moor-inputs").inject(d),f=new Element("div.moor-buttons").inject(d),i=new Element("span.arrow").inject(d);
["red","green","blue","hue","saturation","brightness","hex"].each(function(l){g=(l!="hex")?l[0].capitalize():"# "+l;j=["saturation","brightness"].contains(l)?"%":(l=="hue")?"&deg;":"";
h=new Element("div.moor-inputs-block.moor-inputs-"+l+"[data-moorainbow-input="+l+"]").adopt(new Element("span",{html:g}),new Element("input[data-moorainbow-input-"+l+"]"));
if(j){h.adopt(new Element("span",{html:j}));}e.adopt(h);});["close","transparent","RGB","HSB"].each(function(l){g=l=="close"?"&times;":l=="transparent"?"":l;
new Element("div.moor-button.moor-button-"+l.toLowerCase()+"[data-moorainbow-button-type="+l.toLowerCase()+"]",{html:g}).inject(f);});this.overlayCursor=c.getElement(".moor-overlay-cursor");
this.sliderCursor=b.getElement(".moor-slider-cursor");this.selectedColor=d.getElement("div.moor-colorsbox-current");this.backupColor=d.getElement("div.moor-colorsbox-selected");
this.offsets={overlay:this.overlayCursor.getSize().x/2,slider:this.sliderCursor.getSize().y/2};this.layout=d;this.buttonTypeClick.call(this);},buttonTypeClick:function(d,b){var c=b?b.get("data-moorainbow-button-type")||this.options.activeForm:this.options.activeForm;
if(!this.options.forms[c]){var e=this["trigger"+c.capitalize()];return e?e.call(this,d,b):false;}Object.each(this.options.forms,function(f,g){if(g==c){this.layout.getElement("[data-moorainbow-button-type="+g+"]").addClass("moor-button-active");
f.each(function(h){this.layout.getElement("[data-moorainbow-input="+h+"]").addClass("moor-inputs-block-active");},this);}else{this.layout.getElement("[data-moorainbow-button-type="+g+"]").removeClass("moor-button-active");
f.each(function(h){this.layout.getElement("[data-moorainbow-input="+h+"]").removeClass("moor-inputs-block-active");},this);}},this);},triggerClose:function(c,b){this.hide();
},triggerTransparent:function(d,c){c=this.active;if(!c){return;}var b,e="transparent";if(c.get("data-moorainbow-trigger")){b=c;c=document.id(c.get("data-moorainbow-trigger"));
}else{b=document.getElements("[data-moorainbow-trigger="+c.get("id")+"]");}c.set("value",e);b.getElement(".overlay").addClass("overlay-"+e).setStyle("backgroundColor",e);
this.hide();},_hsbToHex:function(b){b=b||this._getHSB();return b.hsbToRgb().rgbToHex();},_hexToHsb:function(b){return b.hexToRgb().rgbToHsb();},_getHSB:function(){var i=this._getCoordinates(),j=this.layout.getElement("[data-moorainbow-overlay]"),k=this.layout.getElement("[data-moorainbow-slider]"),e=j.getComputedSize(),d=k.getComputedSize();
var g=Math.round((i.overlay.x*100)/e.width),c=100-Math.round((i.overlay.y*100)/e.height),f=360-Math.round((i.slider.y*360)/d.height);f=(f>=360)?0:(f<0)?0:f;
g=(g>100)?100:(g<0)?0:g;c=(c>100)?100:(c<0)?0:c;return[f,g,c];},_updateCursors:function(b,f){var j=this.overlayCursor,e=this.sliderCursor,d=this.layout.getElement("[data-moorainbow-overlay]"),c=this.layout.getElement("[data-moorainbow-slider]"),g=d.getComputedSize(),h=c.getComputedSize();
if(f=="rgb"){b=b.rgbToHsb();}var i={x:Math.round(((g.width*b[1])/100)-this.offsets.overlay),y:Math.round(-((g.height*b[2])/100)+g.height-this.offsets.overlay),z:Math.round((h.height*b[0])/360)};
if(i.z==360){i.z=0;}i.z=h.height-i.z-this.offsets.slider;j.style.top=i.y+"px";j.style.left=i.x+"px";e.style.top=i.z+"px";},_getCoordinates:function(){var d=this.overlayCursor,c=this.sliderCursor,b=this.layout.getElement("[data-moorainbow-overlay]"),e=this.layout.getElement("[data-moorainbow-slider]");
return{overlay:{x:d.getPosition(b).x+this.offsets.overlay,y:d.getPosition(b).y+this.offsets.overlay},slider:{y:c.getPosition(e).y+this.offsets.slider}};
},_validateData:function(d,b){var c=b.getParent("[data-moorainbow-input]"),f=b.get("value"),e;if(!c){return false;}e=c.get("data-moorainbow-input");switch(e){case"hue":if(!f.match(/^[0-9]{1,3}$/)||f.toInt()>=360){b.set("value",0);
return true;}break;case"saturation":case"brightness":if(!f.match(/^[0-9]{1,3}$/)||f.toInt()>100){b.set("value",100);return true;}break;case"red":case"green":case"blue":if(!f.match(/^[0-9]{1,3}$/)||f.toInt()>255){b.set("value",0);
return true;}break;}return true;}});window.addEvent("domready",function(){this.moorainbow=new MooRainbow();});})());