// Title: Tigra Color Picker
// URL: http://www.softcomplex.com/products/tigra_color_picker/
// Version: 1.1
// Date: 06/26/2003 (mm/dd/yyyy)
// 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

// 5/10/2006 MODIFIED as you see here by CalculatorCat.com

var TCP = new TColorPicker('moduleForm');

function TColorPicker(formName) {
	this.f = formName;
	this.initPalette = 0;
	this.build0 = TCGenerateSafe;
	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.S       = TCSelect;
	this.P       = TCPaint;
	this.popup   = TCPopup;
	this.draw    = TCDraw;
	this.dec2hex = TCDec2Hex;
	this.bldCell = TCBuildCell;
	this.otherAction = TCOtherAction;
	this.divs = [];
}

function TCPopup(fieldName, e) {
	var o_pos = eventPos(e);
	var o_tmp = GetStyleObj('p0');
	o_tmp.left = o_pos.x;
	o_tmp.top = o_pos.y;
	this.show(this.initPalette);

	this.field = document.forms[this.f].elements[fieldName];
	this.c_sample = GetStyleObj('c_'+fieldName);
}

function TCBuildCell (R, G, B, w, h) {
	return '<td bgcolor="#' + this.dec2hex((R << 16) + (G << 8) + B) + '"><a href="javascript:TCP.S(\'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')" onmouseover="TCP.P(\'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')"><img src="/images/spacer.gif" width="' + w + '" height="' + h + '" border="0"></a></td>';
}

function TCSelect(c) {
	this.field.value = c.toUpperCase();
	this.otherAction();
	if(this.doc.layers)
		this.c_sample.bgColor = c;
	else {
		if (this.c_sample.backgroundColor != null) this.c_sample.backgroundColor = c;
		else if (this.c_sample.background != null) this.c_sample.background = c;
	}
	this.hide(0);
}

function TCPaint(c, b_noPref) {

	c = (b_noPref ? '' : '#') + c.toUpperCase();

	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;
	}

	o_doc = this.doc;
	if (!o_doc.layers && o_doc.body.innerHTML)
		this.o_samp = o_doc.all ? o_doc.all.samp : o_doc.getElementById('samp');
	if (this.o_samp)
		this.o_samp.innerHTML = 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, 9, 10);
		s += "</tr>";
	}
	s += "<tr>";
	for (i = 15; i >= 0; i --) {
		s += this.bldCell(17*i, 17*i, 17*i, 9, 10);
		if(i==15) {
			s += this.bldCell(248, 248, 248, 9, 10);
		} else if(i==14) {
			s += this.bldCell(232, 232, 232, 9, 10);
		}
	}
	s += "</tr>";
	return s;
}

function TCDec2Hex(v) {
	v = v.toString(16);
	for(; v.length < 6; v = '0' + v);
	return v;
}

function TCDraw(o_win, o_doc) {
	this.win = o_win;
	this.doc = o_doc;
	// CC: changed orig abs positions
	var s_tag_openT  = o_doc.layers ? 'layer visibility=hidden top=0 left=50 width=182' : 'div style=visibility:hidden;position:absolute;left:0px;top:0px;width:182px;height:0';
	//s_tag_openS  = o_doc.layers ? 'layer top=0 left=0' : '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=181 align=center class=bd><tr><td align=center height=18><div id="samp"><font face=Tahoma size=2>sample <font color=white>sample</font></font></div></td></tr></table></' + s_tag_close + '>');
	//this.sample = GetStyleObj('sam');

	for (var k = 0; k < 1; k ++) { // CC: changed to only use first palette
		var s = "";
		s += '<' + s_tag_openT + ' id="p' + k + '" name="p' + k + '">';
		s += '<table cellpadding=0 cellspacing=1 border=0 align=center bgcolor="#ffffff" style="border:1px solid #000000">';
		s += '<tr><td id="sam" align="center" bgcolor="#ffffff" colspan="9" style="border-bottom:1px solid #000000">&nbsp;</td><td id="samp" align="center" bgcolor="#ffffff" colspan="9" style="border-bottom:1px solid #000000">&nbsp;</td></tr>';
		s += this['build' + k]();
		s += '<tr><td align="center" bgcolor="#ffffff" colspan="18" style="border-top:1px solid #000000"><a href="#" onclick="TCP.hide(0);return false;">close</a></td></tr>';
		s += '</table>';
		s += '</' + s_tag_close + '>';
		this.doc.write(s);
		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
	}
	this.sample = GetStyleObj('sam');
}

function GetStyleObj(n) {
	o_doc = document;
	return o_doc.layers ? o_doc.layers[n] : o_doc.getElementById ? o_doc.getElementById(n).style : o_doc.all[n].style;
}

function eventPos(e) {
	var posX = 0, posY = 0;
	if( !e && window.event ) { e = window.event; }
	if( !e ) { return { 'x':0, 'y':0 }; }
	if( typeof( e.pageX ) == 'number' ) {
		posX = e.pageX; posY = e.pageY;
	} else {
		if( typeof( e.clientX ) == 'number' ) {
			posX = e.clientX; posY = e.clientY;
			if( document.body && ( document.body.scrollTop || document.body.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) ) {
				posX += document.body.scrollLeft; posY += document.body.scrollTop;
			} else {
				if( document.documentElement && ( document.documentElement.scrollTop || document.documentElement.scrollLeft ) && !( window.opera || window.debug || navigator.vendor == 'KDE' ) ) {
					posX += document.documentElement.scrollLeft; posY += document.documentElement.scrollTop;
				}
			}
		}
	}
   return { 'x':posX, 'y':posY };
}

function TCOtherAction() {
	modulePreview();
}

/*
	// intialize color fields
	var f = document.forms[formName];
	for(var i=0; i<f.elements.length; i++) {
		if(f.elements[i].name.indexOf('ccm_c')!=-1) {
			alert(f.elements[i].name);
		}
	}
*/