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

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

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

					for (var k = (divs.length - 1); k >= 0; k--) {
						if (cssjs('check', divs[k], 'surveyWidget')) {
							var h4s = divs[k].getElementsByTagName('h4');
							var childDivs = divs[k].getElementsByTagName('div');
 
							if (h4s.length == 1) {
								addLinkEvent(h4s[0], surveyWidget.toggle)
							}
						}
					}

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

					surveyWidget.currentlyOpen = null;

			}

		//--------------------------------------------------
		// Toggle the surveyWidgetOpen class

			this.toggle = function () {

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

					if (surveyWidget.currentlyOpen !== null) {
						cssjs('remove', surveyWidget.currentlyOpen, 'surveyWidgetOpen');
					}

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

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

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

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

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

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

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

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

	}

