/*******************************************************************************************
 * linkedHolder
 * Written by Craig Francis
 * Allow a holder (like a div) to become a link, to create an effect like a banner. The
 * holder must contain only one link, and its href value is used for the destination.
 *******************************************************************************************/

	var linkedHolder = new function () {

		//--------------------------------------------------
		// Do not allow older browsers to run this script

			if (!document.getElementById || !document.getElementsByTagName) {
				return;
			}

		//--------------------------------------------------
		// Initialisation function used to define the global
		// variables used in this script

			this.init = function () {

				//--------------------------------------------------
				// Process all the elements on this page

					var holders = document.getElementsByTagName('div'); // Should be '*' when Safari gets support, or alternative found

					for (var i = (holders.length - 1); i >= 0; i--) {
						if (cssjs('check', holders[i], 'jsLinkedHolder')) {
							linkedHolder.setupHolder(holders[i]);
						}
					}

			}

		//--------------------------------------------------
		// Function to setup a holder

			this.setupHolder = function (holder) {

				//--------------------------------------------------
				// If a single link cannot be found, then give up

					var links = holder.getElementsByTagName('a');
					if (links.length != 1) {
						return;
					}

				//--------------------------------------------------
				// Add the onclick event handler

					//--------------------------------------------------
					// Override the current links onclick - so if the
					// user clicks on the link, its event and this event
					// does not trigger the same code twice (i.e.
					// possibly creating two popups).

						if (links[0].onclick) {
							links[0].linkedHolderOnClick = links[0].onclick;
							links[0].onclick = function () {
								return false;
							}
						}

					//--------------------------------------------------
					// Keep a reference to the link

						holder.linkedHolderLink = links[0];

					//--------------------------------------------------
					// Function which will try to use the links onclick,
					// otherwise fall back to using the standard link

						holder.onclick = function () {
							if (this.linkedHolderLink.linkedHolderOnClick) {
								this.linkedHolderLink.linkedHolderOnClick();
							} else {
								window.location = this.linkedHolderLink.href;
							}
						}

				//--------------------------------------------------
				// Add the onmouseover class - cannot use the :hover
				// rule in CSS as IE5/6 WIN only applies this to links.

					holder.onmouseover = function () {

						window.status = this.linkedHolderLink.href;

						if (cssjs('check', this, 'jsLinkedHolderUseHoverClass')) { // Must opt-in, as IE6 flickers quite a bit with this
							cssjs('add', this, 'linkedHolderHover');
						}

					}

					holder.onmouseout = function () {

						window.status = '';

						if (cssjs('check', this, 'jsLinkedHolderUseHoverClass')) {
							cssjs('remove', this, 'linkedHolderHover');
						}

					}

				//--------------------------------------------------
				// Add a class so we can tell if the link is active

					cssjs('add', this, 'linkedHolderActive');

				//--------------------------------------------------
				// Try to style up the holder so it appears as a link,
				// although IE5 WIN complains on the use of 'pointer'

					try {
						holder.style.cursor = 'pointer';
					} catch (e) {
						try {
							holder.style.cursor = 'hand';
						} catch (e) {
						}
					}

			}

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

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

	}
