/*!
 * 颜色选择器插件
 * www.JSPGen.com
 * @revise 1.0
 */
;(function($){var color_picker=new function(){var jspath=document.scripts;jspath=jspath[jspath.length-1].src.substring(0,jspath[jspath.length-1].src.lastIndexOf('/')+1);grapes.load([jspath+'images/colorPicker.css']);var currentPicker=null;var allColorArray=[];var sideLength=6;this.init=function(){$("body").append('<div id="colorPicker" class="color_picker"><ul class="color_picker_bgview_text" ><li><div id="bigBgshowDiv"></div></li><li><input id="bigHexColorText" size="7" maxlength="7" value="#000000" /></li><li class="color_picker_icon"><a id="colorReset" href="javascript:void(0);" class="color_picker_reset" title="默认色"></a></li></ul><div id="bigSections" class="color_picker_sections_color"></div><div id="bigLayout" class="biglayout" ></div></div>');$("#bigLayout").hover(function(){$(this).show();},function(){$(this).hide();}).click(function(){$("#colorPicker").hide();});$("#bigHexColorText").keypress(function(){var text=$.trim($(this).val());$(this).val(text.replace(/[^A-Fa-f0-9#]/g,''));if(text.length<=0)return;text=text.charAt(0)=='#'?text:"#"+text;var countChar=7-text.length;if(countChar<0){text=text.substring(0,7);}else if(countChar>0){for(var i=0;i<countChar;i++){text+="0";};}
$("#bigBgshowDiv").css("backgroundColor",text);}).keyup(function(){var text=$.trim($(this).val());$(this).val(text.replace(/[^A-Fa-f0-9#]/g,''));}).focus(function(){this.select();});$('#colorReset').bind('click',function(){$('#bigBgshowDiv').css('backgroundColor','');$('#bigHexColorText').val(''); invokeCallBack('');$('#colorPicker').hide();});$(document).bind('mousedown',function(event){if(!($(event.target).parents().addBack().is('#colorPicker'))){$("#colorPicker").hide();}});};this.showPicker=function(callback,engine,sideLength_){if($("body").length>0&&$("#colorPicker").length<=0){color_picker.init();}
var sl_=parseInt(sideLength_,10);if(engine=="L"&&!isNaN(sl_)&&sl_>=2&&sl_<=10){sideLength=sl_;}else{sideLength=6;}
this.getAllColorArray=function(){allColorArray=new Array(sideLength*3+2);var mixColorArray=[];var blackWhiteGradientArray=gradientColor(new RGB(0,0,0),new RGB(255,255,255));for(var i=0;i<blackWhiteGradientArray.length;i++){mixColorArray[i]=blackWhiteGradientArray[i];}
var baseArray=[new RGB(255,0,0),new RGB(0,255,0),new RGB(0,0,255),new RGB(255,255,0),new RGB(0,255,255),new RGB(255,0,255),new RGB(204,255,0),new RGB(153,0,255),new RGB(102,255,255),new RGB(51,0,0)];mixColorArray=mixColorArray.concat(baseArray.slice(0,sideLength));allColorArray[0]=mixColorArray;var blackArray=new Array(sideLength*2);for(var i=0;i<blackArray.length;i++){blackArray[i]=new RGB(0,0,0);}
allColorArray[1]=blackArray;var cornerColorArray=new Array();cornerColorArray.push(generateBlockcornerColor(0),generateBlockcornerColor(51),generateBlockcornerColor(102),generateBlockcornerColor(153),generateBlockcornerColor(204),generateBlockcornerColor(255));var count=0;var halfOfAllArray1=[];var halfOfAllArray2=[];for(var i=0;i<cornerColorArray.length;i++){var startArray=gradientColor(cornerColorArray[i][0][0],cornerColorArray[i][0][1]);var endArray=gradientColor(cornerColorArray[i][1][0],cornerColorArray[i][1][1]);for(var j=0;j<sideLength;j++){if(i<3){halfOfAllArray1[count]=gradientColor(startArray[j],endArray[j]);}else{halfOfAllArray2[count-sideLength*3]=gradientColor(startArray[j],endArray[j]);}
count++;}}
for(var i=0;i<halfOfAllArray1.length;i++){allColorArray[i+2]=halfOfAllArray1[i].concat(halfOfAllArray2[i]);}
for(var i=0;i<allColorArray.length;i++){for(var j=0;j<allColorArray[i].length;j++){allColorArray[i][j]=RGBToHex(allColorArray[i][j]);}}};this.getAllColorArray();$(this).data("callback",callback);if(engine){try{engine=engine.toUpperCase();}catch(e){engine="P";}}
if(engine=="L"){$("#bigSections").unbind("mousemove").unbind("mouseout").removeClass("color_picker_bgimage");var ulArray=new Array();for(var i=0;i<sideLength*3+2;i++){ulArray.push("<ul>");for(var j=0;j<sideLength*2;j++){ulArray.push("<li data-color='"+allColorArray[i][j]+"' style='background-color: "+allColorArray[i][j]+";' ></li>");}
ulArray.push("</ul>");}
$("#bigSections").html(ulArray.join(""));var minColorpickerHeight=90;var minColorpickerWidth=129;var minSectionsHeight=minColorpickerHeight-29;var minSectionsWidth=minColorpickerWidth-1;var defaultSectionsWidth=(sideLength*3+2)*11+1;if(defaultSectionsWidth<minSectionsWidth){$("#bigSections li,#bigLayout").width(minSectionsWidth/(sideLength*3+2)-2).height(minSectionsHeight/(sideLength*2)-1);$("#colorPicker").height(minColorpickerHeight).width(minColorpickerWidth);$("#bigSections").height(minSectionsHeight).width(minSectionsWidth);}else{$("#bigSections").width(defaultSectionsWidth).height(sideLength*2*11);$("#colorPicker").width(defaultSectionsWidth+5).height(sideLength*2*11+29);}
$("#bigSections ul").find("li:last").css("border-bottom","1px solid #000000");$("#bigSections ul:last li").css("border-right","1px solid #000000");$("#bigSections li").hover(function(){var $this=$(this);$("#bigLayout").css({"left":$this.position().left,"top":$this.position().top}).show();var cor=$this.attr("data-color");$("#bigBgshowDiv").css("backgroundColor",cor);$("#bigHexColorText").val(cor);invokeCallBack(cor);},function(){$("#bigLayout").hide();});}else{var bgmage=new Image();bgmage.src=jspath+'images/bg.gif';$("#bigSections").height(134).width(222).addClass("color_picker_bgimage").empty();$("#colorPicker").width(227).height(163);$("#bigSections").unbind("mousemove").unbind("mouseout").mousemove(function(event){var xSections=getSections(sideLength*3+2);var ySections=getSections(sideLength*3);var $this=$(this);var cursorXPos=event.pageX-$this.offset().left;var cursorYPos=event.pageY-$this.offset().top;var xi=0;var yi=0;for(var i=0;i<(sideLength*3+2);i++){if(cursorXPos>=xSections[i][0]&&cursorXPos<=xSections[i][1]){xi=i;break;}}
for(var i=0;i<(sideLength*3);i++){if(cursorYPos>=ySections[i][0]&&cursorYPos<=ySections[i][1]){yi=i;break;}}
$("#bigLayout").css({"left":$this.position().left+xi*11,"top":$this.position().top+yi*11}).show();var hex=allColorArray[xi][yi];$("#bigBgshowDiv").css("backgroundColor",hex);$("#bigHexColorText").val(hex);invokeCallBack(hex);}).mouseout(function(){$("#bigLayout").hide();});}
$(this).bind("click",function(event){currentPicker=event.currentTarget;$("#bigBgshowDiv").css("backgroundColor","#000000");$("#bigHexColorText").val("#000000");var pos=calculatePosition($(currentPicker));$("#colorPicker").css({"left":pos.left+"px","top":pos.top+"px"}).fadeIn(300);var bigSectionsP=$("#bigSections").position();$("#bigLayout").css({"left":bigSectionsP.left,"top":bigSectionsP.top}).show();});};this.hidePicker=function(){$("#colorPicker").hide();};this.movePicker=function(){var pos=calculatePosition($(currentPicker));$("#colorPicker").css({"left":pos.left+"px","top":pos.top+"px"});$("#bigLayout").hide();};function calculatePosition($el){var offset=$el.offset();var compatMode=document.compatMode=='CSS1Compat';var w=compatMode?document.documentElement.clientWidth:document.body.clientWidth;var h=compatMode?document.documentElement.clientHeight:document.body.clientHeight;var pos={left:offset.left,top:offset.top+$el.height()+7};var $colorPicker=$("#colorPicker");if(offset.left+$colorPicker.width()>w){pos.left=offset.left-$colorPicker.width()-7;if(pos.left<0){pos.left=0;}}
if(offset.top+$el.height()+7+$colorPicker.height()>h){pos.top=offset.top-$colorPicker.height()-7;if(pos.top<0){pos.top=0;}}
return pos;}
function generateBlockcornerColor(r){var a=new Array(2);a[0]=[new RGB(r,0,0),new RGB(r,255,0)];a[1]=[new RGB(r,0,255),new RGB(r,255,255)];return a;}
function gradientColor(startColor,endColor){var gradientArray=[];gradientArray[0]=startColor;gradientArray[sideLength-1]=endColor;var averageR=Math.round((endColor.r-startColor.r)/sideLength);var averageG=Math.round((endColor.g-startColor.g)/sideLength);var averageB=Math.round((endColor.b-startColor.b)/sideLength);for(var i=1;i<sideLength-1;i++){gradientArray[i]=new RGB(startColor.r+i*averageR,startColor.g+i*averageG,startColor.b+i*averageB);}
return gradientArray;}
function getSections(sl){var sections=new Array(sl);var initData=0;for(var i=0;i<sl;i++){var temp=initData+1;initData+=11;sections[i]=[temp,initData];}
return sections;}
function RGBToHex(rgb){var hex="#";for(c in rgb){var h=rgb[c].toString(16).toUpperCase();if(h.length==1){hex+="0"+h;}else{hex+=h;}}
return hex;}
function RGB(r,g,b){this.r=Math.max(Math.min(r,255),0);this.g=Math.max(Math.min(g,255),0);this.b=Math.max(Math.min(b,255),0);}
function invokeCallBack(hex){var callback_=$(currentPicker).data("callback");if($.isFunction(callback_)){callback_(currentPicker,hex);}else if(callback_==undefined||callback_==""){$(currentPicker).val(hex);}else{if(callback_.charAt(0)!="#"){callback_="#"+callback_;}
$(callback_).val(hex);}}};$.fn.colorPicker=color_picker.showPicker;$.fn.colorPickerMove=color_picker.movePicker;$.fn.colorPickerHide=color_picker.hidePicker;})(jQuery);