/*
 * $Id: mooRainbow-uncompressed.js 945 2008-06-09 17:28:15Z Fritz Elfert $
 *
 * MooRainbow
 *
 * @version        1.11
 * @license        MIT-style license
 * @author        w00fz - < w00fzIT [at] gmail.com >
 * @infos        http://moorainbow.woolly-sheep.net
 * @copyright    Author
 * 
 *
 */
var MooRainbow=new Class({options:{id:"mooRainbow",prefix:"moor-",imgPath:"images/",startColor:[255,0,0],okLabel:"Select",wheel:false,onComplete:Class.empty,onChange:Class.empty},reAttachAndShow:function(B){this.element=$(B);if(!this.element){return }this.removeEvent("onComplete");this.txtfield=this.element.getProperty("rel");if(this.txtfield){var A=$(this.txtfield);if(A){this.manualSet(A.value,"hex");this.options.startColor=this.currentColor;this.backupColor=this.currentColor;this.layout.backup.setStyle("background-color",this.backupColor.rgbToHex());this.addEvent("onComplete",function(C){$(this.txtfield).value=C.hex})}}this.OverlayEvents();this.hide();this.show()},initialize:function(B,A){this.element=$(B);this.setOptions(A);this.sliderPos=0;this.pickerPos={x:0,y:0};this.backupColor=this.options.startColor;this.currentColor=this.options.startColor;this.sets={rgb:[],hsb:[],hex:[]};this.pickerClick=this.sliderClick=false;if(!this.layout){this.doLayout()}if(this.element){this.OverlayEvents()}this.sliderEvents();this.backupEvent();if(this.options.wheel){this.wheelEvents()}if(this.element){this.element.addEvent("click",function(C){this.toggle(C)}.bind(this))}this.layout.overlay.setStyle("background-color",this.options.startColor.rgbToHex());this.layout.backup.setStyle("background-color",this.backupColor.rgbToHex());this.pickerPos.x=this.snippet("curPos").l+this.snippet("curSize","int").w;this.pickerPos.y=this.snippet("curPos").t+this.snippet("curSize","int").h;this.manualSet(this.options.startColor);this.pickerPos.x=this.snippet("curPos").l+this.snippet("curSize","int").w;this.pickerPos.y=this.snippet("curPos").t+this.snippet("curSize","int").h;this.sliderPos=this.snippet("arrPos")-this.snippet("arrSize","int");if(window.khtml){this.hide()}},toggle:function(){this[this.visible?"hide":"show"]()},show:function(){if(!this.element){return }this.rePosition();this.layout.setStyle("display","block");this.visible=true},hide:function(){this.layout.setStyles({display:"none"});this.visible=false},manualSet:function(B,D){if(!D||(D!="hsb"&&D!="hex")){D="rgb"}var C,A,E;if(D=="rgb"){C=B;A=B.rgbToHsb();E=B.rgbToHex()}else{if(D=="hsb"){A=B;C=B.hsbToRgb();E=C.rgbToHex()}else{E=B;C=B.hexToRgb(true);A=C.rgbToHsb()}}this.setMooRainbow(C);this.autoSet(A)},autoSet:function(B){var K=this.snippet("curSize","int").h;var A=this.snippet("curSize","int").w;var C=this.layout.overlay.height;var I=this.layout.overlay.width;var J=this.layout.slider.height;var L=this.snippet("arrSize","int");var E;var H=Math.round(((I*B[1])/100)-A);var F=Math.round(-((C*B[2])/100)+C-K);var G=Math.round(((J*B[0])/360));G=(G==360)?0:G;var D=J-G+this.snippet("slider")-L;E=[this.sets.hsb[0],100,100].hsbToRgb().rgbToHex();this.layout.cursor.setStyles({top:F,left:H});this.layout.arrows.setStyle("top",D);this.layout.overlay.setStyle("background-color",E);this.sliderPos=this.snippet("arrPos")-L;this.pickerPos.x=this.snippet("curPos").l+A;this.pickerPos.y=this.snippet("curPos").t+K},setMooRainbow:function(B,D){if(!D||(D!="hsb"&&D!="hex")){D="rgb"}var C,A,E;if(D=="rgb"){C=B;A=B.rgbToHsb();E=B.rgbToHex()}else{if(D=="hsb"){A=B;C=B.hsbToRgb();E=C.rgbToHex()}else{E=B;C=B.hexToRgb();A=C.rgbToHsb()}}this.sets={rgb:C,hsb:A,hex:E};if(!$chk(this.pickerPos.x)){this.autoSet(A)}this.RedInput.value=C[0];this.GreenInput.value=C[1];this.BlueInput.value=C[2];this.HueInput.value=A[0];this.SatuInput.value=A[1];this.BrighInput.value=A[2];this.hexInput.value=E;this.currentColor=C;this.chooseColor.setStyle("background-color",C.rgbToHex())},parseColors:function(B,F,E){var D=Math.round((B*100)/this.layout.overlay.width);var A=100-Math.round((F*100)/this.layout.overlay.height);var C=360-Math.round((E*360)/this.layout.slider.height)+this.snippet("slider")-this.snippet("arrSize","int");C-=this.snippet("arrSize","int");C=(C>=360)?0:(C<0)?0:C;D=(D>100)?100:(D<0)?0:D;A=(A>100)?100:(A<0)?0:A;return[C,D,A]},OverlayEvents:function(){var D,C,B,A;C=this.snippet("curSize","int").h;B=this.snippet("curSize","int").w;A=this.arrRGB.copy().concat(this.arrHSB,this.hexInput);document.addEvent("click",function(){if(this.visible){this.hide(this.layout)}}.bind(this));A.each(function(E){E.addEvent("keydown",this.eventKeydown.bindWithEvent(this,E));E.addEvent("keyup",this.eventKeyup.bindWithEvent(this,E))},this);[this.element,this.layout].each(function(E){E.addEvents({click:function(F){new Event(F).stop()},keyup:function(F){F=new Event(F);if(F.key=="esc"&&this.visible){this.hide(this.layout)}}.bind(this)},this)},this);D={x:[0-B,(this.layout.overlay.width-B)],y:[0-C,(this.layout.overlay.height-C)]};if(!this.layout.drag){this.layout.drag=new Drag.Base(this.layout.cursor,{limit:D,onStart:this.overlayDrag.bind(this),onDrag:this.overlayDrag.bind(this),snap:0});this.layout.overlay2.addEvent("mousedown",function(E){E=new Event(E);this.layout.cursor.setStyles({top:E.page.y-this.layout.overlay.getTop()-C,left:E.page.x-this.layout.overlay.getLeft()-B});this.layout.drag.start(E)}.bind(this))}this.okButton.addEvent("click",function(){if(this.currentColor==this.options.startColor){this.hide();this.fireEvent("onComplete",[this.sets,this])}else{this.backupColor=this.currentColor;this.layout.backup.setStyle("background-color",this.backupColor.rgbToHex());this.hide();this.fireEvent("onComplete",[this.sets,this])}}.bind(this))},overlayDrag:function(){var B=this.snippet("curSize","int").h;var A=this.snippet("curSize","int").w;this.pickerPos.x=this.snippet("curPos").l+A;this.pickerPos.y=this.snippet("curPos").t+B;this.setMooRainbow(this.parseColors(this.pickerPos.x,this.pickerPos.y,this.sliderPos),"hsb");this.fireEvent("onChange",[this.sets,this])},sliderEvents:function(){var A=this.snippet("arrSize","int"),B;B=[0+this.snippet("slider")-A,this.layout.slider.height-A+this.snippet("slider")];this.layout.sliderDrag=new Drag.Base(this.layout.arrows,{limit:{y:B},modifiers:{x:false},onStart:this.sliderDrag.bind(this),onDrag:this.sliderDrag.bind(this),snap:0});this.layout.slider.addEvent("mousedown",function(C){C=new Event(C);this.layout.arrows.setStyle("top",C.page.y-this.layout.slider.getTop()+this.snippet("slider")-A);this.layout.sliderDrag.start(C)}.bind(this))},sliderDrag:function(){var A=this.snippet("arrSize","int"),B;this.sliderPos=this.snippet("arrPos")-A;this.setMooRainbow(this.parseColors(this.pickerPos.x,this.pickerPos.y,this.sliderPos),"hsb");B=[this.sets.hsb[0],100,100].hsbToRgb().rgbToHex();this.layout.overlay.setStyle("background-color",B);this.fireEvent("onChange",[this.sets,this])},backupEvent:function(){this.layout.backup.addEvent("click",function(){this.manualSet(this.backupColor);this.fireEvent("onChange",[this.sets,this])}.bind(this))},wheelEvents:function(){var A=this.arrRGB.copy().extend(this.arrHSB);A.each(function(B){B.addEvents({mousewheel:this.eventKeys.bindWithEvent(this,B),keydown:this.eventKeys.bindWithEvent(this,B)})},this);[this.layout.arrows,this.layout.slider].each(function(B){B.addEvents({mousewheel:this.eventKeys.bindWithEvent(this,[this.arrHSB[0],"slider"]),keydown:this.eventKeys.bindWithEvent(this,[this.arrHSB[0],"slider"])})},this)},eventKeys:function(E,B,A){var F,G;A=(!A)?B.id:this.arrHSB[0];if(E.type=="keydown"){if(E.key=="up"){F=1}else{if(E.key=="down"){F=-1}else{return }}}else{if(E.type==Element.Events.mousewheel.type){F=(E.wheel>0)?1:-1}}if(this.arrRGB.test(B)){G="rgb"}else{if(this.arrHSB.test(B)){G="hsb"}else{G="hsb"}}if(G=="rgb"){var H=this.sets.rgb,C=this.sets.hsb,D=this.options.prefix,J;var I=B.value.toInt()+F;I=(I>255)?255:(I<0)?0:I;switch(B.className){case D+"rInput":J=[I,H[1],H[2]];break;case D+"gInput":J=[H[0],I,H[2]];break;case D+"bInput":J=[H[0],H[1],I];break;default:J=H}this.manualSet(J);this.fireEvent("onChange",[this.sets,this])}else{var H=this.sets.rgb,C=this.sets.hsb,D=this.options.prefix,J;var I=B.value.toInt()+F;if(B.className.test(/(HueInput)/)){I=(I>359)?0:(I<0)?0:I}else{I=(I>100)?100:(I<0)?0:I}switch(B.className){case D+"HueInput":J=[I,C[1],C[2]];break;case D+"SatuInput":J=[C[0],I,C[2]];break;case D+"BrighInput":J=[C[0],C[1],I];break;default:J=C}this.manualSet(J,"hsb");this.fireEvent("onChange",[this.sets,this])}E.stop()},eventKeydown:function(C,B){var D=C.code,A=C.key;if((!B.className.test(/hexInput/)&&!(D>=48&&D<=57))&&(A!="backspace"&&A!="tab"&&A!="delete"&&A!="left"&&A!="right")){C.stop()}},eventKeyup:function(F,C){var G=F.code,A=F.key,D,E,B=C.value.charAt(0);if(!$chk(C.value)){return }if(C.className.test(/hexInput/)){if(B!="#"&&C.value.length!=6){return }if(B=="#"&&C.value.length!=7){return }}else{if(!(G>=48&&G<=57)&&(!["backspace","tab","delete","left","right"].test(A))&&C.value.length>3){return }}E=this.options.prefix;if(C.className.test(/(rInput|gInput|bInput)/)){if(C.value<0||C.value>255){return }switch(C.className){case E+"rInput":D=[C.value,this.sets.rgb[1],this.sets.rgb[2]];break;case E+"gInput":D=[this.sets.rgb[0],C.value,this.sets.rgb[2]];break;case E+"bInput":D=[this.sets.rgb[0],this.sets.rgb[1],C.value];break;default:D=this.sets.rgb}this.manualSet(D);this.fireEvent("onChange",[this.sets,this])}else{if(!C.className.test(/hexInput/)){if(C.className.test(/HueInput/)&&C.value<0||C.value>360){return }else{if(C.className.test(/HueInput/)&&C.value==360){C.value=0}else{if(C.className.test(/(SatuInput|BrighInput)/)&&C.value<0||C.value>100){return }}}switch(C.className){case E+"HueInput":D=[C.value,this.sets.hsb[1],this.sets.hsb[2]];break;case E+"SatuInput":D=[this.sets.hsb[0],C.value,this.sets.hsb[2]];break;case E+"BrighInput":D=[this.sets.hsb[0],this.sets.hsb[1],C.value];break;default:D=this.sets.hsb}this.manualSet(D,"hsb");this.fireEvent("onChange",[this.sets,this])}else{D=C.value.hexToRgb(true);if(isNaN(D[0])||isNaN(D[1])||isNaN(D[2])){return }if($chk(D)){this.manualSet(D);this.fireEvent("onChange",[this.sets,this])}}}},doLayout:function(){var S=this.options.id,X=this.options.prefix;var C=S+" ."+X;this.layout=new Element("div",{styles:{display:"block",position:"absolute"},id:S}).inject(document.body);var K=new Element("div",{styles:{position:"relative"},"class":X+"box"}).inject(this.layout);this.box=K;var O=new Element("div",{styles:{position:"absolute",overflow:"hidden"},"class":X+"overlayBox"}).inject(K);var P=new Element("div",{styles:{position:"absolute",zIndex:1},"class":X+"arrows"}).inject(K);P.width=P.getStyle("width").toInt();P.height=P.getStyle("height").toInt();var H=new Element("img",{styles:{"background-color":"#fff",position:"relative",zIndex:2},src:this.options.imgPath+"moor_woverlay.png","class":X+"overlay"}).inject(O);var N=new Element("img",{styles:{position:"absolute",top:0,left:0,zIndex:2},src:this.options.imgPath+"moor_boverlay.png","class":X+"overlay"}).inject(O);if(window.ie6){O.setStyle("overflow","");var I=H.src;H.src=this.options.imgPath+"blank.gif";H.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+I+"', sizingMethod='scale')";I=N.src;N.src=this.options.imgPath+"blank.gif";N.style.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+I+"', sizingMethod='scale')"}H.width=N.width=O.getStyle("width").toInt();H.height=N.height=O.getStyle("height").toInt();var F=new Element("div",{styles:{overflow:"hidden",position:"absolute",zIndex:2},"class":X+"cursor"}).inject(O);F.width=F.getStyle("width").toInt();F.height=F.getStyle("height").toInt();var Z=new Element("img",{styles:{position:"absolute","z-index":2},src:this.options.imgPath+"moor_slider.png","class":X+"slider"}).inject(K);this.layout.slider=$E("#"+C+"slider");Z.width=Z.getStyle("width").toInt();Z.height=Z.getStyle("height").toInt();new Element("div",{styles:{position:"absolute"},"class":X+"colorBox"}).inject(K);new Element("div",{styles:{zIndex:2,position:"absolute"},"class":X+"chooseColor"}).inject(K);this.layout.backup=new Element("div",{styles:{zIndex:2,position:"absolute",cursor:"pointer"},"class":X+"currentColor"}).inject(K);var E=new Element("label").inject(K).setStyle("position","absolute");var L=E.clone().inject(K).addClass(X+"gLabel").appendText("G: ");var Q=E.clone().inject(K).addClass(X+"bLabel").appendText("B: ");E.appendText("R: ").addClass(X+"rLabel");var b=new Element("input");var A=b.clone().inject(L).addClass(X+"gInput");var D=b.clone().inject(Q).addClass(X+"bInput");b.inject(E).addClass(X+"rInput");var V=new Element("label").inject(K).setStyle("position","absolute");var a=V.clone().inject(K).addClass(X+"SatuLabel").appendText("S: ");var W=V.clone().inject(K).addClass(X+"BrighLabel").appendText("B: ");V.appendText("H: ").addClass(X+"HueLabel");var T=new Element("input");var Y=T.clone().inject(a).addClass(X+"SatuInput");var U=T.clone().inject(W).addClass(X+"BrighInput");T.inject(V).addClass(X+"HueInput");a.appendText(" %");W.appendText(" %");new Element("span",{styles:{position:"absolute"},"class":X+"ballino"}).setHTML(" &deg;").injectAfter(V);var J=new Element("label").inject(K).setStyle("position","absolute").addClass(X+"hexLabel").appendText("#hex: ").adopt(new Element("input").addClass(X+"hexInput"));var M=new Element("input",{styles:{position:"absolute"},type:"button",value:this.options.okLabel,"class":X+"okButton"}).inject(K);this.rePosition();var c=$$("#"+C+"overlay");this.layout.overlay=c[0];this.layout.overlay2=c[1];this.layout.cursor=$E("#"+C+"cursor");this.layout.arrows=$E("#"+C+"arrows");this.chooseColor=$E("#"+C+"chooseColor");this.layout.backup=$E("#"+C+"currentColor");this.RedInput=$E("#"+C+"rInput");this.GreenInput=$E("#"+C+"gInput");this.BlueInput=$E("#"+C+"bInput");this.HueInput=$E("#"+C+"HueInput");this.SatuInput=$E("#"+C+"SatuInput");this.BrighInput=$E("#"+C+"BrighInput");this.hexInput=$E("#"+C+"hexInput");this.arrRGB=[this.RedInput,this.GreenInput,this.BlueInput];this.arrHSB=[this.HueInput,this.SatuInput,this.BrighInput];this.okButton=$E("#"+C+"okButton");if(!window.khtml){this.hide()}},rePosition:function(){if(!this.element){return }var C=this.element.getCoordinates();var A,B;switch(this.options.align){case"tl":A=C.left-this.box.getStyle("width").toInt();B=C.top+(C.height/2);break;default:A=C.left;B=C.top+C.height+1;break}this.layout.setStyles({left:C.left-this.box.getStyle("width").toInt(),top:C.top+C.height+1})},snippet:function(G,F){var D;F=(F)?F:"none";switch(G){case"arrPos":var C=this.layout.arrows.getStyle("top").toInt();D=C;break;case"arrSize":var E=this.layout.arrows.height;E=(F=="int")?(E/2).toInt():E;D=E;break;case"curPos":var B=this.layout.cursor.getStyle("left").toInt();var C=this.layout.cursor.getStyle("top").toInt();D={l:B,t:C};break;case"slider":var C=this.layout.slider.getStyle("marginTop").toInt();D=C;break;default:var E=this.layout.cursor.height;var A=this.layout.cursor.width;E=(F=="int")?(E/2).toInt():E;A=(F=="int")?(A/2).toInt():A;D={w:A,h:E}}return D}});MooRainbow.implement(new Options);MooRainbow.implement(new Events);