/*******************************************************************************************
 * flashReplace
 * Written by Craig Francis
 * If the browser can support flash, replace the flash alternative with the flash object
 *******************************************************************************************/

//--------------------------------------------------
// Config variables, if not already defined

	var flashObjects = new Array();
	var flashUseXmlMethods = (document.contentType && document.contentType.indexOf('xml') > -1);

	if (typeof flashRequired == 'undefined' || flashRequired < 6) {
		var flashRequired = 6;
	}

	if (typeof flashDetectObjectUrl == 'undefined') {
		var flashDetectObjectUrl = '/a/swf/common/flashDetect.swf';
	}

	if (typeof flashReloadGifs == 'undefined' || flashUseXmlMethods) {
		var flashReloadGifs = false;
	}

//--------------------------------------------------
// Config for the flash enable and disable links,
// defined here to ensure scope is set correctly

	if (typeof flashBeforeLinkText  == 'undefined') var flashBeforeLinkText  = 'Flash [ ';
	if (typeof flashEnableLinkText  == 'undefined') var flashEnableLinkText  = 'On';
	if (typeof flashBetweenLinkText == 'undefined') var flashBetweenLinkText = ' / ';
	if (typeof flashDisableLinkText == 'undefined') var flashDisableLinkText = 'Off';
	if (typeof flashAfterLinkText   == 'undefined') var flashAfterLinkText   = ' ]';

//--------------------------------------------------
// Simple function to set the flashAvailable
// variable in a cookie and store in a variable
// for this script

	var flashAvailable;

	function setFlashAvailable (version) {
		flashAvailable = version;
		document.cookie = 'flashAvailable=' + escape(version) + '; path=/';
	}

//--------------------------------------------------
// Initial value of "flashAvailable"

	//--------------------------------------------------
	// See if there is a cookie set which can tell
	// us which version of flash the user has (from
	// previous page loads)

		flashAvailable = 0; // RUN TEST
		var cookieString = document.cookie;

		if (cookieString && cookieString.match(/^.*flashAvailable=([\-0-9]+).*$/i)) {
			flashAvailable = parseInt(cookieString.replace(/^.*flashAvailable=([\-0-9]+).*$/i, '$1'));
		}

	//--------------------------------------------------
	// See if we are being told what todo

		var locationString = window.location.search;

		if (locationString.match(/(\?|&)flashReplace=false/i)) {

			setFlashAvailable (-1); // SOFT DISABLE

		} else if (locationString.match(/(\?|&)flashReplace=true/i)) {

			if (flashAvailable < 1) {
				setFlashAvailable (0); // RUN TEST
			}

		}

	//--------------------------------------------------
	// If we have to check if flash exists (0), then
	// do a quick sanity check to see if the plugin
	// exists (if it doesn't, we should disable flash
	// now... to save on processing). This section of
	// code was origionally written by Christopher
	// Sheldon from http://www.microagesolutions.com

		if (flashAvailable == 0) {
			var pluginVersion = -1;
			if (navigator.plugins && navigator.plugins.length) {
				for (var i = (navigator.plugins.length - 1); i >= 0 && pluginVersion == -1; i--) {
					if (navigator.plugins[i].name.indexOf('Shockwave Flash') != -1) {
						pluginVersion = parseInt(navigator.plugins[i].description.split('Shockwave Flash ')[1]);
					}
				}
			} else if (window.ActiveXObject) {
				for (var i = (flashRequired + 10); i >= flashRequired; i--) {
					try {
						oFlash = eval('new ActiveXObject("ShockwaveFlash.ShockwaveFlash.' + i + '");');
						if (oFlash) {
							pluginVersion = i;
						}
					} catch(e) {
					}
				}
			}
			if (pluginVersion < flashRequired) {
				setFlashAvailable (-2); // HARD DISABLE
			}
		}

//--------------------------------------------------
// Function to setup a new flash object, this
// can be used several times from the HTML page

	function flashReplace (divId, flashUrl, flashWidth, flashHeight, flashBgColour) {

		//--------------------------------------------------
		// If this is a SOFT DISABLE, we will give up soon,
		// but first, add the flash disable links. Although
		// technically this is called multiple times

			if (flashAvailable == -1) {
				addLoadEvent (flashDisableLinks);
			}

		//--------------------------------------------------
		// If version of flash is not good enough, give up

			if (flashAvailable < 0 || (flashAvailable > 0 && flashAvailable < flashRequired)) {
				return;
			}

		//--------------------------------------------------
		// Temporarily hide the flash object

			var cssRule = ' #' + divId + ' {';
			cssRule += '  visibility: hidden; ';
			cssRule += '  width: ' + flashWidth + 'px;';
			cssRule += '  height: ' + flashHeight + 'px;';
			cssRule += '}';

			if (flashUseXmlMethods) {

				var styleElement = createElement('style');
				var styleContent = document.createTextNode(cssRule);

				styleElement.setAttribute('type', 'text/css');
				styleElement.appendChild(styleContent);

				var headRef = document.getElementsByTagName('head');
				if (headRef[0]) {
					headRef[0].appendChild(styleElement);
				}

			} else {

				document.write ('<style type="text/css">' + cssRule + '<\/style>');

			}

		//--------------------------------------------------
		// Store the details in an array

			flashObjects[flashObjects.length] = {
				divId: divId,
				flashUrl: flashUrl,
				flashWidth: flashWidth,
				flashHeight: flashHeight,
				flashBgColour: flashBgColour
			}

		//--------------------------------------------------
		// If this is the first flash object, then ONLOAD
		// either create the flash detect object or bypass
		// straight into flashReplaceProcess (if we already
		// know which version of flash the user has)

			if (flashObjects.length == 1) {
				if (flashAvailable == 0) {
					addLoadEvent (flashDetectObjectAdd);
				} else {
					addLoadEvent (flashReplaceProcess);
				}
			}

	}

