/*
  mColorPicker
  Version: 1.0
  
  Copyright (c) 2010 Meta100 LLC.
  
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:
  
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
  
  Except as contained in this notice, the name(s) of the above 
  copyright holders shall not be used in advertising or otherwise 
  to promote the sale, use or other dealings in this Software 
  without prior written authorization.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*/

mColorPicker={show_logo:true,current_color:false,current_value:false,color:false,imageUrl:"http://plugins.meta100.com.s3.amazonaws.com/mcolorpicker/images/",swatches:["#ffffff","#ffff00","#00ff00","#00ffff","#0000ff","#ff00ff","#ff0000","#4c2b11","#3b3b3b","#000000"],colorShow:function(e,d){var a="icp_"+e;eICP=jQuery("#"+a).offset(),hex=jQuery("#"+e).attr("hex");jQuery("#mColorPicker").css({top:(eICP.top+jQuery("#"+a).outerHeight())+"px",left:(eICP.left)+"px",position:"absolute"}).fadeIn("fast");jQuery("#mColorPickerBg").css({"z-index":999990,background:"transparent",position:"absolute",top:0,left:0,width:parseInt(jQuery(document).width())+"px",height:parseInt(jQuery(document).height())+"px"}).fadeIn("fast");var c=jQuery("#"+e).val();jQuery("#colorPreview span").text(c);jQuery("#colorPreview").css("background",c);jQuery("#color").val(c);if(d){mColorPicker.current_color=jQuery("#icp_"+e).css("background-color")}else{mColorPicker.current_color=jQuery("#"+e).css("background-color")}if(hex=="true"){mColorPicker.current_color=mColorPicker.toRGBHex(mColorPicker.current_color)}jQuery("#mColorPickerInput").val(mColorPicker.current_color);var b=jQuery("#mColorPicker");jQuery("#mColorPickerImg, #mColorPickerImgGray, #mColorPickerTransparent, .pastColor").unbind().mousemove(function(f){var g=jQuery(this).offset();mColorPicker.color=jQuery(this).css("background-color");if(jQuery(this).hasClass("pastColor")&&hex=="true"){mColorPicker.color=mColorPicker.toRGBHex(mColorPicker.color)}else{if(jQuery(this).attr("id")=="mColorPickerTransparent"){mColorPicker.color="transparent"}else{if(!jQuery(this).hasClass("pastColor")){mColorPicker.color=mColorPicker.whichColor(f.pageX-g.left,f.pageY-g.top+((jQuery(this).attr("id")=="mColorPickerImgGray")?128:0),hex)}}}mColorPicker.setInputColor(e,mColorPicker.color,d)}).click(function(){mColorPicker.colorPicked(e,d)});jQuery("#mColorPickerInput").unbind().bind("keyup",function(f){mColorPicker.color=jQuery("#mColorPickerInput").val();mColorPicker.setInputColor(e,mColorPicker.color,d);if(f.which==13){mColorPicker.colorPicked(e,d)}}).bind("blur",function(){mColorPicker.setInputColor(e,mColorPicker.current_color,d)});jQuery("#mColorPickerSwatches, #mColorPickerFooter, #mColorPickerWrapper, #mColorPicker, #mColorPickerImg, #mColorPickerImgGray, .pastColor, #mColorPickerTransparent").unbind("mouseleave").bind("mouseleave",function(f){mColorPicker.setInputColor(e,mColorPicker.current_color,d)})},setInputColor:function(e,a,c){var d=(a=="transparent")?"url('"+mColorPicker.imageUrl+"grid.gif')":"",b=mColorPicker.textColor(a);if(c){jQuery("#icp_"+e).css({"background-color":a,"background-image":d})}jQuery("#"+e).val(a).css({"background-color":a,"background-image":d,color:b}).trigger("change");jQuery("#mColorPickerInput").val(a)},textColor:function(a){if(typeof a=="undefined"||a=="transparent"){return"black"}a=mColorPicker.toRGBHex(a);return(parseInt(a.substr(1,2),16)+parseInt(a.substr(3,2),16)+parseInt(a.substr(5,2),16)<400)?"white":"black"},set_cookie:function(c,d,e){var b=c+"="+escape(d),a=new Date();a.setDate(a.getDate()+e);b+="; expires="+a.toGMTString();document.cookie=b},get_cookie:function(b){var a=document.cookie.match("(^|;) ?"+b+"=([^;]*)(;|$)");if(a){return(unescape(a[2]))}else{return null}},colorPicked:function(d,c){var b=[],a=0;mColorPicker.current_value=mColorPicker.current_color=mColorPicker.color;jQuery("#mColorPickerImg").unbind();jQuery("#mColorPickerImgGray").unbind();jQuery(".pastColor").unbind();jQuery("#mColorPickerBg").hide();jQuery("#mColorPicker").fadeOut();if(mColorPicker.color!="transparent"){b[0]=mColorPicker.color}jQuery(".pastColor").each(function(){var e=jQuery(this).css("background-color");if(e!=b[0]&&mColorPicker.toRGBHex(e)!=b[0]&&b.length<10){b[b.length]=e}jQuery(this).css("background-color",b[a++])});jQuery("#"+d).trigger("colorpicked");mColorPicker.set_cookie("swatches",b.join("||"),365)},whichColor:function(a,d,c){var b=colorG=colorB=256;if(a<32){colorG=a*8;colorB=1}else{if(a<64){b=256-(a-32)*8;colorB=1}else{if(a<96){b=1;colorB=(a-64)*8}else{if(a<128){b=1;colorG=256-(a-96)*8}else{if(a<160){b=(a-128)*8;colorG=1}else{colorG=1;colorB=256-(a-160)*8}}}}}if(d<64){b=b+(256-b)*(64-d)/64;colorG=colorG+(256-colorG)*(64-d)/64;colorB=colorB+(256-colorB)*(64-d)/64}else{if(d<=128){b=b-b*(d-64)/64;colorG=colorG-colorG*(d-64)/64;colorB=colorB-colorB*(d-64)/64}else{if(d>128){b=256-(a/192*256);colorG=256-(a/192*256);colorB=256-(a/192*256)}}}b=parseInt(b);colorG=parseInt(colorG);colorB=parseInt(colorB);if(b>=256){b=255}if(colorG>=256){colorG=255}if(colorB>=256){colorB=255}if(c=="true"){b=b.toString(16);colorG=colorG.toString(16);colorB=colorB.toString(16);if(b.length<2){b=0+b}if(colorG.length<2){colorG=0+colorG}if(colorB.length<2){colorB=0+colorB}return"#"+b+colorG+colorB}return"rgb("+b+", "+colorG+", "+colorB+")"},toRGBHex:function(c){if(typeof c=="undefined"){return""}if(c.indexOf("#")>-1){return c}var a=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"],d="#",b=0;c=c.replace(/[^0-9,]/g,"").split(",");for(var e=0;e<c.length;e++){b=Math.floor(c[e]/16);d+=a[b]+a[c[e]-b*16]}return d},main:function(){jQuery("input").filter(function(a){return this.getAttribute("type")=="color"}).each(function(g){if(jQuery("div#mColorPicker").length<1){jQuery(document.createElement("div")).attr("id","mColorPicker").css("display","none").html('<div id="mColorPickerWrapper"><div id="mColorPickerImg"></div><div id="mColorPickerImgGray"></div><div id="mColorPickerSwatches"><div class="clear"></div></div><div id="mColorPickerFooter"><input type="text" size="8" id="mColorPickerInput"/><span id="mColorPickerTransparent">transparent</span></div></div>').appendTo("body");jQuery(document.createElement("div")).attr("id","mColorPickerBg").click(function(){jQuery("#mColorPickerBg").hide();jQuery("#mColorPicker").fadeOut()}).css({display:"none"}).appendTo("body");for(n=9;n>-1;n--){jQuery(document.createElement("div")).attr({id:"cell"+n,"class":"pastColor"+((n>0)?" noLeftBorder":"")}).html("&nbsp;").prependTo("#mColorPickerSwatches")}jQuery("table.pickerTable td").css({width:"12px",height:"14px",border:"1px solid #000",cursor:"pointer"});jQuery("#mColorPicker table.pickerTable").css({"border-collapse":"collapse"});jQuery("#mColorPicker").css({border:"1px solid #ccc",background:"#333",color:"#fff","z-index":999998,width:"194px",height:"184px","font-size":"12px","font-family":"times"});jQuery(".pastColor").css({height:"18px",width:"18px",border:"1px solid #000","float":"left"});jQuery("#colorPreview").css({height:"50px"});jQuery(".noLeftBorder").css({"border-left":"0"});jQuery(".clear").css({clear:"both"});jQuery("#mColorPickerWrapper").css({position:"relative",border:"solid 1px gray","background-color":"white","z-index":"999999"});jQuery("#mColorPickerImg").css({height:"128px",width:"192px",border:"0",cursor:"crosshair","background-image":"url('"+mColorPicker.imageUrl+"colorpicker.png')"});jQuery("#mColorPickerImgGray").css({height:"8px",width:"192px",border:"0",cursor:"crosshair","background-image":"url('"+mColorPicker.imageUrl+"graybar.jpg')"});jQuery("#mColorPickerInput").css({border:"solid 1px gray","font-size":"10pt",margin:"3px",width:"80px"});jQuery("#mColorPickerImgGrid").css({border:"0",height:"20px",width:"20px","vertical-align":"text-bottom"});jQuery("#mColorPickerSwatches").css({"background-color":"#000"});jQuery("#mColorPickerFooter").css({"background-image":"url('"+mColorPicker.imageUrl+"grid.gif')",position:"relative",height:"26px"});jQuery("#mColorPickerTransparent").css({"font-size":" 16px",color:"#000","padding-left":" 4px",cursor:" pointer",overflow:"hidden"});if(mColorPicker.show_logo){jQuery("#mColorPickerFooter").prepend('<a href="http://meta100.com/" title="Meta100 - Designing Fun" alt="Meta100 - Designing Fun" style="float:right;" target="_blank"><img src="'+mColorPicker.imageUrl+'meta100.png" title="Meta100 - Designing Fun" alt="Meta100 - Designing Fun" style="border:0;top:5px;right:1px;position: absolute;"/></a>')}}var b=jQuery(this).attr("id"),c=new Date(),j=false;if(b==""){b=jQuery(this).attr("name")}if(b==""){b="color_"+Math.round(Math.random()*c.getTime())}jQuery(this).attr("id",b);if(jQuery(this).attr("text")=="hidden"){var f=jQuery(this).val(),d=(jQuery(this).width()>0)?jQuery(this).width():parseInt(jQuery(this).css("width")),l=(jQuery(this).height())?jQuery(this).height():parseInt(jQuery(this).css("height")),h=jQuery(this).css("float"),e=(f=="transparent")?"url('"+mColorPicker.imageUrl+"/grid.gif')":"",a="";jQuery("body").append('<span id="color_work_area"></span>');jQuery("span#color_work_area").append(jQuery(this).clone(true));a=jQuery("span#color_work_area").html().replace(/type=[^a-z]*color[^a-z]*/gi,'type="hidden"');jQuery("span#color_work_area").html("").remove();jQuery(this).after('<span style="cursor:pointer;border:1px solid black;float:'+h+";width:"+d+"px;height:"+l+'px;" id="icp_'+b+'">&nbsp;</span>').after(a).remove();jQuery("#icp_"+b).css({"background-color":f,"background-image":e,display:"inline-block"});j=true}else{var f=jQuery(this).val(),e=(f=="transparent")?"url('"+mColorPicker.imageUrl+"/grid.gif')":"",a="";jQuery("body").append('<span id="color_work_area"></span>');jQuery("span#color_work_area").append(jQuery(this).clone(true));a=jQuery("span#color_work_area").html().replace(/type=[^a-z]*color[^a-z]*/gi,'type="text"');jQuery("span#color_work_area").html("").remove();jQuery(this).after(a).remove();jQuery("#"+b).css({"background-color":f,"background-image":e}).css({color:mColorPicker.textColor(jQuery("#"+b).css("background-color"))}).after('<span style="cursor:pointer;" id="icp_'+b+'"><img src="'+mColorPicker.imageUrl+'color.png" style="border:0;margin:0 0 0 3px" align="absmiddle"></span>').addClass("mColorPickerInput")}jQuery("#icp_"+b).bind("click",function(){mColorPicker.colorShow(b,j)});var k=mColorPicker.get_cookie("swatches");g=0;if(k==null){k=mColorPicker.swatches}else{k=k.split("||")}if(k.length<10){k=mColorPicker.swatches}jQuery(".pastColor").each(function(){jQuery(this).css("background-color",k[g++])})});jQuery(".mColorPickerInput").unbind().bind("keyup",function(){jQuery(this).css({"background-color":jQuery(this).val()}).css({color:mColorPicker.textColor(jQuery(this).css("background-color"))})})}};jQuery(document).ready(function(){mColorPicker.main();jQuery(document).bind("ajaxSuccess",function(){mColorPicker.main()})});
