/*******************************************************************************************
 * linkedWidget
 * Written by Craig Francis
 * Functionality to show/hide parts of a page in a widget style.
 *******************************************************************************************/

	var linkedWidget = 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 () {

				//--------------------------------------------------
				// Regular expression to extract the anchor link

					linkedWidget.anchorRegExp = new RegExp('#(.+)$');
					linkedWidget.topAnchorRegExp = new RegExp('#$');

				//--------------------------------------------------
				// Setup the 'jsLinkedWidget' links on the page

					var links = document.getElementsByTagName('a');
					for (k = (links.length - 1); k >= 0; k--) {
						if (cssjs('check', links[k], 'jsLinkedWidget')) {
							linkedWidget.setupLink(links[k]);
						}
					}

			}

		//--------------------------------------------------
		// When given an anchor link, hide the target
		// and setup the link as the toggle

			this.setupLink = function (link) {

				//--------------------------------------------------
				// Get the targetId

					var linkAnchor = linkedWidget.anchorRegExp.exec(link.href);
					if (linkAnchor && linkAnchor.length == 2) {
						var targetId = linkAnchor[1];
					} else {
						return;
					}

				//--------------------------------------------------
				// Try to get a reference to the target div.

					var target = document.getElementById(targetId);
					if (!target) {
						return;
					}

				//--------------------------------------------------
				// Hide the target

					target.style.position = 'absolute';
					target.style.left = '-5000px';

				//--------------------------------------------------
				// Setup the action so that this link can toggle
				// the target

					link.linkedWidgetTarget = target;
					link.onclick = linkedWidget.toggleTarget;

				//--------------------------------------------------
				// See if we have any close links in the target

					var closeLinks = target.getElementsByTagName('a');
					for (j = (closeLinks.length - 1); j >= 0; j--) {

						if (cssjs('check', closeLinks[j], 'jsLinkedWidget') && linkedWidget.topAnchorRegExp.test(closeLinks[j].href)) {
							closeLinks[j].linkedWidgetTarget = target;
							closeLinks[j].onclick = linkedWidget.toggleTarget;
						}
					}

			}

		//--------------------------------------------------
		// When one of the toggle links has been used

			this.toggleTarget = function () {

				//--------------------------------------------------
				// Toggle the display

					if (this.linkedWidgetTarget.style.position == 'absolute') {
						this.linkedWidgetTarget.style.position = 'static';
					} else {
						this.linkedWidgetTarget.style.position = 'absolute';
					}

				//--------------------------------------------------
				// Stop the link from working normally

					return false;

			}

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

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

	}