//--------------------------------------------------
// Self looping function to create the flash
// detection object... as soon as the body is
// available, then add it!

	function flashDetectObjectAdd () {

		//--------------------------------------------------
		// Untill we get a responce from the flash object,
		// it should be taken that the browser does not
		// support flash (for next page load)

			setFlashAvailable (-1); // SOFT DISABLE

		//--------------------------------------------------
		// Get a reference to the body

			var bodyRef = document.getElementsByTagName('body');
			if (bodyRef[0]) {
				bodyRef = bodyRef[0];
			} else {
				flashRestoreAlt();
				return;
			}

		//--------------------------------------------------
		// Create a div which will act as an anchor for
		// a small flash detect object

			var divTag = createElement('div');

			divTag.setAttribute('id', 'flashDetectObject');
			divTag.style.position = 'absolute';
			divTag.style.top = '0';
			divTag.style.left = '0';

		//--------------------------------------------------
		// Create the flash object, if the proper XML method
		// does not work (IE), then do it the old-school way

			if (flashUseXmlMethods) {

				var objTag = createElement('object');
				var objMovieParam = createElement('param');

				objTag.setAttribute('type','application/x-shockwave-flash');
				objTag.setAttribute('width',  '1');
				objTag.setAttribute('height', '1');
				objTag.setAttribute('data', flashDetectObjectUrl);

				objMovieParam.setAttribute('name','movie');
				objMovieParam.setAttribute('value', flashDetectObjectUrl);

				objTag.appendChild(objMovieParam);
				divTag.appendChild(objTag);

			} else {

				var flashHTML  = '<object type="application/x-shockwave-flash" data="' + flashDetectObjectUrl + '" width="1" height="1">';
				flashHTML += '	<param name="movie" value="' + flashDetectObjectUrl + '" />';
				flashHTML += '</object>';

				divTag.innerHTML = flashHTML;

			}

		//--------------------------------------------------
		// Add out flashDetect script to the body

			bodyRef.appendChild(divTag);

	}

//--------------------------------------------------
// When the flash detection object has been
// loaded

	function flashDetectionResult (version) {

		//--------------------------------------------------
		// Remove the flash detection object... it is
		// no longer required. But you cant actually
		// remove it, because IE5 MAC crashes when you
		// try to refresh the page, and you cant do
		// a "display: none" as NS 6.2 crashes on refresh

			var flashDetectObject = document.getElementById('flashDetectObject');
			if (flashDetectObject) {
				flashDetectObject.style.left = '-10px';
			}

		//--------------------------------------------------
		// If this version of flash is good enough,
		// then store the value and continue to process
		// it, otherwise restore the alternative.

			version = parseInt(version);

			if (version >= flashRequired) {
				setFlashAvailable (version);
				flashReplaceProcess();
			} else {
				setFlashAvailable (-2); // HARD DISABLE
				flashRestoreAlt();
			}

	}

