/*******************************************************************************************
 * Cookie functions
 * Written by Scott Andrew (edited by http://www.quirksmode.org/js/cookies.html)
 *******************************************************************************************/

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


/*******************************************************************************************
 * dashboardWidget
 * Written by Craig Francis
 * Provide widget functionality to hide tables
 *******************************************************************************************/

	var dashboardWidget = new function () {

		//--------------------------------------------------
		// Do not allow older browsers to run this script
			if (!document.getElementById || !document.getElementsByTagName) {
				return;
			}

		//--------------------------------------------------
		// Initialisation function used for setup

			this.init = function () {

				//--------------------------------------------------
				// Find all the dashboardWidget links

					var divs = document.getElementsByTagName('div');

					for (var k = (divs.length - 1); k >= 0; k--) {
						if (cssjs('check', divs[k], 'dashboardWidget')) {
							var h4s = divs[k].getElementsByTagName('h4');
							var childDivs = divs[k].getElementsByTagName('div');
 
							if (h4s.length == 1) {
							
								addLinkEvent(h4s[0], dashboardWidget.toggle)
								
								if(readCookie(h4s[0].parentNode.id) == 'true') {
									cssjs('add', h4s[0].parentNode, 'dashboardWidgetOpen');
								}
							}
						}
					}

				//--------------------------------------------------
				// Currently open div

					dashboardWidget.currentlyOpen = null;

			}

		//--------------------------------------------------
		// Toggle the dashboardWidgetOpen class

			this.toggle = function () {

				//--------------------------------------------------
				// If the action is to close current widget

					if (readCookie(this.parentNode.id) == 'true') {
						cssjs('remove', this.parentNode, 'dashboardWidgetOpen');
						eraseCookie(this.parentNode.id);
						
						return;
					}

				//--------------------------------------------------
				// Open new widget

					cssjs('add', this.parentNode, 'dashboardWidgetOpen');

				//--------------------------------------------------
				// Record currently open widget

					createCookie(this.parentNode.id, 'true', 365);

				//--------------------------------------------------
				// Scroll the window

					var top = findPosY(this);
					if (top > 0) {
						window.scroll(0, top);
					}

				//--------------------------------------------------
				// Get Safari 2.0.4 to re-render the page

					this.appendChild(document.createTextNode(' '));

			}

		//--------------------------------------------------
		// Set JS specific styles ready for page load.

			addCssRule('div.dashboardWidget div.dashboardEntries { position: absolute; left: -5000px; }');
			addCssRule('div.dashboardWidgetOpen div.dashboardEntries { position: static; }');

		//--------------------------------------------------
		// When the page has loaded, run the init function

			addLoadEvent (function() {
				dashboardWidget.init();
			});

	}

