

// Sphere Plugin v0.1
// Design/Programming by Ulyses
// © 2007 ColorJack.com

// IE fix's by Hamish

/* GLOBALS */
function inita()
{
$S('mini').left=((XYwin()[1]/2)-350)+'px';
$S('mini').display='block';


}
var zINDEX=1000, hsv=[0,0,100];
function $(v) { return(document.getElementById(v)); }
function $S(v) { return(document.getElementById(v).style); }
function absPos(e) { var r={x:e.offsetLeft,y:e.offsetTop}; if(e.offsetParent) { var v=absPos(e.offsetParent); r.x+=v.x; r.y+=v.y; } return(r); }
function agent(v) { return(Math.max(navigator.userAgent.toLowerCase().indexOf(v),0)); }
function isset(v) { return((typeof(v)=='undefined' || v.length==0)?false:true); }
function toggle(i,t,xy) { var v=$S(i); v.display=t?t:(v.display=='none'?'block':'none'); if(xy) { v.left=xy[0]; v.top=xy[1]; } }
function XY(e,v) { var z=agent('msie')?Array(event.clientX+document.body.scrollLeft,event.clientY+document.body.scrollTop):Array(e.pageX,e.pageY); return(v==3?z: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++; }	

/* PLUGIN */

var stop=1;

function cords(W) {

	var W2=W/2, rad=(hsv[0]/360)*(Math.PI*2), hyp=(hsv[1]+(100-hsv[2]))/100*(W2/2);

	$S('mCur').left=Math.round(Math.abs(Math.round(Math.sin(rad)*hyp)+W2+3))+'px';
	$S('mCur').top=Math.round(Math.abs(Math.round(Math.cos(rad)*hyp)-W2-21))+'px';

}

function coreXY(o,e,xy,z,fu) {

	function point(a,b,e) { eZ=XY(e,3); commit([eZ[0]+a,eZ[1]+b]); }
	function M(v,a,z) { return(Math.max(!isNaN(z)?z:0,!isNaN(a)?Math.min(a,v):v)); }

	function commit(v) { if(fu) fu(v);
	
		if(o=='mCur') { var W=parseInt($S('mSpec').width), W2=W/2, W3=W2/2; 

			var x=v[0]-W2-3, y=W-v[1]-W2+21, SV=Math.sqrt(Math.pow(x,2)+Math.pow(y,2)), hue=Math.atan2(x,y)/(Math.PI*2);

			hsv=[hue>0?(hue*360):((hue*360)+360), SV<W3?(SV/W3)*100:100, SV>=W3?Math.max(0,1-((SV-W3)/(W2-W3)))*100:100];

			$('mHEX').innerHTML=hsv2hex(hsv); $S('plugID').background='#'+$('mHEX').innerHTML; cords(W);

		}
		else {
		
			if(xy) v=[M(v[0],xy[0],xy[2]), M(v[1],xy[1],xy[3])]; // XY LIMIT

			if(!xy || xy[0]) d.left=v[0]+'px'; if(!xy || xy[1]) d.top=v[1]+'px';

		}
	}

	if(stop) { stop=''; var d=$S(o), eZ=XY(e,3); if(!z) zindex($(o));

		if(o=='mCur') { var ab=absPos($(o).parentNode); point(-(ab['x']-1),-(ab['y']-28),e); }
		
		document.onmousemove=function(e){ if(!stop) point(oX,oY,e); };
		document.onmouseup=function(){ stop=1; document.onmousemove=''; document.onmouseup=''; };

	}
}

/* FX */

function fadeOp(v,id,s,top) { var ob=$S(id), b=v/100; ob.opacity=b; ob.top=top+'px'; ob.MozOpacity=b; ob.KhtmlOpacity=b; ob.filter="alpha(opacity="+v+")"; }

function fade(id,ln,s) { var top=zero(parseInt($S(id).top));

    function opacity(oStart,oEnd,ln) { var speed=Math.round(ln/100),timer=0;
    
        if(oStart>oEnd) { for(i=oStart; i>=oEnd; i--) { setTimeout("fadeOp("+i+",'"+id+"','','"+(top--)+"')",timer*speed); timer++; } setTimeout("toggle('"+id+"','none')",timer*speed); }
        else if(oStart<oEnd) { toggle(id,'block'); for(i=oStart; i<=oEnd; i++) { setTimeout("fadeOp("+i+",'"+id+"','"+1+"','"+(top++)+"')",timer*speed); timer++; } }
  
    }

    if(!$S(id).opacity || $S(id).opacity!=s) if(s==0) opacity(100,0,ln); else if($S(id).opacity) opacity(0,100,ln);

}

/* 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 rgb2hex(r) { return(toHex(r[0])+toHex(r[1])+toHex(r[2])); }
function hsv2hex(h) { return(rgb2hex(hsv2rgb(h))); }

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([R?R:0,G?G:0,B?B:0]);

    }
    else return([(V=Math.round(V*255)),V,V]);

}

function hextoRGB(hx)
{
	var R = HexToR(hx);
	var G = HexToG(hx);
	var B = HexToB(hx);
	return R + ',' + G + ',' + B;
}

function HexToR(h) {
	return parseInt((cutHex(h)).substring(0,2),16)
}
function HexToG(h) {
	return parseInt((cutHex(h)).substring(2,4),16)
}
function HexToB(h) {
	return parseInt((cutHex(h)).substring(4,6),16)
}
function cutHex(h) {
	return (h.charAt(0)=="#") ? h.substring(1,7):h
}


