// Title: Tigra Color Picker
// URL: http://www.softcomplex.com/products/tigra_color_picker/
// Version: 1.1
// Date: 06/26/2003 (mm/dd/yyyy)
// Feedback: feedback@softcomplex.com (specify product title in the subject)
// Note: Permission given to use this script in ANY kind of applications if
//    header lines are left unchanged.
// Note: Script consists of two files: picker.js and picker.html
// About us: Our company provides offshore IT consulting services.
//    Contact us at sales@softcomplex.com if you have any programming task you
//    want to be handled by professionals. Our typical hourly rate is $20.

var TCP = new TColorPicker();

function TCPopup(field, palette, parent) {
	this.field = field;
	this.parent = parent;
	this.initPalette = !palette || palette > 2 ? 0 : palette;
	var w = 280, h = 180,
	move = screen ? 
		',left=' + ((screen.width - w) >> 1) + ',top=' + ((screen.height - h) >> 1) : '', 
	o_colWindow = window.open('picker.html', null, "help=no,status=no,scrollbars=no,resizable=no" + move + ",width=" + w + ",height=" + h + ",dependent=yes", true);
	o_colWindow.opener = window;
	o_colWindow.focus();
}

function TCBuildCell (R, G, B, w, h) {
	return '<td bgcolor="#' + this.dec2hex((R << 16) + (G << 8) + B) + '"><a href="javascript:P.S(\'' + this.dec2hex((R << 16) + (G << 8) + B) + ',' +R+ ',' +G+ ',' +B+ '\')" onmouseover="P.P(\'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')"><img src="images/pixel.gif" width="' + w + '" height="' + h + '" border="0"></a></td>';
}

function TCSelect(c) {
	this.field.value = '#' + c.toUpperCase();
	var hexRGBcolor = '#' + c.toUpperCase();
	var color_array = hexRGBcolor.split(",");
	this.parent.style.background = color_array[0]; 
	this.win.close();
}

function TCPaint(c, b_noPref) {
	c = (b_noPref ? '' : '#') + c.toUpperCase();
	if (this.o_samp) 
	//	this.o_samp.innerHTML = '<font face=Tahoma size=2>' + c +' <font color=white>' + c + '</font></font>'
	this.o_samp.innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'
	if(this.doc.layers)
		this.sample.bgColor = c;
	else { 
		if (this.sample.backgroundColor != null) this.sample.backgroundColor = c;
		else if (this.sample.background != null) this.sample.background = c;
	}
}

function TCGenerateSafe() {
	var s = '';
	for (j = 0; j < 12; j ++) {
		s += "<tr>";
		for (k = 0; k < 3; k ++)
			for (i = 0; i <= 5; i ++)
				s += this.bldCell(k * 51 + (j % 2) * 51 * 3, Math.floor(j / 2) * 51, i * 51, 8, 10);
		s += "</tr>";
	}
	return s;
}

function TCGenerateWind() {
	var s = '';
	for (j = 0; j < 12; j ++) {
		s += "<tr>";
		for (k = 0; k < 3; k ++)
			for (i = 0; i <= 5; i++)
				s += this.bldCell(i * 51, k * 51 + (j % 2) * 51 * 3, Math.floor(j / 2) * 51, 8, 10);
		s += "</tr>";
	}
	return s	
}

