///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// IEIED General JavaScript functions
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//	10.02.2011	1.0.0	DJV		Created.
//	06.07.2011	1.0.1	DJV		Mods for delay to hover over list.
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var	cajax = null,
	cid = -1,
	scTm = null,
	killShow = false;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Handle the pop-out for the side menu
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function subCats (id)
{
	cid = id;

	if (scTm != null)
		doClrSubCats ();

	killShow = false;
	scTm = setTimeout ("subCats2()", 400);
}

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

function subCats2 ()
{
	clearTimeout (scTm);
	scTm = null;

	if (cajax == null)
	{
		cajax = DLibUtilities.initAjax ("/ajax/subcat.php?cref=" + cid, showSubCats);
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Display the subcategories popup
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

function showSubCats ()
{
	if (DLibUtilities.checkAjax (cajax))
	{
		if (!killShow)
		{
			var data = cajax.responseText;
			cajax = null;
			var cats = data.split ("`");

			if (scTm != null) clearTimeout (scTm);
			var box = document.getElementById ("subcats");
			var boxs = box.style;
			var smenu = document.getElementById ("mcat" + cid);
			var xy = DLibUtilities.findPos (smenu);
			var off = DLibUtilities.scrollOffset ();

			var txt = "<ul class=\"subcatlist\">\n";
			var cl = "noline";

			for (var i = 0; i < cats.length; i++)
			{
				var cp = cats [i].split ("^");

				if (cp[0] == -1)
				{
					txt += "<li class=\"" + cl + "\">" + cp[1] + "</li>\n";
				}
				else
				{
					txt += "<li class=\"" + cl + "\"><a href=\"/products.php?cref=" + cp[0] + "\">" + cp[1] + "</a></li>\n";
				}

				cl = "doline";
			}

			txt += "</ul>\n";

			box.innerHTML = txt;
			var h = cats.length * 14;
			boxs.width = "auto";
			boxs.display = "block";
			boxs.position = "absolute";
			boxs.left = (xy [0] + 190 - off [0]) + "px";
			var top = (xy [1] - (h / 2) + 5);
			var wh = DLibUtilities.getWindowSize ();
			box.onmouseover = clrSubCatTimer;
			box.onmouseout = clrSubCats;
			var boxHt = box.clientHeight;

			if (top + boxHt + 15 > wh [1] + off [1])	// Keep box onscreen
			{
				top = wh [1] - boxHt - 15 + off [1];
			}

			if (top - off[1] < 0)
			{
				top = off [1] + 5;
			}

			boxs.top = parseInt (top) + "px";

			var scpa = document.getElementById ("scpoparrow").style;
			scpa.display = "block";
			scpa.left = (xy [0] + 183 - off [0]) + "px";
			scpa.top = (xy[1] + 5) + "px";
		}

		cajax = null;
		killShow = false;
	}
}

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

function clrSubCats (e)
{
	scTm = setTimeout ("doClrSubCats()", 700);
	killShow = true;
}

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

function clrSubCatTimer (e)
{
	if (scTm != null)
	{
		clearTimeout (scTm);
		scTm = null;
	}

	killShow = true;
}

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

function doClrSubCats ()
{
	clearTimeout (scTm);
	scTm = null;
	var box = document.getElementById ("subcats");
	var boxs = box.style;
	box.innerHTML = "";
	boxs.display = "none";
	boxs.position = "absolute";
	boxs.left = "-1000px";
	boxs.top = "-1000px";
	var scpa = document.getElementById ("scpoparrow").style;
	scpa.display = "none";
	scpa.position = "absolute";
	scpa.left = "-1100px";
	scpa.top = "-1000px";
}

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

