// Author: Dion, biab@iinet.net.au, http://biab.howtojs.com/
// Permission granted to SimplytheBest.net to feature the script in the 
// DHTML script collection at http://simplythebest.net/info/dhtml_scripts.html

var fastCPU = false;
var capturing = false;
var colorDivNum = 0;
var clrDiv;
var imgWidth;
var xpos, ypos;
var xOffset = 0;	// For mouse coordinates
var yOffset = 0;	// For mouse coordinates
addary=new Array(255,1,1);
clrary=new Array(360);
var rgbAry = new Array(0,0,0);
var quad=new Array(-180,360,180,0);

for(i=0;i<6;i++)
{
	for(j=0;j<60;j++)
	{
		clrary[60*i+j]=new Array(3);
		for(k=0;k<3;k++)
		{
			clrary[60*i+j][k]=addary[k];
			addary[k]+=((Math.floor(65049010/Math.pow(3,i*3+k))%3)-1)*4;
		}
	}
}

var imgDiv;
var objNamesAry = new Array('mono', 'comp', 'triad', 'analog');
var objAry = new Array();
function init()
{ 
	for(i = 0; i < objNamesAry.length; i++){
		for(j = 1; j <= 3; j++){
			var divID = objNamesAry[i]+j;
			objAry[divID] = findDOM(divID, true);
		}
	}
	var x = Get_Cookie('xOffset');
	var y = Get_Cookie('yOffset');
	if(!x && !y){
		calibrateMouse();
	}else{
		xOffset = x;
		yOffset = y;
	}
 }
// Called at end of HTML, so that CSS styles have been applied
function postLoad()
{
	clrDiv = findDOM('clrdiv', true);
	imgWidth = document["colorwheel"].width;
	imgDiv = findDOM('imgdiv', false);
	if(imgDiv){
		imgDiv.onclick = setcolor;
		imgDiv.onmousemove = moved;
	}
}

function startCapture(){
	if(!capturing){
		capturing = true;
		imgDiv.onmousemove = moved;
	}
}
function endCapture(){
	if(capturing){
		capturing = false;
		imgDiv.onmousemove = null;
	}
}


function moved(e)
{
	xpos = ((window.Event)?e.screenY:window.event.clientY) - xOffset;
	ypos = ((window.Event)?e.screenX:window.event.clientX) - yOffset;
	if(fastCPU){
		startCapture();
		calculateColor();
		colorizeDivs();
	}
}

function calculateColor()
{
	var sx= xpos-256;
	var sy= ypos-256;
	var xa=Math.abs(sx);
	var ya=Math.abs(sy);
	var d=ya*45/xa;
	if(ya>xa){
		d=90-(xa*45/ya);
	}
	var deg = Math.floor(Math.abs(quad[2*((sy<0)?0:1)+((sx<0)?0:1)]-d));
	var n=0;
	var c="000000";
	var r=Math.sqrt((xa*xa)+(ya*ya));

	if(sx!=0 || sy!=0)
	{ 
		for(i=0;i<3;i++)
		{
			r2=clrary[deg][i]*r/128;
			if(r>128)
				r2+=Math.floor(r-128)*2;
			if(r2>255)
				r2=255;
			rgbAry[i] = Math.floor(r2);
		};
		col[0].setRGB(rgbAry[0], rgbAry[1], rgbAry[2]);
	}
	return false;
}

