<!--

/*
Author: Maximus © 2003 (http://maximus.ravecore.com)
Script Name: Color Tables
Version: 1.0
*/

// Opera 6 padding fix still questionable

ctx = new function() {

	c = new Array();

	c.colors = new Array(new Array("00", "33", "66", "99", "CC", "FF"),
		new Array("00", "33", "66", "99", "BB", "DD", "FF"),
		new Array("00", "22","44", "66", "88", "AA", "CC", "EE", "FF"),
		new Array("00","11","22","33","44","55","66","77","88","99","AA","BB","CC","DD","EE","FF"));
	c.chars = "0123456789ABCDEF";

	c.create =  function(c,s,n) { with(this) {
		if(typeof n=='undefined') { n = 'ctx'+length; }
		if(typeof s=='undefined') { s = "ctx"; }
		if(typeof c=='undefined') { c = 0; }
		this[n] = {
			name: n,
			num: length,
			colors: colors[c],
			style: s,
			dec: "rgb(255,255,255)",
			hex: "#FFFFFF",
			setColor:setColor,
			toHex:toHex,
			toDec:toDec,
			setValue:setValue,
			checkChar:checkChar,
			checkHex:checkHex,
			checkDec:checkDec
		};
		this[length] = this[n];
		var c = this[n].colors;
		if(!c.html) {
			var OP6 = window.opera && navigator.userAgent.indexOf('Opera 6') > -1;
			var tbl = 'table cellpadding="0" cellspacing="1" border="0"';
			var t = '<td bgcolor="%h" onclick="%t.setColor(false, '+(OP6?'this.style.backgroundColor':'this.bgColor')+');" onmouseover="window.status='+(OP6?'this.style.backgroundColor':'this.bgColor')+'.toUpperCase();return true;" onmouseout="window.status=\'\';"></td>';
			c.html = '<'+tbl+' class="%s" ondragstart="return false"><tr><td><'+tbl+'><tr><td><table cellpadding="0" cellspacing="0" border="0" class="%s">';
			for(var i = 0; i < c.length; i++) {
				c.html += '<tr>';
				for(var j = 0; j < c.length; j++) {
					for(var k = 0; k < c.length; k++) {
						var h = c[i]+c[j]+c[k+((j%2 == 1)?c.length-1-k*2:0)];
						c.html += t.replace(/%h/g,h);
					}
				}
				c.html += '</tr>';
			}
			c.html += '</table></td><td><table cellpadding="0" cellspacing="0" border="0" class="%s">';
			for(var i = 0; i < c.length; i++) {
				var h = c[i]+c[i]+c[i];
				c.html += '<tr>'+t.replace(/%h/g,h);+'</tr>';
			}
			c.html += '</table></td></tr></table>'
				+'<'+tbl+'><tr>'
				+'<td rowspan="2" nowrap>';
			var s = ["RED","GREEN","BLUE"], t = '<div id="%n_%s2" class="slider" onmousedown="%t.setValue(\'%s\',event);%t.setColor(true);this.onmousemove=function(){%t.setValue(\'%s\',event);%t.setColor(true);};document.onmouseup=function(){document.getElementById(\'%n_%s2\').onmousemove=null};" style="'+( OP6 ? 'padding-bottom:3px;' : '' )+'"><div id="%n_%s" class="slider2" style="background-color:%s;'+( OP6 ?'left:1px;top:1px;':'')+'"></div></div>';
			for(var i = 0; i < s.length; i++) { c.html += t.replace(/%s/g,s[i]); }
			c.html += '</td>';
			var s = ["R","G","B"], t = '<td rowspan="2">%s:</td><td><input type="text" id="%n_%s" maxlength="3" value="255" onfocus="this.select()" onkeyup="%t.checkChar(this,true,event,\'%n_%s\');"></td>', u = '<td><input type="text" id="%n_h%s" maxlength="2" value="FF" onfocus="this.select()" onkeyup="%t.checkChar(this,false,event,\'%n_h%s\');"></td>';
			var temp = '';
			for(var i = 0; i < s.length; i++) { c.html += t.replace(/%s/g,s[i]); temp += u.replace(/%s/g,s[i]); }
			c.html += '</tr><tr>'+temp
				+'</tr></table><'+tbl+'><tr>'
				+'<td><div id="%n_bg" class="bg"></div></td>'
				+'<td>&nbsp;Hex:<input type="text" id="%n_hex" class="hex" maxlength="7" value="#FFFFFF" onfocus="this.select()" onkeyup="%t.checkHex(this,event);"></td>'
				+'<td>&nbsp;Dec:<input type="text" id="%n_dec" class="dec" maxlength="16" value="rgb(255,255,255)" onfocus="this.select();this._value = this.value;" onkeydown="" onkeyup="%t.checkDec(this,event);"></td>'
				+'</tr></table></td></tr></table>';
		}
		document.write(c.html.replace(/%t/g,'ctx['+this[n].num+']').replace(/%n/g,this[n].name).replace(/%s/g,this[n].style));
	} };
	c.setColor = function( t, c, o ) { with(this) { if(this == ctx) return false;
		var d = document;
		if( t ) {
			if( !c ) c = "rgb("+d.getElementById(name+"_R").value+","+d.getElementById(name+"_G").value+","+d.getElementById(name+"_B").value+")";
			var cH = toHex( c );
			var cD = toDec( cH );
		}
		else {
			if( !c ) c = toHex( toDec( d.getElementById(name+"_hR").value,d.getElementById(name+"_hG").value,d.getElementById(name+"_hB").value ) );
			var cD = toDec( c );
			var cH = toHex( cD );
		}
		hex = cH;
		dec = cD;
		if(o != name+"_hex") d.getElementById(name+"_hex").value = cH;
		if(o != name+"_dec") d.getElementById(name+"_dec").value = cD;
		d.getElementById(name+"_bg").style.backgroundColor = cH;
		var rgb = cD.slice(4,-1).split(",");
		setValue("RED", rgb[0], o);
		setValue("GREEN", rgb[1], o);
		setValue("BLUE", rgb[2], o);
	} };
	c.toHex = function(d) { with(this) {
		var a = arguments;
		if(a.length == 1) {
			d = d.slice(4,-1).split(",");
		}
		else {
			d = new Array(a[0], a[1], a[2]);
		}
		for(var i in d) d[i] = d[i] < 0 ? 0 : d[i] > 255 ? 255 : d[i];
		var d0 = Math.floor(d[0]/16);
		var d1 = Math.floor(d[1]/16);
		var d2 = Math.floor(d[2]/16);
		var c = ctx.chars;
		return h = "#"
			+c.charAt(d0)
			+c.charAt(d[0]-d0*16)
			+c.charAt(d1)
			+c.charAt(d[1]-d1*16)
			+c.charAt(d2)
			+c.charAt(d[2]-d2*16);
	} };
	c.toDec = function(h) { with(this) {
		var a = arguments;
		if(a.length == 3) {
			for(var i = 0; i < 3; i++) if( a[i].length == 1 ) a[i] = "0"+a[i];
			h = "#"+a[0]+a[1]+a[2];
		}
			h = h.slice(1).toUpperCase();
		var d="rgb(", c = ctx.chars;
		for(var i = 0; i < 5; i+=2) {
			h0 = h.charAt(i);
			h1 = h.charAt(i+1);
			d += ( h0 < "0" ? 0 : h0 > "F" ? 15 : c.indexOf( h0 ) ) * 16 + ( h1 < "0" ? 0 : h1 > "F" ? 15 : c.indexOf( h1 ) ) + ( i != 4 ? "," : "" );
		}
		return d+")";
	} };
	c.setValue = function(ext, e_x, o) { with(this) { if(this == ctx) return false;
		var d = document;
		var sliderWidth = d.getElementById(name+"_"+ext+"2").offsetWidth - 2;
		if( !parseInt(e_x) && parseInt(e_x)!=0 ) {
			e_x = (d.all) ? event.offsetX : e_x.layerX;
			e_x = Math.round( ( e_x >= 0 && e_x <= sliderWidth ? e_x : e_x < 0 ? 0 : sliderWidth) * ( 255 / sliderWidth ) );
		}
		d.getElementById(name+"_"+ext).style.width = Math.round( sliderWidth / 255 * e_x );
		if(o != name+"_"+ext.charAt(0)) d.getElementById(name+"_"+ext.charAt(0)).value = e_x;
		if(o != name+"_h"+ext.charAt(0)) d.getElementById(name+"_h"+ext.charAt(0)).value = toHex("rgb("+e_x+",0,0)").slice(1,3);
		if(navigator.userAgent.indexOf('Gecko') >-1 && navigator.userAgent.indexOf('Netscape') == -1)
			d.getElementById(name+"_"+ext+"2").appendChild(d.getElementById(name+"_"+ext));
	} };
	c.checkChar = function(o,t,e,k) {
		// t = true = dec; t = false = hex
		/*if(o.value.length>0) {
			var i = ctx.chars.indexOf(o.value.charAt(o.value.length-1));
			
			if( ( t && ( i < 0 || i > 8 ) ) ) {
				o.value = o.value.replace(/[^0-9]/gi,"");
			}
			else if( ( !t && i < 0 ) ) {
				o.value = o.value.replace(/[^0-9a-f]/gi,"");
			}
		}
		if((e.keyCode>=48&&e.keyCode<=57||e.keyCode>=65&&e.keyCode<=70||e.keyCode==8)&&o.value.length>(t?0:1))
			this.setColor(t,"",k);*/
		var c;
		if( t ) {
			c = o.value.replace(/[^0-9]/gi,"");
		}
		else if( !t ) {
			c = o.value.replace(/[^0-9a-f]/gi,"");
		}
		if( c != o.value )
			o.value = c;
		if( parseInt(o.value) > 255 )
			o.value = 255;
		this.setColor(t,"",k);
		};
	c.checkHex = function(o,e) {
		var v = "#"+o.value.slice(1).replace(/[^0-9a-f]/gi,"0");
		document.getElementById(this.name+"_hR").value = v.slice(1,3);
		document.getElementById(this.name+"_hG").value = v.slice(3,5);
		document.getElementById(this.name+"_hB").value = v.slice(5,7);
		if((e.keyCode>=48&&e.keyCode<=57||e.keyCode>=65&&e.keyCode<=70||e.keyCode==8)&&o.value.length>6)
			this.setColor(false,"",this.name+"_hex");
	};
	c.checkDec = function(o,e) {
		var p = o.value.match(/[\,]/g);
		var i = o.value.indexOf(')');
		if(o.value.slice(0,4)!="rgb("||i==-1||p==null||p.length<2) {
			o.value = o._value;
		}
		else if((e.keyCode>=48&&e.keyCode<=57||e.keyCode==8)) {
			
			var v = o.value.slice(4,i).split(",");
			if(i!=o.value.length-1) o.value = o.value.slice(0,i+1);
			document.getElementById(this.name+"_R").value = v[0].replace(/[^0-9]/gi,"");
			document.getElementById(this.name+"_G").value = v[1].replace(/[^0-9]/gi,"");
			document.getElementById(this.name+"_B").value = v[2].replace(/[^0-9]/gi,"");
			this.setColor(true,"",this.name+"_dec");
			o._value = o.value;
			if(document.getElementById(this.name+"_R").value != v[0].replace(/[^0-9]/gi,"")||	document.getElementById(this.name+"_G").value != v[1].replace(/[^0-9]/gi,"")||
			document.getElementById(this.name+"_B").value != v[2].replace(/[^0-9]/gi,"")) {
				this.setColor(true);
			}
		}
	}

	return c;
}
//-->