////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//	DLib Colour Field JavaScript routines - copyright davidviner.com 2009-2011
//
//	30.09.2011	5.10.2	DJV		$id() mods.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

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

DLibColourField = function ()
{
	var cf = new Object,
		cpp,
		cpc;

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

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

			if (cf.cY > cf.cppy)
			{
				if (cf.cY < cf.cppy + 15)
					cpp.style.cursor = "default";
				else
					cpp.style.cursor = "crosshair";
			}
		}
	}

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

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

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

			if (cb != undefined)
				eval (cb + '("' + _cfld + '","#' + col.substring (0, 6) + '")');
		},

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

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

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

				doPick = false;
			}

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

				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, cb)
		{
			var fn = "/dlib/images/colswatch.png";

			cf.thisFld = fld;
			cf.callback = cb;

			var box = $id ("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);

			cpp = $id ("colpopup");
			cpc = $id ("colclose");

			cpp.style.position = "absolute";
			cpp.style.left = cf.cppx + "px";
			cpp.style.top = cf.cppy + "px";
			cpp.onmousemove = _private.colmove;
			cpp.style.cursor = "crosshair";
			cpp.style.zIndex = 100;
			cpp.innerHTML = "&nbsp;Colour Selector";

			cpc.style.position = "absolute";
			cpc.style.left = (cf.cppx + 107) + "px";
			cpc.style.top = (cf.cppy + 3) + "px";
			cpc.style.cursor = "pointer";
			cpc.style.zIndex = 110;
		}
	};

	return _public;
} ();

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

