
var tb_ClientID = ''; //颜色文件框ID
var img_ClientID = ''; //显示选取器的图标ID

var color = "" ;
var SelRGB = color;
var DrRGB = '';
var SelGRAY = '120';

var hexch = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F');

function ToHex(n) {	
	var h, l;

	n = Math.round(n);
	l = n % 16;
	h = Math.floor((n / 16)) % 16;
	return (hexch[h] + hexch[l]);
}

            
function DoColor(c, l){
	var r=1, g=2, b=3;

     
 		r = '0x' + c.substring(1, 3);
		g = '0x' + c.substring(3, 5);
		b = '0x' + c.substring(5, 7);

		if(l > 120)
		{
			l = l - 120;
			r = (r * (120 - l) + 255 * l) / 120;
			g = (g * (120 - l) + 255 * l) / 120;
			b = (b * (120 - l) + 255 * l) / 120;
		}
		else
		{
			r = (r * l) / 120;
			g = (g * l) / 120;
			b = (b * l) / 120;
		}
    var aaa='#' + ToHex(r) + ToHex(g) + ToHex(b);
    if(aaa=='#NaNNaNNaN')
    {
		return '#FFFFFF';
    }
    else
    {
        return '#' + ToHex(r) + ToHex(g) + ToHex(b);
    }
}


function wc(r, g, b, n, tb_ClientID)
{         
    r = ((r * 16 + r) * 3 * (15 - n) + 0x80 * n) / 15;
    g = ((g * 16 + g) * 3 * (15 - n) + 0x80 * n) / 15;
    b = ((b * 16 + b) * 3 * (15 - n) + 0x80 * n) / 15;

    document.write('<td bgcolor=#' + ToHex(r) + ToHex(g) + ToHex(b) + ' title=\"#' + ToHex(r) + ToHex(g) + ToHex(b) + '\" height=8 width=8 onmouseover=\"ColorTableMouseOver(this)\" onmousedown=\"ColorTableMouseDown(this)\"  onmouseout=\"ColorTableMouseOut(this)\" ></td>');
}

function WriteColorPanel(tb_ClientID,img_ClientID,leftvalue,topvalue)
{
    
    document.write('<td >');
    document.write('<table border=\"0\" cellPadding=\"0\" cellSpacing=\"0\" id=\"ColorTable_'+tb_ClientID+'\" style=\"cursor:crosshair;\" >');
    var cnum = new Array(1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 0);
    for(i = 0; i < 16; i ++)
    {
        document.write('<TR>');
        for(j = 0; j < 30; j ++)
        {
       	    n1 = j % 5;
       	    n2 = Math.floor(j / 5) * 3;
      	    n3 = n2 + 3;
       	    wc((cnum[n3] * n1 + cnum[n2] * (5 - n1)),
       	    (cnum[n3 + 1] * n1 + cnum[n2 + 1] * (5 - n1)),
       	    (cnum[n3 + 2] * n1 + cnum[n2 + 2] * (5 - n1)), i,tb_ClientID);
        }
        document.write('</tr>');
    }
    document.write('</table></td>');
    
    
    document.write('<td>');
    document.write('<table border=\"0\" cellPadding=\"0\" cellSpacing=\"0\" id=\"GrayTable'+tb_ClientID+'\" style=\"CURSOR: hand;cursor:crosshair;\" >');
    for(i = 255; i >= 0; i -= 8.5)
       document.write('<tr bgcolor=#' + ToHex(i) + ToHex(i) + ToHex(i) + '><td title=' + Math.floor(i * 16 / 17) + ' height=4 width=20 onmouseover=\"GrayTableMouseOver(this)\" onmousedown=\"GrayTableMouseDown(this)\"  onmouseout=\"GrayTableMouseOut(this)\" ></td></tr>');
           
    // alert(tb_ClientID);      
    document.write("<tbody></tbody></table></td>");
}
//var tb_ClientID = 'TabControl1_tabPage51_color_color';

function ColorTableMouseDown(e)
{
	SelRGB = e.title;
	EndColor();
}

function ColorTableMouseOver(e)
{
 	$('RGB'+tb_ClientID).innerHTML = e.title;
	EndColor();
}

function ColorTableMouseOut(e)
{
  	$('RGB'+tb_ClientID).innerHTML = SelRGB;
	EndColor();
}



function GrayTableMouseDown(e)
{
	SelGRAY = e.title;
	EndColor();
}

function GrayTableMouseOver(e)
{
	$('GRAY'+tb_ClientID).innerHTML = e.title;
	EndColor();
}

function GrayTableMouseOut(e)
{
    //alert(tb_ClientID);
	$('GRAY'+tb_ClientID).innerHTML = SelGRAY;
	EndColor();
}


