// Constants
var NAME=0;
var VALUE=1;
var BITS=2;
var CELL=3;
var SAMPLE=4;
var SAMPLETEXT=5;
var VERSUS=6;
var VERSUSTEXT=7;
var CSSCLASS=8;
var STYLE=9;
var HUE=10;
// Vars
var CP_pickers=0;
var CP_config=new Array();
var CP_pCurrent=1;
var CP_pPrevious=1;
var CP_time;
var CP_div=null;

function ColorPicker() {
	if(ColorPicker.arguments.length==0) return;
	CP_pickers++;
	CP_config[0]=CP_pickers;
	CP_config[CP_pickers]=new Array('','',15,10,true,true,false,false,'','font-size:11px;',255);
	CP_config[CP_pickers][NAME]=ColorPicker.arguments[0];
	for(var loop=1;loop<ColorPicker.arguments.length;loop+=2) {
		CP_config[CP_pickers][ColorPicker.arguments[loop]]=ColorPicker.arguments[loop+1];
	}
	CP_config[CP_pickers][BITS]=Math.round(255/CP_config[CP_pickers][BITS]);
	html='';
	if(CP_div==null) {
		CP_div=document.getElementById('ColorPicker');
		CP_div.innerHTML='';
		CP_div.style.zIndex=zIndex();
		CP_div.style.visibility='hidden';
		CP_div.style.cursor='crosshair';
		CP_div.style.position='absolute';
		CP_div.style.border='1px solid #000000';
		CP_div.style.backgroundColor='#ffffff';
	}
}

function CP_PopUp_open(oObj) {
	CP_pPrevious=CP_pCurrent;
	CP_pCurrent=parseInt(oObj.id.replace('CP_Span',''));
	if(CP_div.innerHTML=='') CP_PopUp_build();
	if(CP_pCurrent!=CP_pPrevious) {
		if(CP_config[CP_pCurrent][BITS]!=CP_config[CP_pPrevious][BITS]) CP_PopUp_build();
		if(CP_config[CP_pCurrent][CELL]!=CP_config[CP_pPrevious][CELL]) CP_PopUp_build();
	}
	var aux=document.getElementById('CP_Span'+CP_pCurrent).style.backgroundColor.toUpperCase();
	if(aux.substr(0,3)=='RGB') {
		aux=eval(aux.replace('RGB','CP_RGBcolor2HEX'));
	}
	aux=CP_HEXcolor2RGB(aux);
	if(isNaN(aux[0])) {
		aux[0]=CP_config[CP_pCurrent][HUE];
	}
	CP_PopUp_change_Red(aux[0]);
	if(CP_config[CP_pCurrent][SAMPLE]==true) CP_PopUp_sample(oObj);
	if(CP_config[CP_pCurrent][VERSUS]) CP_PopUp_sample(oObj,'CP_showColorVersus');
	t=oObj.offsetTop-parseInt(oObj.style.height)*7; //+10;
	l=oObj.offsetLeft+23;
	while(oObj.offsetParent) {
		t+=oObj.offsetParent.offsetTop;
		l+=oObj.offsetParent.offsetLeft;
		oObj=oObj.offsetParent;
	}
	CP_div.style.top=t;
	CP_div.style.left=l;
	CP_div.style.visibility='visible';
}

function CP_PopUp_close(t) {
	if(isNaN(t)) t=1000;
	CP_time=setTimeout("CP_div.style.visibility='hidden';",t);
}

function CP_PopUp_show() {
	clearTimeout(CP_time);
	CP_div.style.visibility='visible';
}

