///////////////////////////////////////////////////////////////////////////////////////////////////
//	The syllabus pop-down menu that appears on all pages
///////////////////////////////////////////////////////////////////////////////////////////////////
//	Program Updates
//
//	09.07.2009	1.4.0	DJV		Fixed errors when no syllabus selected (ebQld/s not set).
//
///////////////////////////////////////////////////////////////////////////////////////////////////

var ebmDisplay = false;
var ebQld = null;
var ebQls = null;
var ebTm1 = null;
var ebTm2 = null;
var ebTlen = 0;
var ebTpos = 0;
var ebInscroll = false;
var ebInitscroll = false;
var ebStartSpeed = 300;
var ebSpeed;
var ebThisQ = 0;
var ebLines = 5;
var ebMo = -1;

var col1 = "#eeeeee";	// Normal top/bottom colour
var col2 = "#ffffff";	// Text colour
var col3 = "#912b43";	// Mouseover background colour
var col4 = "#c57015";	// Normal background colour
var col5 = "#a55000";	// Currently selected item bg colour
var col6 = "#e59035";	// Border top
var col7 = "#a55000";	// Border bottom
var col8 = "#ffeebb";	// Mouseover top/bottom colour

var cssStyle = "";

function ebSetColour (n, col, ks)
{
	var ebQb = document.getElementById ("ebqblk" + n);

	if (ebQb)
	{
		var ebQbs = ebQb.style;

		if (ks == 1) ebKillScroll ();

		ebQbs.color = col2;
		ebQbs.borderTop = "solid 1px " + col6;
		ebQbs.borderBottom = "solid 1px " + col7;

		if (col == 0)
		{
			if (ks == 0 && n == ebMo)
			{
				ebQbs.backgroundColor = col3;
			}
			else
			{
				ebQbs.backgroundColor = (n == ebThisQ ? col5 : col4);
			}
		}
		else
		{
			ebQbs.backgroundColor = col3;
			ebMo = n;
		}
	}
}

function ebJumpTo (ref)
{
	ebKillScroll ();
	ebRemoveMenu ();
	var newLoc = "/index.php?mode=syllabus&s=" + ebJlist [ref];
	window.location = newLoc;
}

function ebRemoveMenu ()
{
	ebInscroll = false;
	ebKillScroll ();

	if (ebQls)
	{
		ebQls.display = "none";
		ebQls.left = "0";
	}

	clearTimeout (ebTm1);
	ebTm1 = null;
	ebmDisplay = false;
}

function ebUpdateMenu ()
{
	var txt = "";
	var tmax = ebTpos + ebLines;

	if (tmax > ebTlen) tmax = ebTlen;

	if (ebTpos > 0)
	{
		txt += '<div id="ebup" class="qla" onmouseover="ebScrollMenu(1)" onmouseout="ebScrollMenu(0)">' +
			'<img src="/images/site/menuup' + cssStyle + '.gif"></div>';
	}
	else
	{
		txt += '<div id="ebup" class="qla"><img src="/images/site/menublank.gif"></div>';
	}

	var doRecolour = false;

	for (i = ebTpos; i < tmax; i++)
	{
		if (i == ebThisQ) doRecolour = true;
		txt += ebTlist [i];
		ebSetColour (i, 0, 0);
	}

	var qty = '';

	if (tmax < ebTlen)
	{
		txt += '<div id="ebdown" class="qla" onmouseover="ebScrollMenu(2)" onmouseout="ebScrollMenu(0)">' +
			qty + '<img src="/images/site/menudown' + cssStyle + '.gif"></div>';
	}
	else
	{
		txt += '<div id="ebdown" class="qla">' + qty + '<img src="/images/site/menublank.gif"></div>';

		if (ebTm2 != null)
		{
			clearTimeout (ebTm2);
			ebTm2 = null;
		}
	}

	if (ebQld) ebQld.innerHTML = txt;

	if (doRecolour) document.getElementById ("ebqblk" + ebThisQ).style.backgroundColor = col5;
}

function ebKillScroll ()
{
	ebInscroll = false;

	if (ebTm2 != null)
	{
		clearTimeout (ebTm2);
		ebTm2 = null;
	}
}

function ebScrollMenu (n)
{
	switch (n)
	{
		case 0 :
			if (!ebInitscroll)
			{
				ebKillScroll ();
				document.getElementById ("ebup").style.backgroundColor = col1;
				document.getElementById ("ebdown").style.backgroundColor = col1;
			}

			break;

		case 1 :
			if (!ebInscroll && ebTpos > 0)
			{
				ebSpeed = ebStartSpeed;
				ebInscroll = true;
				ebInitscroll = true;
				ebTpos--;
				ebUpdateMenu ();
				ebTm2 = setTimeout ("ebContinueScroll(1)", ebSpeed);
				document.getElementById ("ebup").style.backgroundColor = col8;
				ebMo = -1;
			}

			break;

		case 2 :
			if (!ebInscroll && ebTpos < ebTlen - ebLines)
			{
				ebSpeed = ebStartSpeed;
				ebInscroll = true;
				ebInitscroll = true;
				ebTpos++;
				ebUpdateMenu ();
				ebTm2 = setTimeout ("ebContinueScroll(2)", ebSpeed);
				document.getElementById ("ebdown").style.backgroundColor = col8;
				ebMo = -1;
			}

	}
}

function ebContinueScroll (n)
{
	ebInitscroll = false;
	ebKillScroll ();

	if (ebSpeed > 80) ebSpeed = parseInt (ebSpeed * 0.8);

	switch (n)
	{
		case 1 :
			if (ebTpos > 0)
			{
				ebInscroll = true;
				ebTpos--;
				ebUpdateMenu ();
				ebTm2 = setTimeout ("ebContinueScroll(1)", ebSpeed);

				if (ebTpos > 0)
				{
					document.getElementById ("ebup").style.backgroundColor = col8;
				}
			}

			break;

		case 2 :
			if (ebTpos < ebTlen - ebLines)
			{
				ebInscroll = true;
				ebTpos++;
				ebUpdateMenu ();
				ebTm2 = setTimeout ("ebContinueScroll(2)", ebSpeed);

				if (ebTpos < ebTlen - ebLines)
				{
					document.getElementById ("ebdown").style.backgroundColor = col8;
				}
			}

	}
}

function ebSetMenu (n)
{
	if (ebQls && n == 1)
	{
		if (!ebmDisplay)
		{
			var box = document.getElementById ("ebsyl1");

			var curleft = 0;
			var curtop = 0;

			// Note: this loop will fail if there is a "position: relative" box in the hierarchy

			if (box.offsetParent)
			{
				curleft = box.offsetLeft;
				curtop = box.offsetTop;

				while (box = box.offsetParent)
				{
					curleft += box.offsetLeft;
					curtop += box.offsetTop;
				}
			}

			ebUpdateMenu ();
			ebQls.display = "block";
			ebQls.top = "1px";
			ebQls.left = (curleft - 83) + "px";
			ebQls.width = "80px";
		}

		ebmDisplay = true;

		if (ebTm1 != null)
		{
			clearTimeout (ebTm1);
			ebTm1 = null;
		}

		ebInitMenu ();
	}
	else
	{
		ebTm1 = setTimeout ("ebRemoveMenu()", 250);
	}
}

function ebInitMenu ()
{
	for (i = 0; i < ebTlen; i++)
	{
		ebSetColour (i, 0, 0);
	}
}