function HideColorPanel(tb_clientid)
{   
    $('ShowColor'+tb_ClientID).bgColor = $('SelColor'+tb_ClientID).value;
    $('ColorPicker'+tb_clientid).style.display='none';
}

function EndColor()
{
    var i;

    if(DrRGB != SelRGB)
    {
       DrRGB = SelRGB;
       for(i = 0; i <= 30; i ++)
       {
            $('GrayTable'+tb_ClientID).rows[i].bgColor = DoColor(SelRGB, 240 - i * 8);
       }
    }

    $('SelColor'+tb_ClientID).value = DoColor($('RGB'+tb_ClientID).innerHTML, $('GRAY'+tb_ClientID).innerHTML);
    $('ShowColor'+tb_ClientID).bgColor = $('SelColor'+tb_ClientID).value;
    $(tb_ClientID).value = $('SelColor'+tb_ClientID).value;
}

function ColorPickerOK(tb_clientid ,img_clientid)
{
     var selectcolor=$('SelColor'+tb_clientid).value;
     $(img_ClientID).style.background=selectcolor;
     obj=$(tb_clientid);
     obj.value=selectcolor;
     obj.focus();
     obj.select();
     //if(navigator.appName.indexOf('Explorer') > -1)
     //{
     //    obj.createTextRange().execCommand('Copy');
     //    window.status = '将模板内容复制到剪贴板';
     //    setTimeout(\"window.status=''\", 1800);
     //}
     HideColorPanel(tb_clientid);
}

function ShowColorPanel(tb_clientid,img_clientid, leftvalue,topvalue)
{
     tb_ClientID = tb_clientid;
     img_ClientID = img_clientid;
     var p = GetColorPanelPosition($(tb_ClientID),leftvalue,topvalue);
     $('ColorPicker'+tb_ClientID).style.display = 'block';
    
        if(navigator.appName.indexOf('Explorer') > -1)
        {
 			$('ColorPicker'+tb_ClientID).style.left = p['x'] + leftvalue +  'px';
		}
		else
		{
		    $('ColorPicker'+tb_ClientID).style.left = p['x'] + 'px';
		}
        
        if(navigator.appName.indexOf('Explorer') > -1)
        {
            $('ColorPicker'+tb_ClientID).style.top = p['y'] + $(tb_ClientID).offsetHeight + topvalue + 'px';
        }
        else
        {
			$('ColorPicker'+tb_ClientID).style.top = p['y'] + $(tb_ClientID).offsetHeight + 'px';
        }    
}

function IsShowColorPanel(tb_clientid,img_clientid, leftvalue,topvalue)
{
     tb_ClientID = tb_clientid;
     img_ClientID = img_clientid;
     if($('ColorPicker'+tb_ClientID).style.display == 'none')
     {
        var p = GetColorPanelPosition($(tb_ClientID),leftvalue,topvalue);
        $('ColorPicker'+tb_ClientID).style.display = 'block';
        
        if(navigator.appName.indexOf('Explorer') > -1)
        {
 			$('ColorPicker'+tb_ClientID).style.left = p['x'] + leftvalue +  'px';
		}
		else
		{
		    $('ColorPicker'+tb_ClientID).style.left = p['x'] + 'px'; 
		}
        
        if(navigator.appName.indexOf('Explorer') > -1)
        {
            $('ColorPicker'+tb_ClientID).style.top = p['y'] + $(tb_ClientID).offsetHeight + topvalue + 'px';
        }
        else
        {
			$('ColorPicker'+tb_ClientID).style.top = p['y'] + $(tb_ClientID).offsetHeight + 'px';
        }    
     }
     else
     {
        $('ColorPicker'+tb_ClientID).style.display = 'none';
     }
}


function GetColorPanelPosition(obj,leftvalue,topvalue)
{
    // alert(topvalue);
     var r = new Array();
     r['x'] = obj.offsetLeft;
     r['y'] = obj.offsetTop;
     while(obj = obj.offsetParent)
     {
 		r['x'] += obj.offsetLeft;
		r['y'] += obj.offsetTop;
     }
     return r;
}

function $() {
  var elements = new Array();
  
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    try
    {
		if (typeof element == 'string')
		element = document.getElementById(element) || document.all(element) || document.forms(0).all(element);
    }
    catch(ex)
    {
		element = null;
    }

    if (arguments.length == 1) 
      return element;
      
    elements.push(element);
  }
  
  return elements;
}


//初始化选取器中图标的背景色
function InitColorPicker(img_ClientID , selectColor)   
{         
    if (selectColor != null && selectColor != "")
    {
        $(img_ClientID).style.background=selectColor;
    }
}