function colorizeDivs(){
	var c = col[0];
	var hex = c.getHex();

	// Set main color square's bgcolor
	clrDiv.backgroundColor = '#'+hex;

	// Monochromatic
	col[1].setHSV( c.H, c.S/2, 1-(1-c.V)/2);
	col[2].setHSV( c.H, c.S/4, 1-(1-c.V)/4);
	col[3].setHSV( c.H, 2*c.S/3, 2*c.V/3);
	for(i = 1; i <= 3; i++){
		objAry['mono'+i].backgroundColor = '#' + col[i].getHex();
	}

	// Complementary
	col[1].setHSV( c.H, c.S, c.V ); 
	col[1].rotate(180);
	col[2].setHSV( c.H, c.S/4, 1-(1-c.V)/4 );
	col[3].setHSV( col[1].H, col[1].S/4, 1-(1-col[1].V)/4 );
	for(i = 1; i <= 3; i++){
		objAry['comp'+i].backgroundColor = '#' + col[i].getHex();
	}

	// Triad, soft contrast
	var dif = 30;
	col[1].setHSV( c.H, c.S, c.V ); col[1].rotate(180-dif);
	col[2].setHSV( c.H, c.S, c.V ); col[2].rotate(180+dif);
	col[3].setHSV( c.H, c.S/4, 1-(1-c.V)/4);
	for(i = 1; i <= 3; i++){
		objAry['triad'+i].backgroundColor = '#' + col[i].getHex();
	}

	// Analogic
	dif = 30;
	col[1].setHSV( c.H, c.S, c.V );
	col[1].rotate(dif);
	col[2].setHSV( c.H, c.S, c.V );
	col[2].rotate(360-dif);
	col[3].setHSV( c.H, c.S, c.V );
	for(i = 1; i <= 3; i++){
		objAry['analog'+i].backgroundColor = '#' + col[i].getHex();
	}

	document.frm.hex.value="#"+hex;
	document.frm.hid.value="#"+hex;
	document.frm.r.value = c.R;
	document.frm.g.value = c.G;
	document.frm.b.value = c.B;

}

function setcolor()
{ 
	if(capturing){
		if(fastCPU){
			endCapture();
		}else{
			calculateColor();
			colorizeDivs();
		}
	}else{
		startCapture();
	}

}


function cssRgb2hex(cssRgb){
	var a = cssRgb.toString().match(/\d+/g);
	return rgb2hex(a[0], a[1], a[2]);
}

function rgb2hex(r, b, g){ return r.toString(16) + g.toString(16) + b.toString(16); }

function setHex(rgb)
{
	if(!colorDivNum) colorDivNum = 1;
	if(rgb.indexOf("#") != -1){
		col[0].setHex(rgb.substr(1,6));
	}else{
		var rgbArray = rgb.toString().match(/\d+/g);
		col[0].setRGB(rgbArray[0], rgbArray[1], rgbArray[2]);
	}
	clrDiv.backgroundColor = rgb;
	colorizeDivs();	
}
function toggleFast(){
	fastCPU = !fastCPU;
	document.frm.fast.checked=fastCPU;
}
function calibrateMouse()
{
	alert('Mouse calibration: Click on the tiny black dot in the top-left corner of the colorwheel image');
	imgDiv.onclick = calibrate;
	imgDiv.onmousemove = null;
}

function calibrate(e)
{
	xOffset = 1 * ((window.Event)?e.screenY:window.event.clientY);
	yOffset = 1 * ((window.Event)?e.screenX:window.event.clientX);
	imgDiv.onclick = setcolor;
	imgDiv.onmousemove = moved;
	alert('Mouse calibrated');
	Set_Cookie('xOffset', xOffset);
	Set_Cookie('yOffset', yOffset);
}

function Get_Cookie(name) {
    var start = document.cookie.indexOf(name+"=");
    var len = start+name.length+1;
    if ((!start) && (name != document.cookie.substring(0,name.length))) return null;
    if (start == -1) return null;
    var end = document.cookie.indexOf(";",len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len,end));
}

function Set_Cookie(name,value,expires,path,domain,secure) {
    document.cookie = name + "=" +escape(value) +
        ( (expires) ? ";expires=" + expires.toGMTString() : "") +
        ( (path) ? ";path=" + path : "") + 
        ( (domain) ? ";domain=" + domain : "") +
        ( (secure) ? ";secure" : "");
}

function Delete_Cookie(name,path,domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
        ( (path) ? ";path=" + path : "") +
        ( (domain) ? ";domain=" + domain : "") +
        ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
