/*******************************************************************************************
 * cssjs
 * Written by Christian Heilmann (http://icant.co.uk)
 * Eases the dynamic application of CSS classes via DOM
 * Parameters: action a, object o and class names c1 and c2 (c2 optional)
 * Actions: swap exchanges c1 and c2 in object o
 *			add adds class c1 to object o
 *			remove removes class c1 from object o
 *			check tests if class c1 is applied to object o
 * Example:	cssjs('swap',document.getElementById('foo'),'bar','baz');
 *******************************************************************************************/

function cssjs (a, o, c1, c2) {
	switch (a) {
		case 'swap':
			o.className = !cssjs('check', o, c1) ? o.className.replace(c2, c1) : o.className.replace(c1, c2);
			break;
		case 'add':
			if (!o.className)
				o.className = '';
			if (!cssjs('check', o, c1)) {
				o.className += o.className ? ' '+c1 : c1;
			}
			break;
		case 'remove':
			var rep = o.className.match(' '+c1) ? ' '+c1 : c1;
			o.className = o.className.replace(rep, '');
			break;
		case 'check':
			if (!o.className)
				return(false);
			return(new RegExp('\\b'+c1+'\\b').test(o.className));
			break;
	}
	return(true);
}


/*******************************************************************************************
 * 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;


/*******************************************************************************************
 * 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();
			});

	}
	
	/*******************************************************************************************/
	/* MSDN Fix for CSS Hover flicker in IE -												   */
	/* http://www.hedgerwow.com/360/bugs/dom-fix-ie6-background-image-flicker.html		       */
	/*******************************************************************************************/

	 function fixCSSHover() 
	{ 	
		/*Use Object Detection to detect IE6*/
		var  m = document.uniqueID /*IE*/
		&& document.compatMode  /*>=IE6*/
		&& !window.XMLHttpRequest /*<=IE6*/
		&& document.execCommand ;

		try
		{
			if(!!m)
			{
				m("BackgroundImageCache", false, true) /* = IE6 only */ 
			}

		}
		catch(oh)
		{
		};
		
	}

