///////////////////////////////////////////////////////////////////////////////////////////////////
//	DLib Colour Field JavaScript routines - copyright davidviner.com 2009
//
//	22.02.2009	5.5.4	DJV		Now uses DLibUtilities.initAjax.
//
///////////////////////////////////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////////////////////////

DLibColourField = function ()
{
	var cf = new Object;

	var _private =
	{
		checkReadyState : function ()
		{
			return (cf.xmlHttp != null && cf.xmlHttp.readyState == 4 && cf.xmlHttp.status == 200);
		},

		response : function ()
		{
			if (_private.checkReadyState ())
			{
				var msg = cf.xmlHttp.responseText;
				document.getElementById (cf.thisFld).value = msg.substring (0, 6);
				_public.process (cf.thisFld);
			}
		},

		colmove : function (eo)
		{
			if (eo != null)
			{
				cf.cX = eo.clientX;
				cf.cY = eo.clientY;
			}
			else
			{
				cf.cX = event.clientX;
				cf.cY = event.clientY;
			}
		}
	}

	var _public =
	{
		process : function (_cfld)
		{
			var col = document.getElementById (_cfld).value;
			var c = document.getElementById ('f' + _cfld);

			while (col.length < 6)
			{
				col += '0';
			}

			c.style.backgroundColor = '#' + col.substring (0, 6);
		},

		readColour : function (md)
		{
			sc = DLibUtilities.scrollOffset ();

			var picX = (cf.cX - cf.cppx + sc [0]);
			var picY = (cf.cY - cf.cppy + sc [1]);
			var close = true;
			var doPick = true;

			if (picY < 16)
			{
				if (!(picX > 106 && picX < 118 && picY > 1 && picY < 13))
				{
					close = false;
				}

				doPick = false;
			}

			if (close || md == 1)
			{
				var cpp = document.getElementById ("colpopup");
				cpp.style.position = "absolute";
				cpp.style.top = "-200px";
				cpp.style.left = "-200px";

				if (cf.x1 > -1 || md == 1)
				{
					var cpc = document.getElementById ("colclose");
					cpc.style.position = "absolute";
					cpc.style.top = "-200px";
					cpc.style.left = "-200px";
				}

				if (doPick && md == 0)
				{
					var url = "/dlib/php/colswatch.php?x=" + picX + "&y=" + picY;
					cf.xmlHttp = DLibUtilities.initAjax (url, _private.response);
				}
			}
		},

		popup : function (fld)
		{
			var fn = "/dlib/images/colswatch.png";

			cf.thisFld = fld;

			var box = document.getElementById ("f" + fld);
			var boxPos = DLibUtilities.findPos (box);

			var swatch = new Image (120, 90);
			swatch.src = fn;

			sc = DLibUtilities.scrollOffset ();
			cf.cX = boxPos [0] - sc [0];
			cf.cY = boxPos [1] - sc [1];

			// Center

			cf.cppx = parseInt (boxPos [0] - 60);
			cf.cppy = parseInt (boxPos [1] - 45);

			var cpp = document.getElementById ("colpopup");
			cpp.style.position = "absolute";
			cpp.style.left = cf.cppx + "px";
			cpp.style.top = cf.cppy + "px";
			cpp.style.cursor = "crosshair";
			cpp.innerHTML = '<img src="' + fn + '" border="1" alt="">';
			cpp.onmousemove = _private.colmove;

/*			if (cf.x1 > -1)
			{
				var cpc = document.getElementById ("colclose");
				cpc.style.position = "absolute";
				cpc.style.left = (cf.cppx + cf.x1 + 1) + "px";
				cpc.style.top = (cf.cppy + cf.y1 + 1) + "px";
				cpc.style.cursor = "default";
			}*/
		}
	};

	return _public;
} ();

///////////////////////////////////////////////////////////////////////////////////////////////////
