/*******************************************************************************************
 * addLoadEvent
 * Originally written by Simon Willison (http://simonwillison.net/2004/May/26/addLoadEvent/)
 * Incorporated with code by Dean Edwards (http://dean.edwards.name/weblog/2006/06/again)
 * Takes a function as an argument which should be executed once the DOM has loaded
 * Parameters: function func
 * Example:
 *		addLoadEvent(myFunction);
 *		addLoadEvent(function() {
 *			alert ('a');
 *		});
 *******************************************************************************************/

	//--------------------------------------------------
	// Loading setup

		var addLoadEventStack = []; // Array
		var addLoadEventCount = 0;

		function addLoadEvent(func) {
			addLoadEventStack[addLoadEventCount++] = func;
		}

	//--------------------------------------------------
	// Execute the functions when the page has loaded

		var addLoadEventDone = false;
		var addLoadEventSafariTimer;

		function addLoadEventInit() {

			//--------------------------------------------------
			// Do not run the functions twice

				if (addLoadEventDone) {
					return;
				} else {
					addLoadEventDone = true;
				}

			//--------------------------------------------------
			// Remove the loading timer for Safari

				if (addLoadEventSafariTimer) {
					clearInterval(addLoadEventSafariTimer);
				}

			//--------------------------------------------------
			// Execute the functions - cannot use variable 'i'
			// as the executed functions scope could change it

				var addLoadEventProgress;
				for (addLoadEventProgress = 0; addLoadEventProgress < addLoadEventCount; addLoadEventProgress++) {
					addLoadEventStack[addLoadEventProgress]();
				}

		}

	//--------------------------------------------------
	// Triggers for the different browsers

		//--------------------------------------------------
		// For DOM compatible browsers - Firefox and Opera

		 	if (document.addEventListener) {
		 		document.addEventListener('DOMContentLoaded', addLoadEventInit, false);
		 	}

		//--------------------------------------------------
		// For Safari

			if (/WebKit/i.test(navigator.userAgent)) { // sniff
				addLoadEventSafariTimer = setInterval(function() {
					if (/loaded|complete/.test(document.readyState)) {
						addLoadEventInit(); // call the onload handler
					}
				}, 10);
			}

		//--------------------------------------------------
		// For Internet Explorer

			/*@cc_on @*/
			/*@if (@_win32)

				document.write('<script id="addLoadEventIeOnload" defer="defer" src=//:><\/script>');
				var script = document.getElementById("addLoadEventIeOnload");
				script.onreadystatechange = function() {
					if (this.readyState === "complete") {
						addLoadEventInit(); // call the onload handler
					}
				};

			@end @*/

		//--------------------------------------------------
		// Fall back for other browsers

			window.onload = addLoadEventInit;

/*******************************************************************************************
 * cssjs
 * Originally written by Christian Heilmann (http://icant.co.uk)
 * Eases the dynamic application of CSS classes via DOM
 * Example:	cssjs('add', document.getElementById('foo'), 'bar');
 *******************************************************************************************/

	function cssjs(action, object, className) {

		var exp;

		if (action === 'add') {

			if (!cssjs('check', object, className)) {
				object.className += (object.className === '' ? '' : ' ') + className;
			}

		} else if (action === 'remove') {

			exp = new RegExp('(^' + className + '( |$)| ' + className + '\\b)');
			object.className = object.className.replace(exp, '');

		} else if (action === 'check') {

			exp = new RegExp('\\b' + className + '\\b');
			return exp.test(object.className);

		}

		return true;

	}


/*******************************************************************************************
 * 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;
						cssjs('add', this, 'linkedHolderHover');
					}

					holder.onmouseout = function () {
						window.status = '';
						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();
			});

	}