function CP_PopUp_sample(oObj,CP_place) {
	var showText=null;
	if(CP_PopUp_sample.arguments.length==1) {
		CP_place='CP_showColor';
	}
	if(CP_place=='CP_showColor') {
		if(!CP_config[CP_pCurrent][SAMPLE]) return;
		textPlace='CP_showValue';
		showText=CP_config[CP_pCurrent][SAMPLETEXT];
	}
	if(CP_place=='CP_showColorVersus') {
		if(!CP_config[CP_pCurrent][VERSUS]) return;
		textPlace='CP_showValueVersus';
		showText=CP_config[CP_pCurrent][VERSUSTEXT];
	}
	document.getElementById(CP_place).style.backgroundColor=oObj.style.backgroundColor;
	var CP_value=oObj.style.backgroundColor.toUpperCase();
	var CP_valueHEX='';
	var CP_valueRGB='';
	if(showText) {
		if(CP_value.substr(0,3)=='RGB') {
			CP_valueRGB=CP_value;
			var aux=CP_value.replace('RGB','CP_RGBcolor2HEX');
			CP_valueHEX=eval(aux);
			var aux2=CP_HEXcolor2RGB(CP_valueHEX);
		} else if(CP_value.substr(0,1)=='#') {
			CP_valueHEX=CP_value;
			var aux2=CP_HEXcolor2RGB(CP_value);
			CP_valueRGB='RGB('+aux2[0]+','+aux2[1]+','+aux2[2]+')';
		}
		var bright=(aux2[0]+aux2[1]+aux2[2])/3;
		if(bright>153 || aux2[1]>153) var textColor='#000000';
		else var textColor='#ffffff';
	} else {
		var textColor='#000000';
	}
	document.getElementById(textPlace).innerHTML='<b style="color:'+textColor+'">'+CP_valueHEX+'<br />'+CP_valueRGB+'</b>';
}

function CP_PopUp_click(oObj) {
	//var c='#'+CP_dec2hex(CP_PopUp_click.arguments[0])+CP_dec2hex(CP_PopUp_click.arguments[1])+CP_dec2hex(CP_PopUp_click.arguments[2]);
	var CP_value=oObj.style.backgroundColor.toUpperCase();
	if(CP_value.substr(0,3)=='RGB') {
		var aux=CP_value.replace('RGB','CP_RGBcolor2HEX');
		var CP_valueHEX=eval(aux);
	} else if(CP_value.substr(0,1)=='#') {
		CP_valueHEX=CP_value;
	}
	document.getElementById('CP_Span'+CP_pCurrent).style.backgroundColor=CP_valueHEX;
	document.getElementById('CP_Text'+CP_pCurrent).value=CP_valueHEX;
	CP_config[CP_pCurrent][VALUE]=CP_valueHEX;
	CP_PopUp_sample(document.getElementById('CP_Span'+CP_pCurrent),'CP_showColorVersus');
	CP_PopUp_close(0);
}

function CP_PopUp_change_Red(R) {
	CP_config[CP_pCurrent][HUE]=R;
	for(var G=0;G<=255;G=G+CP_config[CP_pCurrent][BITS]) {
		for(var B=0;B<=255;B=B+CP_config[CP_pCurrent][BITS]) {
			document.getElementById('CP_'+G+'_'+B).style.backgroundColor='#'+CP_dec2hex(R)+CP_dec2hex(G)+CP_dec2hex(B);
		}
	}
}

function CP_PopUp_change_Green(G) {
	CP_config[CP_pCurrent][HUE]=G;
	for(var R=0;R<=255;R=R+CP_config[CP_pCurrent][BITS]) {
		for(var B=0;B<=255;B=B+CP_config[CP_pCurrent][BITS]) {
			document.getElementById('CP_'+R+'_'+B).style.backgroundColor='#'+CP_dec2hex(R)+CP_dec2hex(G)+CP_dec2hex(B);
		}
	}
}
function CP_PopUp_change_Blue(B) {
	CP_config[CP_pCurrent][HUE]=B;
	for(var R=0;R<=255;R=R+CP_config[CP_pCurrent][BITS]) {
		for(var G=0;G<=255;G=G+CP_config[CP_pCurrent][BITS]) {
			document.getElementById('CP_'+R+'_'+G).style.backgroundColor='#'+CP_dec2hex(R)+CP_dec2hex(G)+CP_dec2hex(B);
		}
	}
}

