//------------------------------------------------------------------------
// All contents copyright 2005, Colin James Fitzpatrick.
// All rights reserved; you may not remove this notice.
//------------------------------------------------------------------------

//------------------------------------------------------------------------
// Bug Tracker -- JavaScript
//------------------------------------------------------------------------

//------------------------------------------------------------------------
// Globals
//------------------------------------------------------------------------
var g_bUsingIe = false;
var g_projects = new Array("progress", "trans3", "toolkit3", "miscellanea");
var g_historyTop = 0;
var g_historyLinks = "[ <a href=\"javascript: scrollHistory(-1);\">recent</a>, <a href=\"javascript: scrollHistory(1);\">dated</a> ]&nbsp;";
var g_historyLength = 7;

//------------------------------------------------------------------------
// Window values
//------------------------------------------------------------------------
window.onload = initialize;	// Main entry point

//------------------------------------------------------------------------
// Download a file
//------------------------------------------------------------------------
function downloadFile(fileIdx)
{
	var strConfirm =
		"Development builds are generally unstable, and soley for testing purposes. While we sympathize with any problems you experience, please remember that you are using at your own risk. In order to download a file from this site, you must agree to these terms. Download and agree?";
	if (confirm(strConfirm))
	{
		void window.open("?file=" + fileIdx, "", "resizable = no, scrollbars = no, width = 400, height = 150");
		window.location = "/";
	}
}

//------------------------------------------------------------------------
// Show a footnote
//------------------------------------------------------------------------
function showFootnote(idx, height)
{
	if (!height) height = 160;
	void window.open("footnote.php?id=" + idx, "", "resizable = no, scrollbars = no, width = 400, height = " + height);
}

//------------------------------------------------------------------------
// Update the history text
//------------------------------------------------------------------------
function updateHistoryText()
{
	var historyFloor = g_historyTop + g_historyLength;
	var historyCeiling = g_historyTop + 1;
	var historyText = g_historyLinks + "viewing " + historyCeiling + " to " + historyFloor + " of " + g_history.length;
	document.getElementById("currentHistory").innerHTML = historyText;
}

//------------------------------------------------------------------------
// Initialize the page
//------------------------------------------------------------------------
function initialize()
{
	if (document.getElementsByTagName)
	{
		var anchors = document.getElementsByTagName("a");
		for (var i = 0; i < anchors.length; i++)
		{
			var anchor = anchors[i];
			if (anchor.getAttribute("rel") == "external")
			{
				if (anchor.getAttribute("href"))
				{
					anchor.target = "_blank";
				}
			}
		}
	}
	// Show up to ten history entries
	var historySize = g_history.length < g_historyLength ? g_history.length : g_historyLength;
	for (var i = 0; i < historySize; i++)
	{
		var pEntry = document.getElementById("historyEntry" + i);
		pEntry.innerHTML = g_history[i];
	}
	if (historySize < g_historyLength)
	{
		for (var i = historySize; i < g_historyLength; i++)
		{
			var pEntry = document.getElementById("historyEntry" + i);
			pEntry.style.visibility = "hidden";
			// pEntry.style.display = "none";
		}
	}
	if (g_history.length < g_historyLength)
	{
		g_historyLength = g_history.length;
	}
	updateHistoryText();
	// Hide the projects
	/**for (var i = 1; i < 4; i++)
	{
		toggleProjectState(g_projects[i], null);
		document.getElementById(g_projects[i] + "_link").innerHTML = "&nbsp;+&nbsp;";
	}**/
	document.getElementById(g_projects[0] + "_link").innerHTML = "&nbsp;-&nbsp;";
}

//------------------------------------------------------------------------
// Change the showing history items
//------------------------------------------------------------------------
function scrollHistory(amount)
{
	var newTop = g_historyTop + amount;
	var newBottom = newTop + g_historyLength;
	if (newTop >= 0 && newBottom <= g_history.length)
	{
		for (var i = newTop, j = 0; i < newBottom; i++, j++)
		{
			var pEntry = document.getElementById("historyEntry" + j);
			pEntry.innerHTML = g_history[i];
			pEntry.style.visibility = "visible";
			// pEntry.style.display = "block";
		}
		g_historyTop = newTop;
		updateHistoryText();
	}
}

//------------------------------------------------------------------------
// Open or close a project
//------------------------------------------------------------------------
function changeProjectState(project, pLink, bState)
{
	var pLive = document.getElementById(project + "_live");
	var pCorrected = document.getElementById(project + "_corrected");
	var newState = (bState ? "visible" : "hidden");
	var newHeight = (bState ? "auto" : "0");
	var newPosistion = (bState ? "relative" : "absolute");
	if (project == "progress")
	{
		if (!bState)
		{
			for (var i = 0; i < g_historyLength; i++)
			{
				var pEntry = document.getElementById("historyEntry" + i);
				pEntry.innerHTML = "";
				pEntry.style.visibility = "hidden";
			}
		}
		else
		{
			scrollHistory(0);
		}
	}
	pLive.style.position = newPosistion;
	pLive.style.height = newHeight;
	pLive.style.visibility = newState;
	pCorrected.style.position = newPosistion;
	pCorrected.style.height = newHeight;
	pCorrected.style.visibility = newState;
	if (pLink) pLink.innerHTML = "&nbsp;" + (bState ? "-" : "+") + "&nbsp;";
}

//------------------------------------------------------------------------
// Determine whether a project is visibile
//------------------------------------------------------------------------
function isProjectVisible(project)
{
	var currentState = document.getElementById(project + "_live").style.visibility;
	return (currentState == "visible" || currentState == "");
}

//------------------------------------------------------------------------
// Hide and projects and obtain previous state
//------------------------------------------------------------------------
function flushAndStore()
{
	var toRet = new Array();
	for (var i = 0; i < 4; i++)
	{
		toRet[i] = isProjectVisible(g_projects[i]);
		changeProjectState(g_projects[i], null, false);
	}
	return toRet;
}

//------------------------------------------------------------------------
// Toggle the state of a project
//------------------------------------------------------------------------
function toggleProjectState(project)
{

	// Get the link for this project
	var pLink = document.getElementById(project + "_link");

	// Get height of the document
	var doc = document.getElementById("document");
	var minHeight = document.getElementById("leftBar").offsetHeight + 55;

	// If greater than max height, enlarge to use all space
	if (doc.offsetHeight > minHeight) doc.style.height = "auto";

	if (g_bUsingIe)
	{

		// Hide all the projects, then unroll them from top to bottom

		var visible = flushAndStore();
		for (var i = 0; i < 4; i++)
		{
			if (project != g_projects[i])
			{
				changeProjectState(g_projects[i], null, visible[i]);
			}
			else
			{
				changeProjectState(g_projects[i], pLink, !visible[i]);
			}
		}

		if (doc.offsetHeight <= minHeight)
		{

			// Document is shorter than side bar -- heighten
			doc.style.height = minHeight + "px";

			// Hide them all (again), and unroll once more
			var visible = flushAndStore();
			for (var i = 0; i < 4; i++)
			{
				changeProjectState(g_projects[i], null, visible[i]);
			}

		}

	}
	else
	{

		// Just change if using a real browser
		changeProjectState(project, pLink, !isProjectVisible(project));

	}

}
