// 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

var TCP = new TColorPicker();

function TCPopup(field, palette) {
   this.field = field;
   this.initPalette = !palette || palette > 2 ? 0 : palette;
   var w = 294, h = 350,
   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, txt) {
    var tdval = '';
    tdval += '<td bgcolor="#' + this.dec2hex((R << 16) + (G << 8) + B) + '" width="' + w + '" height="' + h + '"><a href="javascript:P.S(\'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')" onmouseover="P.P(\'' + this.dec2hex((R << 16) + (G << 8) + B) + '\')">';

    if (txt) {
        tdval += txt;
      }
      else {
        tdval += '<img src="pixel.gif" width="' + w + '" height="' + h + '" border="0">';
    }

    tdval += '</a></td>';
  return tdval;
}

function TCSelect(c) {
   this.field.value = '#' + c.toLowerCase();
   this.win.close();
}

function TCPaint(c, b_noPref) {
   c = (b_noPref ? '' : '#') + c.toLowerCase();
   if (this.o_samp) 
   	this.o_samp.innerHTML = '<font face="Tahoma" size="2">' + c +' <font color="white">' + c + '</font></font>'
   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 txt = '';
   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, 12, 15);
   s += "</tr>";
   }
   return s;
}

function TCGenerateWind() {
    var txt = '';
   var s = '';
   var s = '<tr align="center">';
      s += this.bldCell(255,0,0,65,18,'<font size="2" color="#ffffff">red</font>');
      s += this.bldCell(255,204,204,65,18,'<font size="2" color="#000000">lt red</font>');
      s += this.bldCell(153,0,0,65,18,'<font size="2" color="#ffffff">dk red</font>');
      s += this.bldCell(0,0,0,65,18,'<font size="2" color="#ffffff">black</font>');
   s += '</tr>';
   s += '<tr align="center">';
      s += this.bldCell(0,255,0,65,18,'<font size="2" color="#000000">green</font>');
      s += this.bldCell(204,255,204,65,18,'<font size="2" color="#000000">lt green</font>');
      s += this.bldCell(0,153,0,65,18,'<font size="2" color="#ffffff">dk green</font>');
      s += this.bldCell(255,255,255,65,18,'<font size="2" color="#000000">white</font>');
   s += '</tr>';
   s += '<tr align="center">';
      s += this.bldCell(0,0,255,65,18,'<font size="2" color="#ffffff">blue</font>');
      s += this.bldCell(204,204,255,65,18,'<font size="2" color="#000000">lt blue</font>');
      s += this.bldCell(0,0,153,65,18,'<font size="2" color="#ffffff">dk blue</font>');
      s += this.bldCell(244,239,214,65,18,'<font size="2" color="#000000">beige</font>');
   s += '</tr>';
   s += '<tr align="center">';
      s += this.bldCell(0,255,255,65,18,'<font size="2" color="#000000">cyan</font>');
      s += this.bldCell(204,255,255,65,18,'<font size="2" color="#000000">lt cyan</font>');
      s += this.bldCell(0,153,153,65,18,'<font size="2" color="#ffffff">dk cyan</font>');
      s += this.bldCell(150,106,64,65,18,'<font size="2" color="#ffffff">brown</font>');
   s += '</tr>';
   s += '<tr align="center">';
      s += this.bldCell(255,0,255,65,18,'<font size="2" color="#ffffff">magenta</font>');
      s += this.bldCell(255,204,255,65,18,'<font size="2" color="#000000">lt magenta</font>');
      s += this.bldCell(153,0,153,65,18,'<font size="2" color="#ffffff">dk magenta</font>');
      s += this.bldCell(153,51,255,65,18,'<font size="2" color="#ffffff">purple</font>');
   s += '</tr>';
   s += '<tr align="center">';
      s += this.bldCell(255,255,0,65,18,'<font size="2" color="#000000">yellow</font>');
      s += this.bldCell(255,255,204,65,18,'<font size="2" color="#000000">lt yellow</font>');
      s += this.bldCell(153,153,0,65,18,'<font size="2" color="#ffffff">dk yellow</font>');
      s += this.bldCell(255,215,0,65,18,'<font size="2" color="#000000">gold</font>');
   s += '</tr>';
   s += '<tr align="center">';
      s += this.bldCell(204,204,204,65,18,'<font size="2" color="#000000">lt gray</font>');
      s += this.bldCell(153,153,153,65,18,'<font size="2" color="#ffffff">md gray</font>');
      s += this.bldCell(102,102,102,65,18,'<font size="2" color="#ffffff">dk gray</font>');
      s += this.bldCell(192,192,192,65,18,'<font size="2" color="#000000">silver</font>');
   s += '</tr>';
   return s   
}

function TCGenerateGray() {
    var txt = '';
   var s = '';
   for (j = 0; j <= 15; j ++) {
   s += "<tr>";
   	for (k = 0; k <= 15; k ++) {
      	g = Math.floor((k + j * 16) % 256);
      s += this.bldCell(g, g, g, 13, 11);
   	}
   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="90" left="5" width="282"' : 
   	'div style="visibility:hidden;position:absolute;left:6px;top:90px;width:282px;height:0"',
   s_tag_openS  = o_doc.layers ? 'layer top="32" left="6"' : '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="1" width="281" align="center" class="bd"><tr><td align="center" height="18"><div id="samp"><font face="Tahoma" size="2"><font color="black">sample</font> <font color="white">sample</font></font></div></td></tr><tr><td align="center" height="18"><div id="samp"><font face="Tahoma" size="2"> <font color="#ff0000"><b>red</b> text</font> <font color="00ff00"><b>green</b> text</font> <font color="0000ff"><b>blue</b> text</font></font></div></td></tr><tr><td align="center" height="18"><div id="samp"><font face="Tahoma" size="2"> <font color="#00ffff"><b>cyan</b> text</font> <font color="ff00ff"><b>magenta</b> text</font> <font color="ffff00"><b>yellow</b> text</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" border="1" 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)
}