function CP_PopUp_build() {
	var htmlPalette='<table id="CP_palette" border="0" cellpadding="0" cellspacing="0" width="'+Math.round((255/CP_config[CP_pCurrent][BITS])*(CP_config[CP_pCurrent][CELL]+1))+'" height="'+Math.round((255/CP_config[CP_pCurrent][BITS])*CP_config[CP_pCurrent][CELL])+'" style="width:'+Math.round((255/CP_config[CP_pCurrent][BITS])*(CP_config[CP_pCurrent][CELL]+1))+'px;">';
	// color palette
	for(var x=0;x<=255;x=x+CP_config[CP_pCurrent][BITS]) {
		htmlPalette+='<tr>';
		for(var y=0;y<=255;y=y+CP_config[CP_pCurrent][BITS]) {
			htmlPalette+='<td id="CP_'+x+'_'+y+'" onClick="CP_PopUp_click(this)" onMouseOver="CP_PopUp_sample(this)" unselectable="on" width="'+(CP_config[CP_pCurrent][CELL]+1)+'" height="'+CP_config[CP_pCurrent][CELL]+'" style="font-size: 1px; width:'+(CP_config[CP_pCurrent][CELL]+1)+'px; height:'+CP_config[CP_pCurrent][CELL]+'px;">&nbsp;</td>';
		}
		htmlPalette+='</tr>';
	}
	var aux; var auxR; var auxG; var auxB;
	// gray colors
	htmlPalette+='<tr>';
	for(var x=0;x<=255;x=x+CP_config[CP_pCurrent][BITS]) {
		if((x+CP_config[CP_pCurrent][BITS])>255) x=255;
		aux=CP_dec2hex(x)+CP_dec2hex(x)+CP_dec2hex(x);
		htmlPalette+='<td onMouseOver="CP_PopUp_sample(this)" onClick="CP_PopUp_click(this)" unselectable="on" width="'+(CP_config[CP_pCurrent][CELL]+1)+'" height="'+CP_config[CP_pCurrent][CELL]+'" style="background-color:#'+aux+'; font-size: 1px; width:'+(CP_config[CP_pCurrent][CELL]+1)+'px; height:'+CP_config[CP_pCurrent][CELL]+'px;">&nbsp;</td>';
	}
	htmlPalette+='</tr></table>';
	// color hue
	var g=0; var b=255; var gS=0; var bS=1;
	var htmlTone='<table border="0" cellpadding="0" cellspacing="0" width="'+Math.round(CP_config[CP_pCurrent][CELL]*4)+'" height="'+Math.round((255/CP_config[CP_pCurrent][BITS])*CP_config[CP_pCurrent][CELL])+'">';
	for(var loop=0;loop<=255;loop=loop+CP_config[CP_pCurrent][BITS]) {
		auxR=CP_dec2hex(loop)+CP_dec2hex(255-loop)+CP_dec2hex(255-loop);
		auxG=CP_dec2hex(255-loop)+CP_dec2hex(loop)+CP_dec2hex(255-loop);
		auxB=CP_dec2hex(255-loop)+CP_dec2hex(255-loop)+CP_dec2hex(loop);
		htmlTone+='<tr>';
		htmlTone+='<td bgcolor="#'+auxR+'" onClick="CP_PopUp_change_Red('+loop+')" width="'+Math.round(CP_config[CP_pCurrent][CELL]*1.4)+'" height="'+CP_config[CP_pCurrent][CELL]+'" style="font-size: 1px; width:'+Math.round(CP_config[CP_pCurrent][CELL]*1)+'px; height:'+CP_config[CP_pCurrent][CELL]+'px;">&nbsp;</td>';
		htmlTone+='<td bgcolor="#'+auxG+'" onClick="CP_PopUp_change_Green('+loop+')" width="'+Math.round(CP_config[CP_pCurrent][CELL]*1.3)+'" height="'+CP_config[CP_pCurrent][CELL]+'" style="font-size: 1px; width:'+Math.round(CP_config[CP_pCurrent][CELL]*1.5)+'px; height:'+CP_config[CP_pCurrent][CELL]+'px;">&nbsp;</td>';
		htmlTone+='<td bgcolor="#'+auxB+'" onClick="CP_PopUp_change_Blue('+loop+')" width="'+Math.round(CP_config[CP_pCurrent][CELL]*1.3)+'" height="'+CP_config[CP_pCurrent][CELL]+'" style="font-size: 1px; width:'+Math.round(CP_config[CP_pCurrent][CELL]*1.5)+'px; height:'+CP_config[CP_pCurrent][CELL]+'px;">&nbsp;</td>';
		htmlTone+='</tr>';
	}
	// final tone
	htmlTone+='<tr><td bgcolor="#ff0000" onclick="CP_PopUp_change_Red(255)" width="'+(CP_config[CP_pCurrent][CELL]*1.4)+'" height="'+CP_config[CP_pCurrent][CELL]+'"></td><td bgcolor="#00ff00" onclick="CP_PopUp_change_Green(255)" width="'+(CP_config[CP_pCurrent][CELL]*1.3)+'" height="'+CP_config[CP_pCurrent][CELL]+'"></td><td bgcolor="#0000ff" onclick="CP_PopUp_change_Blue(255)" width="'+(CP_config[CP_pCurrent][CELL]*1.3)+'" height="'+CP_config[CP_pCurrent][CELL]+'"></td></tr></table>';
	// build all
	var html='<table border="0" cellpadding="0" cellspacing="0"><tr><td valign="top" style="padding:0px">'+htmlPalette+'</td><td valign="top" style="border-left:1px solid #000000; padding:0px">'+htmlTone+'</td></tr>';
	// sample colors
	html+='<tr><td colspan="2" style="border-top:1px solid #000000; padding:0px"><table border="0" cellpadding="0" cellspacing="0" width="100%" height="100%"><tr><td id="CP_showColor" align="center" onClick="CP_PopUp_click(this)" unselectable="on" style="padding:0px"><span id="CP_showValue" style="font-family:Arial; font-size:8pt;"></span></td></tr>';
	html+='<tr><td id="CP_showColorVersus" align="center" onClick="CP_PopUp_click(this)" unselectable="on" style="padding:0px"><span id="CP_showValueVersus" style="font-family:Arial; font-size:8pt;"></span></td></tr>';
	html+='</table></td></tr></table>';
	CP_div.innerHTML=html;
}

