/* jQCP - $ Color Picker v 0.2
 *  jQCP is distributed under the terms of the MIT license
 *  For more information visit http://jqframework.com/jqcp
 *  Copyright (C) 2009  jqframework.com
 * Do not remove this copyright message
 */

$j.jQCP={imagePath:"images/c_tuner.png",prefix:"",colorObj:null,init:function(id){var prefix=$j.jQCP.prefix;
var str='<table class="jqcp_table"><tr>';
str+='<td><div id="'+prefix+'jqcp" class="jqcp"><div id="'+prefix+'marker" class="jqcpmarker"></div></div></td>';
str+='<td><div id="'+prefix+'jqbr" class="jqbr"></td>';
str+='<td><div id="'+prefix+'jqbrp" class="jqbrp"><div id="'+prefix+'c_point" class="jqbrpointer"></div></div></td>';
str+="</tr></table>";
$j("#"+id).html(str);
$j("#"+prefix+"jqcp").click(function(e){$j.jQCP.clickCp(e.pageX,e.pageY);
});
$j("#"+prefix+"jqbr").click(function(e){$j.jQCP.clickBp(e.pageX,e.pageY);
});
$j("#"+prefix+"jqcp_h").change(function(e){if($j.jQCP.validHSL($j("#"+prefix+"jqcp_h").val(),"jqcp_h")){$j.jQCP.setHsl();
}});
$j("#"+prefix+"jqcp_s").change(function(e){if($j.jQCP.validHSL($j("#"+prefix+"jqcp_s").val(),"jqcp_s")){$j.jQCP.setHsl();
}});
$j("#"+prefix+"jqcp_l").change(function(e){if($j.jQCP.validHSL($j("#"+prefix+"jqcp_l").val(),"jqcp_l")){$j.jQCP.setHsl();
}});
$j("#"+prefix+"jqcp_r").change(function(e){if($j.jQCP.validRGB($j("#"+prefix+"jqcp_r").val(),"jqcp_r")){$j.jQCP.setRGB();
}});
$j("#"+prefix+"jqcp_g").change(function(e){if($j.jQCP.validRGB($j("#"+prefix+"jqcp_g").val(),"jqcp_g")){$j.jQCP.setRGB();
}});
$j("#"+prefix+"jqcp_b").change(function(e){if($j.jQCP.validRGB($j("#"+prefix+"jqcp_b").val(),"jqcp_b")){$j.jQCP.setRGB();
}});
$j(".jqcp_value").click(function(e){$j.jQCP.colorObj=this.id;
$j.jQCP.HextoRGB();
});
$j.jQCP.setCpPos();
$j.jQCP.setBpPos();
},validHSL:function(value,obj){if(!isNaN(value)){if(value>240){$j("#"+$j.jQCP.prefix+obj).val(240);
}else{if(value<0){$j("#"+$j.jQCP.prefix+obj).val(0);
}}return true;
}return false;
},validRGB:function(value,obj){if(!isNaN(value)){if(value>255){$j("#"+$j.jQCP.prefix+obj).val(255);
}else{if(value<0){$j("#"+$j.jQCP.prefix+obj).val(0);
}}return true;
}return false;
},setCpPos:function(){var pos=$j("#"+$j.jQCP.prefix+"jqcp").offset();
var x=parseInt($j("#"+$j.jQCP.prefix+"jqcp_h").val())-4;
var y=240-parseInt($j("#"+$j.jQCP.prefix+"jqcp_s").val())-4;
$j("#"+$j.jQCP.prefix+"marker").css({position:"relative",zIndex:5000,left:x+"px",top:y+"px"});
},setBpPos:function(){var pos=$j("#"+$j.jQCP.prefix+"jqbrp").offset();
var y=240-parseInt($j("#"+$j.jQCP.prefix+"jqcp_l").val())-5;
$j("#"+$j.jQCP.prefix+"c_point").css({position:"relative",zIndex:5000,top:y+"px"});
},hsl2rgb:function(){var h=parseInt($j("#"+$j.jQCP.prefix+"jqcp_h").val())/240;
var s=parseInt($j("#"+$j.jQCP.prefix+"jqcp_s").val())/240;
var l=parseInt($j("#"+$j.jQCP.prefix+"jqcp_l").val())/240;
var r,g,b;
if(s==0){r=g=b=l;
}else{function hue2rgb(p,q,t){if(t<0){t+=1;
}if(t>1){t-=1;
}if(t<1/6){return p+(q-p)*6*t;
}if(t<1/2){return q;
}if(t<2/3){return p+(q-p)*(2/3-t)*6;
}return p;
}var q=l<0.5?l*(1+s):l+s-l*s;
var p=2*l-q;
r=hue2rgb(p,q,h+1/3);
g=hue2rgb(p,q,h);
b=hue2rgb(p,q,h-1/3);
}$j("#"+$j.jQCP.prefix+"jqcp_r").val(Math.round(r*255));
$j("#"+$j.jQCP.prefix+"jqcp_g").val(Math.round(g*255));
$j("#"+$j.jQCP.prefix+"jqcp_b").val(Math.round(b*255));
$j.jQCP.RGBtoHex();
},rgb2hsl:function(){var r=parseInt($j("#"+$j.jQCP.prefix+"jqcp_r").val())/255;
var g=parseInt($j("#"+$j.jQCP.prefix+"jqcp_g").val())/255;
var b=parseInt($j("#"+$j.jQCP.prefix+"jqcp_b").val())/255;
var max=Math.max(r,g,b),min=Math.min(r,g,b);
var h,s,l=(max+min)/2;
if(max==min){h=s=0;
}else{var d=max-min;
s=l>0.5?d/(2-max-min):d/(max+min);
switch(max){case r:h=(g-b)/d+(g<b?6:0);
break;
case g:h=(b-r)/d+2;
break;
case b:h=(r-g)/d+4;
break;
}h/=6;
}$j("#"+$j.jQCP.prefix+"jqcp_h").val(Math.round(h*240));
$j("#"+$j.jQCP.prefix+"jqcp_s").val(Math.round(s*240));
$j("#"+$j.jQCP.prefix+"jqcp_l").val(Math.round(l*240));
$j.jQCP.RGBtoHex();
},clickCp:function(x,y){var pos=$j("#"+$j.jQCP.prefix+"jqcp").offset();
$j("#"+$j.jQCP.prefix+"jqcp_h").val(Math.round(x-pos.left));
$j.jQCP.validHSL($j("#"+$j.jQCP.prefix+"jqcp_h").val(),"jqcp_h");
$j("#"+$j.jQCP.prefix+"jqcp_s").val(Math.round(240-y+pos.top));
$j.jQCP.validHSL($j("#"+$j.jQCP.prefix+"jqcp_s").val(),"jqcp_s");
$j.jQCP.hsl2rgb();
$j.jQCP.changeBpBg();
x=x-4-pos.left;
y=y-4-pos.top;
pos=$j("#"+$j.jQCP.prefix+"jqcp").offset();
$j("#"+$j.jQCP.prefix+"marker").css({position:"relative",zIndex:5000,left:x+"px",top:y+"px"});
},clickBp:function(x,y){var pos=$j("#"+$j.jQCP.prefix+"jqbr").offset();
$j("#"+$j.jQCP.prefix+"jqcp_l").val(Math.round(240-y+pos.top));
$j.jQCP.validHSL($j("#"+$j.jQCP.prefix+"jqcp_l").val(),"jqcp_l");
$j.jQCP.hsl2rgb();
y=y-3-pos.top;
pos=$j("#"+$j.jQCP.prefix+"jqbrp").offset();
$j("#"+$j.jQCP.prefix+"c_point").css({position:"relative",zIndex:5000,top:y+"px"});
},changeBpBg:function(){if(navigator.appVersion.indexOf("MSIE")!=-1){$j("#"+$j.jQCP.prefix+"jqbr").css({backgroundImage:"none",filter:"progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=true, sizingMethod=crop, src='"+$j.jQCP.imagePath+"')",backgroundColor:$j("#"+$j.jQCP.prefix+$j.jQCP.colorObj).val()});
}else{$j("#"+$j.jQCP.prefix+"jqbr").css({backgroundColor:$j("#"+$j.jQCP.prefix+$j.jQCP.colorObj).val()});
}},RGBtoHex:function(){$j("#"+$j.jQCP.prefix+$j.jQCP.colorObj).val("#"+$j.jQCP.toHex($j("#"+$j.jQCP.prefix+"jqcp_r").val())+$j.jQCP.toHex($j("#"+$j.jQCP.prefix+"jqcp_g").val())+$j.jQCP.toHex($j("#"+$j.jQCP.prefix+"jqcp_b").val()));
$j("#"+$j.jQCP.prefix+$j.jQCP.colorObj).css({backgroundColor:$j("#"+$j.jQCP.prefix+$j.jQCP.colorObj).val()});
},HextoRGB:function(){var value=$j("#"+$j.jQCP.prefix+$j.jQCP.colorObj).val();
$j("#"+$j.jQCP.prefix+"jqcp_r").val($j.jQCP.HexToR(value));
$j("#"+$j.jQCP.prefix+"jqcp_g").val($j.jQCP.HexToG(value));
$j("#"+$j.jQCP.prefix+"jqcp_b").val($j.jQCP.HexToB(value));
$j.jQCP.setRGB();
},setRGB:function(){$j.jQCP.rgb2hsl();
$j.jQCP.setCpPos();
$j.jQCP.setBpPos();
$j.jQCP.changeBpBg();
},setHsl:function(){$j.jQCP.hsl2rgb();
$j.jQCP.setCpPos();
$j.jQCP.setBpPos();
$j.jQCP.changeBpBg();
},toHex:function(N){if(N==null){return"00";
}N=parseInt(N);
if(N==0||isNaN(N)){return"00";
}N=Math.max(0,N);
N=Math.min(N,255);
N=Math.round(N);
return"0123456789ABCDEF".charAt((N-N%16)/16)+"0123456789ABCDEF".charAt(N%16);
},HexToR:function(h){return parseInt(($j.jQCP.cutHex(h)).substring(0,2),16);
},HexToG:function(h){return parseInt(($j.jQCP.cutHex(h)).substring(2,4),16);
},HexToB:function(h){return parseInt(($j.jQCP.cutHex(h)).substring(4,6),16);
},cutHex:function(h){return(h.charAt(0)=="#")?h.substring(1,7):h;
},setColorObj:function(colorObj){$j.jQCP.colorObj=colorObj;
$j("#"+$j.jQCP.prefix+colorObj).unbind("change");
$j("#"+$j.jQCP.prefix+colorObj).bind("change",function(e){$j.jQCP.HextoRGB();
});
$j("#"+$j.jQCP.prefix+$j.jQCP.colorObj).css({backgroundColor:$j("#"+$j.jQCP.prefix+$j.jQCP.colorObj).val()});
},setColor:function(colorObj,color){$j.jQCP.colorObj=colorObj;
$j("#"+$j.jQCP.prefix+colorObj).val(color);
$j.jQCP.HextoRGB();
}};
$j.fn.extend({jqcp:function(){return this.each(function(){$j.jQCP.init(this.id);
});
},jqcp_setObject:function(){return this.each(function(){$j.jQCP.setColorObj(this.id);
});
},jqcp_setColor:function(color){return this.each(function(){$j.jQCP.setColor(this.id,color);
});
}});