//--------------------------------------------------
// The function which will process the replacement
// of the flash objects. This function should ONLY
// be called if we know that the client can do flash

	function flashReplaceProcess () {

		//--------------------------------------------------
		// Go though each of the objects and try to
		// replace them (if they exist yet).

			for (var k=(flashObjects.length - 1); k >= 0; k--) {
				var flashObject = flashObjects[k];
				var objHolder = document.getElementById(flashObject.divId);
				if (objHolder) {

					//--------------------------------------------------
					// Add the flashHolder class to the main div
					// so that it can be targeted by CSS

						cssjs('add', objHolder, 'flashHolder');

					//--------------------------------------------------
					// Remove all the child nodes by sending them
					// off the screen, but add a class to them, so
					// we can bring them back (if nessary).

						var ch = objHolder.childNodes;
						var chLen = ch.length;

						for (i=(chLen-1); i>= 0; i--) {
							if (ch[i].style) {

 								cssjs('add', ch[i], 'flashAlternative');

								ch[i].style.position = 'absolute';
								ch[i].style.left = '-10000px';
								ch[i].style.height = '1px';
								ch[i].style.overflow = 'auto';

 							} else {
 								objHolder.removeChild(ch[i]);
 							}
						}

					//--------------------------------------------------
					// Create the flash object. If this fails the
					// user has a bad browser (IE) and has to be
					// told how to-do things incorrectly

						if (flashUseXmlMethods) {

							//--------------------------------------------------
							// Create nodes

								var objTag = createElement('object');
								var objMovieParam = createElement('param');
								var objBgcolorParam = createElement('param');

							//--------------------------------------------------
							// Setup

								objTag.setAttribute('type','application/x-shockwave-flash');
								objTag.setAttribute('width', flashObject.flashWidth);
								objTag.setAttribute('height', flashObject.flashHeight);
								objTag.setAttribute('data', flashObject.flashUrl);
								objTag.setAttribute('class', 'flashObject');

								objMovieParam.setAttribute('name','movie');
								objMovieParam.setAttribute('value', flashObject.flashUrl);

								if (flashObject.flashBgColour == 'transparent') {
									objBgcolorParam.setAttribute('name','wmode');
									objBgcolorParam.setAttribute('value', 'transparent');
								} else {
									objBgcolorParam.setAttribute('name','bgcolor');
									objBgcolorParam.setAttribute('value', flashObject.flashBgColour);
								}

							//--------------------------------------------------
							// Combine

								objTag.appendChild(objMovieParam);
								objTag.appendChild(objBgcolorParam);
								objHolder.appendChild(objTag);

						} else {

							//--------------------------------------------------
							// Build the html

								var flashHTML = '<object type="application/x-shockwave-flash" data="' + flashObject.flashUrl + '" width="' + flashObject.flashWidth + '" height="' + flashObject.flashHeight + '" class="flashObject">';
								flashHTML += '<param name="movie" value="' + flashObject.flashUrl + '" />';

								if (flashObject.flashBgColour == 'transparent') {
									flashHTML += '<param name="wmode" value="transparent" />';
								} else {
									flashHTML += '<param name="bgcolor" value="' + flashObject.flashBgColour + '" />';
								}

								flashHTML += '</object>';

							//--------------------------------------------------
							// Add it to the holder

								objHolder.innerHTML = flashHTML;

						}

					//--------------------------------------------------
					// Restore visibility to this holder

						objHolder.style.visibility = 'visible';

				}
			}

		//--------------------------------------------------
		// Fix IE5+6 WIN where it will stops animated gifs

			if (flashReloadGifs) {
				var images = document.getElementsByTagName('img');
				for (var i=images.length; i--;){
					if (images[i].src.match(/\.gif/i)) {
						images[i].src = images[i].src;
					}
				}
			}

		//--------------------------------------------------
		// Add the flash enable and disable links

			flashDisableLinks();

	}

//--------------------------------------------------
// Function to restore the flash alternatives
// if we should give up.

	function flashRestoreAlt () {
		for (var k=(flashObjects.length - 1); k >= 0; k--) {
			var objHolder = document.getElementById(flashObjects[k].divId);
			if (objHolder) {

				objHolder.style.visibility = 'visible';
 				objHolder.style.width = 'auto';
 				objHolder.style.height = 'auto';

			}
		}
	}

//--------------------------------------------------
// Function to add the flash on / off links

	function flashDisableLinks () {
		var flashDisableLinks = document.getElementById('flashDisableLinks');
		if (flashDisableLinks) {

			//--------------------------------------------------
			// Give up if the links already exist

				var linkE = document.getElementById('flashEnableLink');
				var linkD = document.getElementById('flashDisableLink');
				if (linkE || linkD) {
					return;
				}

			//--------------------------------------------------
			// Set the enable/disable link urls, while removing
			// any reference to the "flashReplace" variable

				var query = window.location.search;
				query = query.replace(/^\?/, '');
				query = query.replace(/(^|&)flashReplace=[^&]*/gi, '');

				if (query != '') {
					query = '&' + query;
				}

				var enableUrl = './?flashReplace=true' + query;
				var disableUrl = './?flashReplace=false' + query;

			//--------------------------------------------------
			// Create the link elements and nodes

				var enableLink = createElement('a');
				var disableLink = createElement('a');

				var beforeContent = document.createTextNode(flashBeforeLinkText);
				var enableContent = document.createTextNode(flashEnableLinkText);
				var betweenContent = document.createTextNode(flashBetweenLinkText);
				var disableContent = document.createTextNode(flashDisableLinkText);
				var afterContent = document.createTextNode(flashAfterLinkText);

			//--------------------------------------------------
			// Setup the links

				enableLink.setAttribute('href', enableUrl);
				enableLink.setAttribute('id', 'flashEnableLink');
				disableLink.setAttribute('href', disableUrl);
				disableLink.setAttribute('id', 'flashDisableLink');

			//--------------------------------------------------
			// Set one as current - setAttribute() does not
			// work with the class on IE5+6 WIN and IE5 MAC

				if (flashAvailable > 0) {
					cssjs('add', enableLink, 'current');
				} else {
					cssjs('add', disableLink, 'current');
				}

			//--------------------------------------------------
			// Add to the flashDisableLinks holder

				enableLink.appendChild(enableContent);
				disableLink.appendChild(disableContent);

				flashDisableLinks.appendChild(beforeContent);
				flashDisableLinks.appendChild(enableLink);
				flashDisableLinks.appendChild(betweenContent);
				flashDisableLinks.appendChild(disableLink);
				flashDisableLinks.appendChild(afterContent);

		}
	}