function CP_setColorTyped(oObj) {
	CP_pPrevious=CP_pCurrent;
	CP_pCurrent=parseInt(oObj.id.replace('CP_Text',''));
	if(!CP_isColor(oObj.value)) oObj.value='';
	oObj.value=oObj.value.toUpperCase();
	CP_config[CP_pCurrent][VALUE]=oObj.value;
	document.getElementById(oObj.id.replace('CP_Text','CP_Span')).style.backgroundColor=oObj.value;
}

function CP_isColor(value) {
	if(value.length!=7) return false;
	if(value.substr(0,1)!='#') return false;
	var aux=CP_HEXcolor2RGB(value);
	for(i=0;i<3;i++) {
		if(isNaN(aux[i])) return false;
		if(aux[i]>255 || aux[i]<0) return false;
	}
	return true;
}

function CP_HEXcolor2RGB(value) {
	value=value.replace('#','');
	var colorRGB=new Array();
	colorRGB[0]=value.substr(0,2);
	colorRGB[1]=value.substr(2,2);
	colorRGB[2]=value.substr(4,2);
	for(i=0;i<3;i++) {
		colorRGB[i]=parseInt(colorRGB[i],16);
	}
	return colorRGB;
}

function CP_RGBcolor2HEX(r,g,b) {
	var colorHEX='#'+CP_dec2hex(r)+CP_dec2hex(g)+CP_dec2hex(b);
	return colorHEX;
}

function CP_dec2hex(value) {
	var ConvArray=new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F');
	var retval=''; var tmpnum; var i=0;
	var intnum=parseInt(value,10);
	if(isNaN(intnum)) {
		retval='NaN';
	} else {
		while(intnum>0.9) {
			i++; tmpnum=intnum;
			retval=ConvArray[tmpnum%16]+retval;
			intnum=Math.floor(tmpnum/16);
			if(i>100) {
				retval='NaN'; break;
			}
		}
	}
	if(retval.length==1) {
		retval='0'+retval;
	} else if(retval.length==0) {
		retval='00';
	}
	return retval;
}
