
/*
Example:


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
</head>
<body>

<input type="text" name="color_1" id="color_1" style="width: 70px; text-transform: uppercase;" value="3366CC" OnChange="OnChange(this.value);">
<span id="color_1_picker" style="border: 1px solid black; background-color: #3366CC; font-size: 5px; width: 15px; height: 15px; margin: 0px 0px 0px 3px; cursor: pointer;" OnClick="color_picker.Show('color_1', 'color_1_picker', OnChange);"></span>

<script language='JavaScript1.3' src='js/myl_color_picker.js'></script>
<script language="JavaScript">
<!--

	var color_picker = new MyColorPicker();

	function OnChange(color)
	{
		document.body.style.backgroundColor = '#'+((color_picker.IsRgb(color))? color : 'FFFFFF');
		document.getElementById("color_1_picker").style.backgroundColor = '#'+((color_picker.IsRgb(color))? color : 'FFFFFF');
	}


//-->
</script>

</body>
</html>


*/



function MyColorPicker(div_frame_id)
{
	var me = this;

	var cp_grid = [
	  ["FFFFFF", "FFCCCC", "FFCC99", "FFFF99", "FFFFCC", "99FF99", "99FFFF", "CCFFFF", "CCCCFF", "FFCCFF"],
	  ["CCCCCC", "FF6666", "FF9966", "FFFF66", "FFFF33", "66FF99", "33FFFF", "66FFFF", "9999FF", "FF99FF"],
	  ["C0C0C0", "FF0000", "FF9900", "FFCC66", "FFFF00", "33FF33", "66CCCC", "33CCFF", "6666CC", "CC66CC"],
	  ["999999", "CC0000", "FF6600", "FFCC33", "FFCC00", "33CC00", "00CCCC", "3366FF", "6633FF", "CC33CC"],
	  ["666666", "990000", "CC6600", "CC9933", "999900", "009900", "339999", "3333FF", "6600CC", "993399"],
	  ["333333", "660000", "993300", "996633", "666600", "006600", "336666", "000099", "333399", "663366"],
	  ["000000", "330000", "663300", "663333", "333300", "003300", "003333", "000066", "330099", "330033"]];

	// create and show color picking window besides picker object
	//var color_frame = document.createElement("div");
	var color_frame = document.getElementById(div_frame_id);
	color_frame.style.backgroundColor = "#C0C0C0";
	color_frame.style.width = "100px";
	color_frame.style.height = "100px";
	color_frame.style.zIndex = "100";
	
	color_frame.style.position = "absolute";
	color_frame.style.left = "0px";
	color_frame.style.top = "0px";
	color_frame.style.visibility = "hidden";

	//color_frame.id = "zzzzzzzzzzzzz";


	// this somehow brakes the html !!!!
	//document.body.appendChild(color_frame);
	//document.getElementById('contents').appendChild(color_frame);


	// color table
	var table = document.createElement("table");
	table.setAttribute("border", "1");
	table.style.backgroundColor = "#808080";
	var tbody = document.createElement("tbody"); // IE 6 needs this.
	var row = null, cell = null;
	for (var y=0; y<cp_grid.length; y++)
	{
		row = document.createElement("tr");
		tbody.appendChild(row);
		for (var x=0; x<cp_grid[y].length; x++)
		{
			cell = document.createElement("td");
			row.appendChild(cell);
			cell.style.backgroundColor = "#"+cp_grid[y][x];
			cell.label = cp_grid[y][x];
			cell.style.border = "solid 2px #"+cell.label;
			cell.innerHTML = "<div style='width: 20px; height: 20px;'>&nbsp;</div>";
			cell.onmousedown = function(){document.onmouseup=""; SelectColor(this.label)}
			cell.onmouseover = function(){MouseOver(this.label)}
		}
	}
	table.appendChild(tbody);
	color_frame.appendChild(table);

	


	var start_value = '', text_box = null, picker = null, on_change_function = null;
	this.Show = function(text_box_id, picker_id, on_change_fn)
	{
		text_box = document.getElementById(text_box_id);
		picker = document.getElementById(picker_id);
		on_change_function = on_change_fn;
		start_value = text_box.value;
		
		var caller = picker;
		var posX = 0;
		var posY = caller.offsetHeight;
		while (caller)
		{
			posX += caller.offsetLeft;
			posY += caller.offsetTop;
			caller = caller.offsetParent;
		}

		/*posX -= 300;
		posY -= 200;

		posX = color_picker_mouse_x;
		posY = color_picker_mouse_y;*/
		
		color_frame.style.left = posX+15+"px";
		color_frame.style.top = posY-15+"px";
		color_frame.style.visibility = "visible";

		document.onmouseup = function(){me.Cancel()};

	}

	function SelectColor(color)
	{
		text_box.value = color;
		picker.style.backgroundColor = '#' + ((me.IsRgb(color))? color : 'FFFFFF');
		color_frame.style.visibility = "hidden";
		text_box = null;
		picker = null;
		on_change_function(color);
	}

	this.Cancel = function()
	{
		if (text_box != null && picker != null)
		{
			text_box.value = start_value;
			picker.style.backgroundColor = '#'+((me.IsRgb(start_value)) ? start_value : 'FFFFFF');
			color_frame.style.visibility = "hidden";
			on_change_function(start_value);
		}
		text_box = null;
		picker = null;
	}

	function MouseOver(color)
	{
		text_box.value = color;
		picker.style.backgroundColor = '#' + ((me.IsRgb(color)) ? color : 'FFFFFF');
		on_change_function(color);
	}

	this.IsRgb = function(color)
	{
		var m = color.match(/^([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])$/i);
		return (m) ? true : false;
	}
}



// This function inserts newNode after referenceNode
function insertAfter(referenceNode, newNode)
{
	referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling );
}



// get mouse coordinates
if (document.addEventListener)	// ff
{
	document.addEventListener("mousemove", ColorPickerMouseMove, false);
} 
else if (document.attachEvent) 		// ie
{
	document.attachEvent("onmousemove", ColorPickerMouseMove);
}
var color_picker_mouse_x = 0;
var color_picker_mouse_y = 0;
function ColorPickerMouseMove(e)
{
	color_picker_mouse_x = pointerX(e);
	color_picker_mouse_y = pointerY(e);
}
//cross browser functions for mouse coords
function pointerX(e)
{
	var posx = 0;
	if (!e) var e = window.event;
	if (e.pageX)
		posx = e.pageX;
	else if (e.clientX)
		posx = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;

	return posx;
}
function pointerY(e)
{
	if (!e) var e = window.event;
	if (e.pageY)
		return e.pageY;
	else if (e.clientY)
		return e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	else
		return 0
}

