// Workaround for Windows MSIE 1-6 not supporting CSS2 "position: fixed":
// exploits the fact that WinIE appears to render "position: absolute" as fixed within viewport.
// Thereforeset create var "positionfixed" initially "fixed" but set to "absolute" if browser is Win MSIE 1-6, 
// and use this in "shownote" function to reset element style.position.
// "hidenote" has short delay.
// William Robertson, www.williamrobertson.net 2003


var positionfixed = "fixed";  // Set to alternate value later if workaround required (thanks Microsoft)

var browserSupports = 
	{ getelement: false,
	  position_fixed: true };

if (document.getElementById)
	browserSupports.getelement = true;

// Workaround for CSS2 "position: fixed" which is supported by all browsers except Win IE1-6:
if (navigator.platform.search(/Win/) >= 0 && navigator.appVersion.search(/MSIE [1-6]/) >= 0)
{
	browserSupports.position_fixed = false
	positionfixed = "absolute";  // We will set elements' position to [positionfixed]
}

var contentswin = null;

function menuitemcurrent()
{
	this.style.color = "yellow";
}

function shownote(p_footnote)
{
	var f = document.getElementById(p_footnote);

	f.style.position = positionfixed;  // Has special value if Win IE1-6
	f.style.visibility = "visible";
	f.style.zIndex = "99";
}

function hidenote(p_footnote)
{
	// window.setTimeout('actuallyhidenote("' + p_footnote + '")', 1500);
	window.setTimeout('actuallyhidenote("' + p_footnote + '")', 3500);
}

function actuallyhidenote(p_footnote)
{
	var f = document.getElementById(p_footnote);
	f.style.visibility = "hidden";
	f.style.zIndex = "0";
}

function showcontents(p_document)
{
	function textcontent(p_element)
	{
		// Using W3C DOM, build text string from contents of an element
		// (adapted from "JavaScript The Definitive Guide", David Flanagan, O'Reilly 2002)
		// Returns the text from the specified element, e.g. a heading.
		var v_text = "";
		var v_children = p_element.childNodes;

		for (var i = 0; i < v_children.length; i++)
		{
			var v_child = v_children[i];
			if ( v_child.nodeType == 3 ) // Node.TEXT_NODE
				v_text += v_child.data;
			else
				v_text += textcontent(v_child);  // Recurse...
		}

		return v_text;
	}

	// Note "contentswin" is a variable within the showcontents() function.
	// "navwin" is the name of the pop-up window.
	self.contentswin = window.open
		( ""         // URL
		, "navwin"   // Name
		, "menubar=yes,scrollbars=yes,resizable=yes,width=340,height=200" );

	var headings = p_document.getElementsByTagName("H2");

	contentswin.document.writeln("<HTML>\n<HEAD>\n<TITLE>" + p_document.title + ": Contents</TITLE>\n");
	contentswin.document.writeln("<LINK rel='stylesheet' type='text/css' href='../menu.css'>");
	// contentswin.document.writeln("<SCRIPT language=JavaScript type='TEXT/JAVASCRIPT'>");
	// contentswin.document.writeln("function retirement_monitor()\n{");
	// contentswin.document.writeln("}\n</SCRIPT");
	// contentswin.document.writeln("</HEAD>\n<BODY class='tint-bg-light' onload='retirement_monitor();'>");
	contentswin.document.writeln("</HEAD>\n<BODY class='tint-bg-light'>");
	contentswin.document.writeln("<h1 id=doctitle>Contents</h1>"); 

	for (var i = 0; i < headings.length; i++)
	{
		var heading = headings[i];
		var text = null;

		try { text = textcontent(heading); }  // Recursive function defined above
		catch(err)
		{
			text = heading.id + " " + heading.Text;
			if (( text == null) || (text == '')) text = heading.innerText;
		}

		contentswin.document.write
		( '<a class=tocpopup href="" onclick="opener.location.hash=\'' + heading.id + '\'; return false;">' );

		contentswin.document.write(text);

		contentswin.document.writeln('</a><br>');
	}

	contentswin.document.writeln("</BODY></HTML>");

	contentswin.document.close();  // Close the stream to contentswin's document for writes
	contentswin.focus(); 
}

function push_attributes_to_frame()
{
	try
	{
		if (top.document.title != document.title)
		{
			top.document.title = document.title;
		}
	}
	catch(forgetthewholething) { }
}

var fadein_id;  // ID for "fadein" animation

function fadein_signature (p_target_rgb)
{
	var v_element = document.getElementById('signature');
	var v_rgb = v_element.style.getPropertyValue("color").match(/\d+/g);
	var v_changed = false;

	for (var i=0; i < v_rgb.length; i++) {
		if ( v_rgb[i] > p_target_rgb[i] )
		{
			v_rgb[i]--
			v_changed = true;
		}
	}

	if (v_changed)
		v_element.style.setProperty("color", "rgb(" + v_rgb.join() + ")", "");
	else
		window.clearInterval(fadein_id);
}

function fadein
	( p_element       // Name of element to fade in
	, p_target_rgb )  // Target colour
{
	var v_element = document.getElementById(p_element);
	var v_rgb = v_element.style.getPropertyValue("color").match(/\d+/g);
	var v_changed = false;

	for (var i=0; i < v_rgb.length; i++) {
		if ( v_rgb[i] > p_target_rgb[i] )
		{
			v_rgb[i]--
			v_changed = true;
		}
	}

	if (v_changed)
		v_element.style.setProperty("color", "rgb(" + v_rgb.join() + ")", "");
	else
		window.clearInterval(fadein_id);
}
