/**
 * JavaScript header for A+ webpages
 * Version 1.0, September 2006
 *
 * These functions inject HTML into the webpage.
 *
 * @author      John King
 */

var contentsTreeWidth = 200;

hideContents = getCookie("AplusHideContents");
if (! hideContents) {
	insertContents();
	startPadding();
} else {
	addTreeOption();
}

/**
 * Inserts a fixed-position <IFRAME> onto the page.
 */
function insertContents() {
	document.write('  <div id="contentsTree" class="static" style="width: ' + contentsTreeWidth + 'px;">\n');
	document.write('   <iframe src="contents.html" frameborder="0" width="' + contentsTreeWidth + '" height="400"></iframe>\n');
	document.write('  </div>\n');
}

/**
 * Nudges the main page contents across so they are not obscured by the
 * contents iframe. 
 * 
 * I've used an empty table cell to do this.
 * 
 * I'd previously set the body padding-left to do this, but altering that
 * padding size with JavaScript affected all @media modes, creating a gap
 * on printouts .
 *   
 * Another possible solution was to nudge things across with a floating div tag.  
 * However the float style is affected by the special CSS used to fake 
 * 'position: fixed' in IE5/6.
 * 
 * The 'padding.gif' width is most important because IE5/6 will shrink 
 * fixed-width table cells if it thinks it can.     
 */
function startPadding() {
	document.write('  <table id="contentsTreePadding"><tr>');
	document.write('<td id="padding" style="width: ' + contentsTreeWidth + 'px;">');
	document.write('<img src="padding.gif" style="width: ' + contentsTreeWidth + 'px;">');
	document.write('</td><td id="main">\n');
}

/**
 * Adds an option to switch the tree back on.
 */
function addTreeOption() {
	document.write('  <div class="showTree">[<a href="javascript: showTree();">Show tree</a>]</div>\n');
}



/**
 * JavaScript Cookie Library
 *
 * Useful functions for saving and loading cookies.
 * Version 1.0
 *
 * NOTE: it's not possible to #include .js files so I've just pasted cookies.js here.
 *
 * @author	John King
 */

/**
 * Creates (or overwrites) a cookie on the user's web browser.
 *
 * @param	name	The name of the cookie.
 * @param	value	The value to set the cookie to.
 * @param	days	(optional) How many days the cookie should live.
 * 			Default is current session only.
 * 			A negative value will remove the cookie.
 * @param	path	(optional) The path that the cookie is valid for.
 * 			Default is path of the calling document.
 * @param	domain	(optional) The domain that the cookie is valid for.
 * 			Default is domain of the calling document.
 * @param	secure	(optional) Boolean - is secure transmission required?
 */
function setCookie(name, value, days, path, domain, secure) {
	if (name) {
		var newcookie = escape(name) + "=" + escape(value);
		if (days) {
			var date = new Date();
			date.setTime(date.getTime() + (days*24*60*60*1000));
			newcookie += "; expires=" + date.toGMTString();
		}
		if (path) newcookie += "; path=" & path;
		if (domain) newcookie += "; domain=" & domain;
		if (path) newcookie += "; path=" & path;
		if (secure) newcookie += "; secure=" & secure;
		document.cookie = newcookie;
	}
}

/**
 * Deletes a cookie from the user's web browser.
 *
 * @param	name	The name of the cookie.
 */
function deleteCookie(name) {
	setCookie(name, "", -1);
}

/**
 * Gets the value of a cookie from the user's web browser.
 *
 * @param	name  	The name of the cookie.
 *
 * @return	The value of the cookie.
 * 		If no cookie was found, returns null.
 */
function getCookie(name) {
	var value = null;
	if (name) {
		name = escape(name) + "=";
		var chunks = document.cookie.split(';');
		for (var i=0; i < chunks.length; i++) {
			var crumb = chunks[i];
			while (crumb.charAt(0) == ' ') {
				crumb = crumb.substring(1, crumb.length);
			}
			if (crumb.indexOf(name) == 0) {
			        crumb = crumb.substring(name.length, crumb.length);
				value = unescape(crumb);
			}
		}
	}
	return value;
}


