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

	var tableWidget = 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 tableWidget links

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

					for (var k = (divs.length - 1); k >= 0; k--) {
						if (cssjs('check', divs[k], 'tableWidget')) {

							var h3s = divs[k].getElementsByTagName('h3');
							var tables = divs[k].getElementsByTagName('table');

							if (h3s.length == 1 && h3s[0].parentNode == divs[k] && tables.length == 1) {

								addLinkEvent(h3s[0], tableWidget.toggle);

							}

						}
					}

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

					tableWidget.currentlyOpen = null;

			}

		//--------------------------------------------------
		// Toggle the tableWidgetOpen class

			this.toggle = function () {

				//--------------------------------------------------
				// Close the currently open widget

					if (tableWidget.currentlyOpen !== null) {
						cssjs('remove', tableWidget.currentlyOpen, 'tableWidgetOpen');
					}

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

					if (tableWidget.currentlyOpen === this.parentNode) {
						tableWidget.currentlyOpen = null;
						return;
					}

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

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

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

					tableWidget.currentlyOpen = this.parentNode;

				//--------------------------------------------------
				// 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.tableWidget table { position: absolute; left: -5000px; }');
			addCssRule('div.tableWidgetOpen table { position: static; }');

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

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

	}

