/*******************************************************************************************
 * ulWidget
 * Written by Craig Francis
 * Provide widget functionality to hide uls
 *******************************************************************************************/

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

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

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

							var h3s = divs[k].getElementsByTagName('h3');
							var uls = divs[k].getElementsByTagName('ul');

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

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

							}

						}
					}

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

					ulWidget.currentlyOpen = null;

			}

		//--------------------------------------------------
		// Toggle the ulWidgetOpen class

			this.toggle = function () {

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

					if (ulWidget.currentlyOpen !== null) {
						cssjs('remove', ulWidget.currentlyOpen, 'ulWidgetOpen');
					}

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

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

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

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

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

					ulWidget.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.ulWidget ul { position: absolute; left: -5000px; }');
			addCssRule('div.ulWidgetOpen ul { position: static; }');

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

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

	}