function TCGenerateGray() {
	var s = '';
	for (j = 0; j <= 17; j ++) {
		s += "<tr>";
		g = Math.floor(255-(j *15));
		f = g-25;
		e = g-51;
		d = g-102;
		if (e < 0) {
		e=0
		}
		if (d < 0) {
		d=0
		}
		if (f < 0) {
		f=0
		}
		f2 = g+25;
		e2 = g+51;
		d2 = g+102;
		if (e2 > 255) {
		e2=255
		}
		if (d2 > 255) {
		d2=255
		}
		if (f2 > 255) {
		f2=255
		}
		
		s += this.bldCell(0, 0, g, 6,6); //dark blue
		s += this.bldCell(0, d, g, 6,6); //dark bluegreen
		s += this.bldCell(0, e, g, 6,6); //dark bluegreen
		s += this.bldCell(0, f, g, 6,6); //dark bluegreen
		s += this.bldCell(0, g, g, 6,6); //dark bluegreen
		s += this.bldCell(0, g, f, 6,6); //dark bluegreen
		s += this.bldCell(0, g, e, 6,6); //dark bluegreen
		s += this.bldCell(0, g, d, 6,6); //dark bluegreen
		s += this.bldCell(0, g, 0, 6,6); //dark green
		s += this.bldCell(d, g, 0, 6,6); //dark green
		s += this.bldCell(e, g, 0, 6,6); //dark yellowgreen
		s += this.bldCell(f, g, 0, 6,6); //dark yellowgreen
		s += this.bldCell(g, g, 0, 6,6); //dark yellow
		s += this.bldCell(g, f, 0, 6,6); //dark orange
		s += this.bldCell(g, e, 0, 6,6); //dark orange
		s += this.bldCell(g, d, 0, 6,6); //dark orange
		s += this.bldCell(g, 0, 0, 6,6); //dark red
		s += this.bldCell(g, 0, d, 6,6); //dark violet
		s += this.bldCell(g, 0, e, 6,6); //dark violet
		s += this.bldCell(g, 0, g, 6,6); //dark violet
		s += this.bldCell(d2, g, d2, 6,6);
		s += this.bldCell(e2, g, d2, 6,6);
		s += this.bldCell(f2, g, d2, 6,6);
		s += this.bldCell(g, g, d2, 6,6);
		s += this.bldCell(g, f2, d2, 6,6);
		s += this.bldCell(g, e2, d2, 6,6);
		s += this.bldCell(g, d2, d2, 6,6);
		s += this.bldCell(g, d2, e2, 6,6);
		s += this.bldCell(g, d2, f2, 6,6);
		s += this.bldCell(g, d2, g, 6,6);
		s += this.bldCell(f2, d2, g, 6,6);
		s += this.bldCell(e2, d2, g, 6,6);
		s += this.bldCell(d2, d2, g, 6,6);
		s += this.bldCell(d2, e2, g, 6,6);
		s += this.bldCell(d2, f2, g, 6,6);
		s += this.bldCell(d2, g, g, 6,6);
		s += this.bldCell(d2, g, f2, 6,6);
		s += this.bldCell(d2, g, e2, 6,6);
		s += this.bldCell(d, d, d, 6,6); //gray
		s += this.bldCell(e, e, e, 6,6); //gray
		s += this.bldCell(f, f, f, 6,6); //gray
		s += this.bldCell(g, g, g, 6,6); //gray
		s += '</tr>';
	}
	return s
}

function TCDec2Hex(v) {
	v = v.toString(16);
	for(; v.length < 6; v = '0' + v);
	return v;
}

function TCChgMode(v) {
	for (var k in this.divs) this.hide(k);
	this.show(v);
}

function TColorPicker(field) {
	this.build0 = TCGenerateSafe;
	this.build1 = TCGenerateWind;
	this.build2 = TCGenerateGray;
	this.show = document.layers ? 
		function (div) { this.divs[div].visibility = 'show' } :
		function (div) { this.divs[div].visibility = 'visible' };
	this.hide = document.layers ? 
		function (div) { this.divs[div].visibility = 'hide' } :
		function (div) { this.divs[div].visibility = 'hidden' };
	// event handlers
	this.C       = TCChgMode;
	this.S       = TCSelect;
	this.P       = TCPaint;
	this.popup   = TCPopup;
	this.draw    = TCDraw;
	this.dec2hex = TCDec2Hex;
	this.bldCell = TCBuildCell;
	this.divs = [];
}

function TCDraw(o_win, o_doc) {
	this.win = o_win;
	this.doc = o_doc;
	var 
	s_tag_openT  = o_doc.layers ? 
		'layer visibility=hidden top=54 left=5 width=80' : 
		'div style=visibility:hidden;position:absolute;left:6px;top:54px;width:182px;height:0',
	s_tag_openS  = o_doc.layers ? 'layer top=32 left=6 width=40' : 'div',
	s_tag_close  = o_doc.layers ? 'layer' : 'div'
		
	this.doc.write('<' + s_tag_openS + ' id=sam name=sam><table cellpadding=0 cellspacing=0 border="0" width=20 align=center ><tr><td align=center height=20><div id="samp"><font face=Tahoma size=2><font color=white>sample</font></font></div></td></tr></table></' + s_tag_close + '>');
	this.sample = o_doc.layers ? o_doc.layers['sam'] : 
		o_doc.getElementById ? o_doc.getElementById('sam').style : o_doc.all['sam'].style

	for (var k = 0; k < 3; k ++) {
		this.doc.write('<' + s_tag_openT + ' id="p' + k + '" name="p' + k + '"><table cellpadding=0 cellspacing=0 align=center>' + this['build' + k]() + '</table></' + s_tag_close + '>');
		this.divs[k] = o_doc.layers 
			? o_doc.layers['p' + k] : o_doc.all 
				? o_doc.all['p' + k].style : o_doc.getElementById('p' + k).style
	}
	if (!o_doc.layers && o_doc.body.innerHTML) 
		this.o_samp = o_doc.all 
			? o_doc.all.samp : o_doc.getElementById('samp');
	this.C(this.initPalette);
	if (this.field.value) this.P(this.field.value, true)
}
