/**
 * searchpanel.js
 *
 * copyright (c) 2004-2007 by Harald Lapp, PixelQuelle GmbH <harald.lapp@gmail.com>
 * all rights reserved!
 *
 * copyright of some portions:
 * - findPos (c) by ppk: http://www.quirksmode.org/js/findpos.html:
 */
dhtml={findPosX:function(obj){var curleft=0;if(obj.offsetParent)
{while(obj.offsetParent)
{curleft+=obj.offsetLeft
obj=obj.offsetParent;}}
else if(obj.x)
curleft+=obj.x;return curleft;},findPosY:function(obj){var curtop=0;if(obj.offsetParent)
{while(obj.offsetParent)
{curtop+=obj.offsetTop
obj=obj.offsetParent;}}
else if(obj.y)
curtop+=obj.y;return curtop;}}
function hsvpicker(pWheelId,pRadius){this.save_mmove=null;this.save_mup=null;this.wheelObj=document.getElementById(pWheelId);this.active=false;this.r=pRadius;this.mx=dhtml.findPosX(this.wheelObj);this.my=dhtml.findPosY(this.wheelObj);this.mw=this.wheelObj.offsetWidth*1;this.mh=this.wheelObj.offsetHeight*1;this.cx=Math.floor(this.mx+this.r);this.cy=Math.floor(this.my+this.r);if(document.getElementById(pWheelId+'crosshair')){this.crosshair=document.getElementById(pWheelId+'crosshair');}else{this.crosshair=null;}
this.h=0;this.s=1;this.v=100;this.callback=new Array();}
hsvpicker.prototype.init=function(phsv){if(arguments.length==1){this.h=phsv[0];this.s=phsv[1];this.v=phsv[2];}
this.mx=dhtml.findPosX(this.wheelObj);this.my=dhtml.findPosY(this.wheelObj);this.mw=this.wheelObj.offsetWidth*1;this.mh=this.wheelObj.offsetHeight*1;this.cx=Math.floor(this.mx+this.r);this.cy=Math.floor(this.my+this.r);var xy=this.get_xy();this.set_crosshair(xy[0],xy[1]);}
hsvpicker.prototype.add_callback=function(pCallback){this.callback[this.callback.length]=pCallback;}
hsvpicker.prototype.replace_callback=function(pCallback){this.callback=new Array();this.callback[0]=pCallback;}
function hsvpicker_observe(e,pobjname){var px=document.all?window.event.clientX:e.pageX;var py=document.all?window.event.clientY:e.pageY;eval(pobjname+'.save_mmove = document.onmousemove;');eval(pobjname+'.save_mup = document.onmouseup;');document.onmousemove=new Function("event","return "+pobjname+".move(event);");document.onmouseup=new Function("return "+pobjname+".detach()");if(document.all){if(window.event.preventDefault){window.event.preventDefault();}}else if(e){if(e.preventDefault){e.preventDefault();}}
eval(pobjname+'.active = true');eval(pobjname+'.select('+px+','+py+');');eval(pobjname+'.set_crosshair('+px+','+py+');');return false;}
function hsvpicker_onclick(e,pobjname){var px=document.all?window.event.clientX:e.pageX;var py=document.all?window.event.clientY:e.pageY;if(document.all){if(window.event.preventDefault){window.event.preventDefault();}}else if(e){if(e.preventDefault){e.preventDefault();}}
eval(pobjname+'.select('+px+','+py+');');eval(pobjname+'.set_crosshair('+px+','+py+');');return false;}
hsvpicker.prototype.move=function(e){if(!this.active){return;}
var px=document.all?window.event.clientX:e.pageX;var py=document.all?window.event.clientY:e.pageY;if(document.all){if(window.event.preventDefault){window.event.preventDefault();}}else if(e){if(e.preventDefault){e.preventDefault();}}
this.select(px,py);this.set_crosshair(px,py);return false;}
hsvpicker.prototype.select=function(px,py){if(this.in_range(px,py)){var dx=px-this.mx-this.r;var dy=py-this.my-this.r;if(dx*dx+dy*dy<=this.r*this.r){this.h=Math.floor(Math.atan2(dx,dy)*180/Math.PI);if(this.h<0){this.h+=360;}
this.s=Math.floor(Math.sqrt(dx*dx+dy*dy)/this.r*100);if(this.s>100){this.s=100;}}else{this.h=0;this.s=0;this.v=100;}
document.getElementById('debh').value=[px,py,'-',dx,dy,'-',this.mx,this.my];for(i=0;i<this.callback.length;i++){this.callback[i]([this.h,this.s,this.v]);}}}
hsvpicker.prototype.get_xy=function(){var w=this.h*(Math.PI/180);var x=Math.floor(Math.sin(w)*this.s+this.cx);var y=Math.floor(Math.cos(w)*this.s+this.cy);document.getElementById('deby').value=[x,y,this.h,this.s,this.v,w];return[x,y];}
hsvpicker.prototype.in_range=function(px,py){return((px>=this.mx)&&(py>=this.my)&&(px<=this.mx+this.mw)&&(py<=this.my+this.mh));}
hsvpicker.prototype.set_crosshair=function(px,py){if((this.in_range(px,py))&&(this.crosshair)){this.crosshair.style.left=(px-this.crosshair.offsetWidth*1-1)+'px';this.crosshair.style.top=(py-this.crosshair.offsetHeight*1-1)+'px';document.getElementById('debx').value=[px,py,' | ',this.mx,this.my,'-',this.mx+this.mw,this.my+this.mh];}}
hsvpicker.prototype.get=function(){return new Array(this.h,this.s,this.v);}
hsvpicker.prototype.set=function(pparam){this.h=pparam[0];this.s=pparam[1];this.v=pparam[2];}
hsvpicker.prototype.detach=function(){if(this.active){document.onmousemove=this.save_mmove;document.onmouseup=this.save_mup;this.active=false;}}
function hsvpicker_detach(e,pobjname){eval(pobjname+'.detach();');}
mouse={debug:false,mx:0,my:0,init:function(pdebug){document.onmousemove=mouse.observe;},observe:function(e){mouse.mx=document.all?window.event.clientX:e.pageX;mouse.my=document.all?window.event.clientY:e.pageY;}}
function color(p){this.r=p[0];this.g=p[1];this.b=p[2];this.observer=new Array();}
color.prototype.cmyk2rgb=function(p){c=(p[0]/100);m=(p[1]/100);y=(p[2]/100);k=(p[3]/100);var r=Math.floor((1-Math.min(1,c*(1-k)+k))*255);var g=Math.floor((1-Math.min(1,m*(1-k)+k))*255);var b=Math.floor((1-Math.min(1,y*(1-k)+k))*255);return new Array(r,g,b);}
color.prototype.rgb2cmyk=function(p){var r=(p[0]/255);var g=(p[1]/255);var b=(p[2]/255);var k=Math.min(1-r,Math.min(1-g,1-b));var c=Math.floor(((1-r-k)/(1-k))*100);var m=Math.floor(((1-g-k)/(1-k))*100);var y=Math.floor(((1-b-k)/(1-k))*100);var k=Math.floor(k*100);return new Array(c,m,y,k);}
color.prototype.hsv2rgb=function(p){var h=(p[0]%360);var s=p[1]/100;var v=p[2]/100;if(h<0){h+=360;}
h=h/60;var i=Math.floor(h);var f=h-i;var p=v*(1-s);var q=v*(1-s*f);var t=v*(1-s*(1-f));if(i==0){r=v;g=t;b=p;}else if(i==1){r=q;g=v;b=p;}else if(i==2){r=p;g=v;b=t;}else if(i==3){r=p;g=q;b=v;}else if(i==4){r=t;g=p;b=v;}else{r=v;g=p;b=q;}
return new Array(Math.floor(r*255),Math.floor(g*255),Math.floor(b*255));}
color.prototype.rgb2hsv=function(p){var r=p[0]/255;var g=p[1]/255;var b=p[2]/255;var h=0;var max=Math.max(r,Math.max(g,b));var min=Math.min(r,Math.min(g,b));if(max==min){h=0;}else if(r==max){h=(g-b)/(max-min)*60;}else if(g==max){h=(2+(b-r)/(max-min))*60;}else if(b==max){h=(4+(r-g)/(max-min))*60;}
var h=Math.floor(h);var s=Math.floor((max-min)/max*100);var v=Math.floor(max*100);if(h<0){h+=360;}
if(max==0){s=0;}
return new Array(h,s,v);}
color.prototype.bw2rgb=function(p){var bw=Math.floor((p[0]/100)*255);return new Array(bw,bw,bw);}
color.prototype.rgb2bw=function(p){var bw=Math.floor((p[0]*0.31+p[1]*0.46+p[2]*0.23)/255*100);return new Array(bw,bw,bw);}
color.prototype.set=function(p){var rgb=p;this.r=p[0];this.g=p[1];this.b=p[2];}
color.prototype.get=function(){return new Array(this.r,this.g,this.b);}
function slider(pSliderId,pGaugeId,pMin,pMax){this._VSLIDER=1;this._HSLIDER=2;this.save_mmove=null;this.save_mup=null;this.active=false;this.enabled=true;this.sliderObj=document.getElementById(pSliderId);this.x=dhtml.findPosX(this.sliderObj);this.y=dhtml.findPosY(this.sliderObj);this.w=this.sliderObj.offsetWidth*1;this.h=this.sliderObj.offsetHeight*1;this.Xoffset=0;this.Yoffset=0;this.track_x1=dhtml.findPosX(this.sliderObj.parentNode);this.track_y1=dhtml.findPosY(this.sliderObj.parentNode);this.track_w=this.sliderObj.parentNode.offsetWidth;this.track_h=this.sliderObj.parentNode.offsetHeight;this.track_x2=this.track_x1+this.track_w;this.track_y2=this.track_y1+this.track_h;if(this.track_w>this.track_h){this.type=this._HSLIDER;}else{this.type=this._VSLIDER;}
this.gaugeObj=document.getElementById(pGaugeId);this.gauge_min=Math.min(pMin,pMax);this.gauge_max=Math.max(pMin,pMax);this.gauge_scale=this.gauge_max-this.gauge_min;this.value=this.gauge_min;this.gauge_prec=0;if(this.type==this._HSLIDER){this.gauge_unit=this.gauge_scale/(this.track_w-this.w);}else{this.gauge_unit=this.gauge_scale/(this.track_h-this.h);}
document.getElementById('deby').value=this.type+' '+this.track_w+' '+this.w+' '+this.gauge_scale;this.callback=new Array();this.show();}
slider.prototype.init=function(pMin,pMax,pValue){this.gauge_min=Math.min(pMin,pMax);this.gauge_max=Math.max(pMin,pMax);this.gauge_scale=this.gauge_max-this.gauge_min;this.value=pValue;this.gauge_prec=0;this.show();if(this.type==this._HSLIDER){this.gauge_unit=this.gauge_scale/(this.track_w-this.w);}else{this.gauge_unit=this.gauge_scale/(this.track_h-this.h);}
this.set(pValue);}
slider.prototype.show=function(){this.enable();document.getElementById(this.sliderObj.id+'box').style.visibility='visible';}
slider.prototype.hide=function(){this.enabled=false;document.getElementById(this.sliderObj.id+'box').style.visibility='hidden';}
slider.prototype.enable=function(){this.enabled=true;this.sliderObj.src='searchpanel/img/slider.gif';this.gaugeObj.disabled='';this.onenable();}
slider.prototype.disable=function(){this.enabled=false;this.sliderObj.src='searchpanel/img/slider_disabled.gif';this.gaugeObj.disabled='disabled';this.ondisable();}
slider.prototype.add_callback=function(pCallback){this.callback[this.callback.length]=pCallback;}
slider.prototype.replace_callback=function(pCallback){this.callback=new Array();this.callback[0]=pCallback;}
slider.prototype.onchange=function(){}
slider.prototype.ondisable=function(){}
slider.prototype.onenable=function(){}
slider_observe=function(e,pobjname){if(eval(pobjname+'.active')){return;}
eval(pobjname+'.active = true;');eval(pobjname+'.save_mmove = document.onmousemove;');eval(pobjname+'.save_mup = document.onmouseup;');eval(pobjname+'.Xoffset = ('+pobjname+'.x - mouse.mx);');eval(pobjname+'.Yoffset = ('+pobjname+'.y - mouse.my);');document.onmousemove=new Function("event","return "+pobjname+".move(event);");document.onmouseup=new Function("return "+pobjname+".detach()");if(document.all){if(window.event.preventDefault){window.event.preventDefault();}}else if(e){if(e.preventDefault){e.preventDefault();}}
return false;}
gauge_change=function(pobjname){eval(pobjname+'.set('+pobjname+'.gaugeObj.value);');}
slider.prototype.move=function(e){if((this.sliderObj==null)||(this.gaugeObj==null)){return false;}
if(!this.enabled){return false;}
if(this.type==this._HSLIDER){this.x=document.all?window.event.clientX:e.pageX;this.x+=this.Xoffset;if(this.x<this.track_x1){this.x=this.track_x1;}else if(this.x+this.w>this.track_x2){this.x=this.track_x2-this.w;}
var pos=this.x-this.track_x1;var val=pos*this.gauge_unit;if(this.x==this.track_x1){val=this.gauge_min;}else if(this.x+this.w==this.track_x2){val=this.gauge_max;}
this.sliderObj.style.left=pos+'px';}else{this.y=document.all?window.event.clientY:e.pageY;this.y+=this.yoffset;if(this.y<this.track_y1){this.y=this.track_y1;}else if(this.y+this.h>this.track_y2){this.y=this.track_y2-this.h;}
var pos=this.y-this.track_y1;var val=pos*this.gauge_unit;if(this.y==this.track_y1){val=this.gauge_min;}else if(this.y+this.h==this.track_y2){val=this.gauge_max;}
this.sliderObj.style.top=pos;}
document.getElementById('debx').value='this.x: '+this.x+' | this.Xoffset: '+this.Xoffset+' | mouse.mx: '+mouse.mx;this.display(val);if(document.all){if(window.event.preventDefault){window.event.preventDefault();}}else if(e.preventDefault){e.preventDefault();}
return false;}
slider.prototype.detach=function(){document.onmousemove=this.save_mmove;document.onmouseup=this.save_mup;this.active=false;}
slider.prototype.validate=function(pvalue){var val=parseInt(pvalue);if((isNaN(val))||(val<this.gauge_min)){val=this.gauge_min;}else if(val>this.gauge_max){val=this.gauge_max;}
return val;}
slider.prototype.display=function(pvalue){var val=this.validate(pvalue);var p=Math.pow(10,this.gauge_prec);this.value=Math.round(val*p)/p;if(this.gaugeObj!=null){this.gaugeObj.value=this.value;}
for(i=0;i<this.callback.length;i++){this.callback[i](val);}}
slider.prototype.set=function(pvalue){if(!this.enabled){pvalue=0;}
var val=this.validate(pvalue);var pos=Math.round(val/this.gauge_unit);if(this.type==this._HSLIDER){this.x=pos+this.track_x1;this.sliderObj.style.left=pos+'px';}else{this.y=pos+this.track_y1;this.sliderObj.style.top=pos;}
document.getElementById('debx').value='this.x: '+this.x+' | this.Xoffset: '+this.Xoffset+' | mouse.mx: '+mouse.mx+' | pos: '+pos;this.display(val);}
slider.prototype.get=function(){return this.value;}
slider.prototype.ondisable=function(){var name='color'+this.sliderObj.id.substr(6,1);if(document.getElementById(name)){color.disable(name)}}
slider.prototype.onenable=function(){var name='color'+this.sliderObj.id.substr(6,1);if(obj=document.getElementById(name)){color.enable(name);}}
slider.prototype.onchange=function(){var no=this.sliderObj.id.substr(6,1);if(obj=document.getElementById('color'+no)){color.show(colorpane.pane,no,this.value);}}
color.prototype.disable=function(pid){obj=document.getElementById(pid)
obj.style.backgroundImage='url(searchpanel/img/disabled.gif)';obj.style.borderColor='#bbbbbb';}
color.prototype.enable=function(pid){obj=document.getElementById(pid)
obj.style.backgroundImage='';obj.style.borderColor='#000000';}
color.prototype.toRGB=function(pcolorsys,param){if(pcolorsys=='cmyk'){var rgb=this.cmyk2rgb(param);}else if(pcolorsys=='rgb'){var rgb=param;}else if(pcolorsys=='hsv'){var rgb=this.hsv2rgb(param);}else if(pcolorsys=='bw'){var rgb=this.bw2rgb(param);}
return rgb;}
color.prototype.show=function(pcolorsys,pno,param){var rgb=this.toRGB(pcolorsys,param);document.getElementById('color'+pno).style.backgroundColor='rgb('+rgb[0]+','+rgb[1]+','+rgb[2]+')';for(i=0;i<param.length;i++){param[i]=eval('slider_'+(i+1)+".get();");}
rgb=this.toRGB(pcolorsys,param);document.getElementById('rgbColor').style.backgroundColor='rgb('+rgb[0]+','+rgb[1]+','+rgb[2]+')';color.set(rgb);}
color.prototype.hsvshow=function(ptype,pno,pvalue){if(ptype=='wheel'){if(pno==0){var param=[pvalue[0],100,slider_5.get()];}else{var param=hsvpicker_1.get();param[2]=pvalue;hsvpicker_1.set(param);param[1]=100;}
var rgb=this.toRGB('hsv',param);document.getElementById('color5').style.backgroundColor='rgb('+rgb[0]+','+rgb[1]+','+rgb[2]+')';var rgb=this.toRGB('hsv',hsvpicker_1.get());document.getElementById('rgbColor').style.backgroundColor='rgb('+rgb[0]+','+rgb[1]+','+rgb[2]+')';}else if(ptype=='slider'){var param=[[slider_1.get(),100,100],[slider_1.get(),slider_2.get(),100],[slider_1.get(),100,slider_3.get()]];var rgb;param[pno-1][pno-1]=pvalue;for(i=0;i<3;i++){rgb=this.toRGB('hsv',param[i]);document.getElementById('color'+(i+1)).style.backgroundColor='rgb('+rgb[0]+','+rgb[1]+','+rgb[2]+')';}
var rgb=this.toRGB('hsv',[slider_1.get(),slider_2.get(),slider_3.get()]);document.getElementById('rgbColor').style.backgroundColor='rgb('+rgb[0]+','+rgb[1]+','+rgb[2]+')';}
color.set(rgb);}
function chg_format(pobj){var no=pobj.id.substr(pobj.id.length-1,1);if(no<3){if((document.getElementById('sp_fmt_3').checked==false)&&(document.getElementById('sp_fmt_'+(3-no)).checked==false)){document.getElementById('sp_fmt_'+(3-no)).checked=true;}
document.getElementById('sp_fmt_3').checked=false;}else if(no==3){document.getElementById('sp_fmt_3').checked=true;document.getElementById('sp_fmt_1').checked=false;document.getElementById('sp_fmt_2').checked=false;}}
function mm_clear(pid){document.getElementById(pid+'min').value='';document.getElementById(pid+'max').value='';}
spanel={send:function(){document.getElementById('spcolorcode').value=color.get();document.searchpanel.submit();},rset:function(){document.searchpanel.reset();colorpane.reset();var pane=colorpane.container.options[colorpane.container.options.selectedIndex].value;if(colorpane.pane!=pane){colorpane.init(pane);}}}
pane={show:function(pobj,pid){if(pobj.checked){document.getElementById(pid).style.display='block';}else{document.getElementById(pid).style.display='none';}}}
colorpane={container:null,pane:'',init:function(ppane){colorpane.container=document.getElementById('sp_paneselector');var pobj=colorpane.container;if(ppane=='hsvwheel'){document.getElementById('sliderpane').style.display='none';document.getElementById('hsvwheelpane').style.display='block';var hsv=color.rgb2hsv(color.get());hsvpicker_1.init(hsv);slider_5.init(0,100,hsv[2]);slider_5.replace_callback(new Function("pvalue","color.hsvshow('wheel',5,pvalue)"));color.hsvshow('wheel',5,hsv[2]);}else{document.getElementById('sliderpane').style.display='block';document.getElementById('hsvwheelpane').style.display='none';if(ppane=='cmyk'){var sliders=[true,true,true,true];var cmyk=color.rgb2cmyk(color.get());slider_1.init(0,100,cmyk[0]);slider_2.init(0,100,cmyk[1]);slider_3.init(0,100,cmyk[2]);slider_4.init(0,100,cmyk[3]);slider_1.replace_callback(new Function("pvalue","color.show('cmyk',1,[pvalue,0,0,0])"));slider_2.replace_callback(new Function("pvalue","color.show('cmyk',2,[0,pvalue,0,0])"));slider_3.replace_callback(new Function("pvalue","color.show('cmyk',3,[0,0,pvalue,0])"));slider_4.replace_callback(new Function("pvalue","color.show('cmyk',4,[0,0,0,pvalue])"));color.show('cmyk',1,[cmyk[0],0,0,0]);color.show('cmyk',2,[0,cmyk[1],0,0]);color.show('cmyk',3,[0,0,cmyk[2],0]);color.show('cmyk',4,[0,0,0,cmyk[3]]);}else if(ppane=='rgb'){var sliders=[true,true,true,false];var rgb=color.get();slider_1.init(0,255,rgb[0]);slider_2.init(0,255,rgb[1]);slider_3.init(0,255,rgb[2]);slider_4.hide();slider_1.replace_callback(new Function("pvalue","color.show('rgb',1,[pvalue,0,0])"));slider_2.replace_callback(new Function("pvalue","color.show('rgb',2,[0,pvalue,0])"));slider_3.replace_callback(new Function("pvalue","color.show('rgb',3,[0,0,pvalue])"));color.show('rgb',1,[rgb[0],0,0]);color.show('rgb',2,[0,rgb[1],0]);color.show('rgb',3,[0,0,rgb[2]]);}else if(ppane=='hsv'){var sliders=[true,true,true,false];var hsv=color.rgb2hsv(color.get());slider_1.init(0,360,hsv[0]);slider_2.init(0,100,hsv[1]);slider_3.init(0,100,hsv[2]);slider_4.hide();slider_1.replace_callback(new Function("pvalue","color.hsvshow('slider',1,pvalue)"));slider_2.replace_callback(new Function("pvalue","color.hsvshow('slider',2,pvalue)"));slider_3.replace_callback(new Function("pvalue","color.hsvshow('slider',3,pvalue)"));color.show('hsv',1,[hsv[0],100,100]);color.show('hsv',2,[0,hsv[1],0]);color.show('hsv',3,[0,0,hsv[2]]);}else if(ppane=='bw'){var sliders=[true,false,false,false];var bw=color.rgb2bw(color.get());slider_1.init(0,100,bw[0]);slider_2.hide();slider_3.hide();slider_4.hide();slider_1.replace_callback(new Function("pvalue","color.show('bw',1,[pvalue])"));color.show('bw',1,[bw[0]]);}}
this.pane=ppane;},reset:function(){if(this.pane=='hsvwheel'){gauge_change('slider_5');}else{gauge_change('slider_1');gauge_change('slider_2');gauge_change('slider_3');if(this.pane=='cmyk'){gauge_change('slider_4');}}
gauge_change('t_slider');},enable:function(){for(i=1;i<=5;i++){eval('slider_'+i+'.enable();');}
t_slider.enable();this.container.disabled='';color.enable('rgbColor');},disable:function(){this.init('cmyk');for(i=1;i<=5;i++){eval('slider_'+i+'.disable();');}
t_slider.disable();this.container.options[this.container.options.selectedIndex].selected=false;this.container.options[0].selected=true;this.container.disabled='disabled';color.disable('rgbColor');},select:function(pobj){var val=pobj.options[pobj.options.selectedIndex].value;colorpane.init(val);}}
window.onload=function(e){mouse.init();color=new color([255,255,255]);slider_1=new slider('slider1','gauge1',0,100);slider_2=new slider('slider2','gauge2',0,100);slider_3=new slider('slider3','gauge3',0,100);slider_4=new slider('slider4','gauge4',0,100);slider_5=new slider('slider5','gauge5',0,100);t_slider=new slider('tSlider','tGauge',0,100);t_slider.set(40);hsvpicker_1=new hsvpicker('hsvpicker1',45);hsvpicker_1.add_callback(new Function("parray","color.hsvshow('wheel',0,parray)"));colorpane.init('cmyk');}
