/* DHTML Color Picker, Programming by Ulyses, ColorJack.com */
/* updated by S.S. 4/6/07 */

function getElem(v) { return(document.getElementById(v)); }
function getStyle(v) { return(getElem(v).style); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function cpToggle(v) { getStyle(v).display=(getStyle(v).display=='none'?'block':'none'); }
function cpHide(v) { getStyle(v).display='none'; }
function within(v,a,z) { return((v>=a && v<=z)?true:false); }
function XY(e,v) { var z=agent('msie')?[event.clientX+document.body.scrollLeft,event.clientY+document.body.scrollTop]:[e.pageX,e.pageY]; return(z[zero(v)]); }
function XYwin(v) { var z=agent('msie')?[document.body.clientHeight,document.body.clientWidth]:[window.innerHeight,window.innerWidth]; return(!isNaN(v)?z[v]:z); }
function zero(v) { v=parseInt(v); return(!isNaN(v)?v:0); }
function zindex(d) { d.style.zIndex=zINDEX++; }

function cpFindPosY(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 cpFindPosX(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;
}

function cpShow(v, o) { 

	var x = cpFindPosX(o);
	var y = cpFindPosY(o);
	var w = o.offsetWidth;
	var p = getElem(v);

	p.style.display='block';
	p.style.top = (y-100)+'px';
	p.style.left = (x+w+2)+'px';
}

/* PLUGIN */

var maxValue={'h':360,'s':100,'v':100},HSV={0:360,1:100,2:100};
var SVHeight=165,wSV=162,wH=163,slideHSV={0:360,1:100,2:100},zINDEX=15,stop=1;

function HSVslide(d,o,e) {

	function tXY(e) { tY=XY(e,1)-top; tX=XY(e)-left; }
	function mkHSV(a,b,c) { return(Math.min(a,Math.max(0,Math.ceil((parseInt(c)/b)*a)))); }
	function ckHSV(a,b) { if(within(a,0,b)) return(a); else if(a>b) return(b); else if(a<0) return('-'+oo); }
	function drag(e) { 
		if(!stop) {
			if(d!='drag') tXY(e);

			if(d=='SVslide') { 
				
				ds.left=ckHSV(tX-oo,wSV)+'px'; 
				ds.top=ckHSV(tY-oo,wSV)+'px';
			
				slideHSV[1]=mkHSV(100,wSV,ds.left); 
				slideHSV[2]=100-mkHSV(100,wSV,ds.top);
				HSVupdate();
				
			}
			else if(d=='Hslide') {
				var ck=ckHSV(tY-oo,wH), j, r='hsv', z={};	
				
				ds.top=(ck-5)+'px'; 
				slideHSV[0]=mkHSV(360,wH,ck);
	 
				for(var i=0; i<=r.length-1; i++) { 
					j=r.substr(i,1); 
					z[i]=(j=='h')?maxValue[j]-mkHSV(maxValue[j],wH,ck):HSV[i]; 
				}
				
				HSVupdate(z); 
				getStyle('SV').backgroundColor='#'+hsv2hex([HSV[0],100,100]);

			}
			else if(d=='drag') { 
				ds.left=XY(e)+oX-eX+'px'; 
				ds.top=XY(e,1)+oY-eY+'px'; 
			}

		}
	}

	if(stop) { stop=''; var ds=getStyle(d!='drag'?d:o);

		if(d=='drag') { var oX=parseInt(ds.left), oY=parseInt(ds.top), eX=XY(e), eY=XY(e,1); zindex(getElem(o)); }
		else { var left=(getElem(o).offsetLeft+10), top=(getElem(o).offsetTop+22), tX, tY, oo=(d=='Hslide')?2:4; if(d=='SVslide') slideHSV[0]=HSV[0]; }

		document.onmousemove=drag; document.onmouseup=function(){ stop=1; document.onmousemove=''; document.onmouseup=''; }; drag(e);

	}
}


function HSVupdate(v) { 

	v=hsv2hex(HSV=v?v:slideHSV);

	// set values on page
	mkColor(v); 

	getElem('plugHEX').innerHTML=v;
	
	return(v);

}

function loadSV() { 
	var z='';

	for(var i=SVHeight; i>=0; i--) z+="<div style=\"BACKGROUND: #"+hsv2hex([Math.round((360/SVHeight)*i),100,100])+";\"><br /><\/div>";
	
	getElem('Hmodel').innerHTML=z;
	
}

function updateH(v) { 
	HSV=hex2hsv(v);
	getStyle('SV').backgroundColor='#'+hsv2hex(Array(HSV[0],100,100)); 
	getStyle('SVslide').top=(parseInt(wSV-wSV*(HSV[2]/100))-4)+'px'; getStyle('SVslide').left=parseInt(wSV*(HSV[1]/100))+'px';
	getStyle('Hslide').top=(parseInt(wH*((maxValue['h']-HSV[0])/maxValue['h']))-7)+'px';

	mkColor(v);
}

/* CONVERSIONS */

function toHex(v) { v=Math.round(Math.min(Math.max(0,v),255)); return("0123456789ABCDEF".charAt((v-v%16)/16)+"0123456789ABCDEF".charAt(v%16)); }
function hex2rgb(r) { return({0:parseInt(r.substr(0,2),16),1:parseInt(r.substr(2,2),16),2:parseInt(r.substr(4,2),16)}); }
function rgb2hex(r) { return(toHex(r[0])+toHex(r[1])+toHex(r[2])); }
function hsv2hex(h) { return(rgb2hex(hsv2rgb(h))); }	
function hex2hsv(v) { return(rgb2hsv(hex2rgb(v))); }

function rgb2hsv(r) { // easyrgb.com/math.php?MATH=M20#text20

	var max=Math.max(r[0],r[1],r[2]),delta=max-Math.min(r[0],r[1],r[2]),H,S,V;
	
	if(max!=0) { S=Math.round(delta/max*100);

		if(r[0]==max) H=(r[1]-r[2])/delta; else if(r[1]==max) H=2+(r[2]-r[0])/delta; else if(r[2]==max) H=4+(r[0]-r[1])/delta;

		var H=Math.min(Math.round(H*60),360); if(H<0) H+=360;

	}

	return({0:H?H:0,1:S?S:0,2:Math.round((max/255)*100)});

}

function hsv2rgb(r) { // easyrgb.com/math.php?MATH=M21#text21

	var R,B,G,S=r[1]/100,V=r[2]/100,H=r[0]/360;

	if(S>0) { if(H>=1) H=0;

		H=6*H; F=H-Math.floor(H);
		A=Math.round(255*V*(1.0-S));
		B=Math.round(255*V*(1.0-(S*F)));
		C=Math.round(255*V*(1.0-(S*(1.0-F))));
		V=Math.round(255*V); 

		switch(Math.floor(H)) {

			case 0: R=V; G=C; B=A; break;
			case 1: R=B; G=V; B=A; break;
			case 2: R=A; G=V; B=C; break;
			case 3: R=A; G=B; B=V; break;
			case 4: R=C; G=A; B=V; break;
			case 5: R=V; G=A; B=B; break;

		}

		return({0:R?R:0,1:G?G:0,2:B?B:0});

	}
	else return({0:(V=Math.round(V*255)),1:V,2:V});

}