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

//--------------------------------------------------
// Ensure that we know what the minimum version
// of flash is set (don't over-ride previous
// values), and that this value is not less than
// what is required for the flash file verification

	if (!document.flashRequired || document.flashRequired < 5) {
		document.flashRequired = 6;
	}

//--------------------------------------------------
// Location of the flashDetect file

	document.flashDetectObjectUrl = '/a/swf/flashDetect.swf';

//--------------------------------------------------
// There may be an additional script which adds
// javascript enable / disable buttons. These
// should only be present if we do any manipulation
// on the document... at this time we haven't done
// any

	document.flashDocumentChangesMade = false;

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

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

//--------------------------------------------------
// Initial value of "document.flashAvailable"

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

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

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

	//--------------------------------------------------
	// Check to see if we should be disabling
	// javascript

		var locationString = window.location.search;

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

			setFlashAvailable (-1); // SOFT DISABLE

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

			if (document.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 (document.flashAvailable == 0) {

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

		}

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

	document.flashUseXmlMethods = true;

	document.flashObjectIds = new Array();
	document.flashObjectUrls = new Array();
	document.flashObjectWidths = new Array();
	document.flashObjectHeights = new Array();
	document.flashObjectBgs = new Array();
	document.flashObjectsDone = new Array();

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

		//--------------------------------------------------
		// If the client does not want flash or
		// a cookie says they don't have a good enough
		// version, then give up now. But if this is a
		// soft disable, then say we made changes, so
		// the enable/disable links can be created

			if (document.flashAvailable < 0 || (document.flashAvailable > 0 && document.flashAvailable < document.flashRequired)) {
				document.flashDocumentChangesMade = (document.flashAvailable == -1);
				return false;
			}

		//--------------------------------------------------
		// We are about to make (damaging) changes to
		// the website.

			document.flashDocumentChangesMade = true;

		//--------------------------------------------------
		// Temporarily hide the flash object - first
		// try standards compliant, then do one for
		// the lesser browsers (IE)

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

			if (document.createElementNS) {
				var styleElement = document.createElementNS('http://www.w3.org/1999/xhtml', 'style');
				var styleContent = document.createTextNode(cssRule);
			} else {
				var styleElement = document.createElement('style');
				var styleContent = document.createTextNode(cssRule);
			}

			try {

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

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

				if (document.frames) {
					a++; // This ones for you IE5 Mac (you need an error)
				}

			} catch (e) {

				document.flashUseXmlMethods = false;

				styleElement = null; // Help IE5.5 + IE6 which keeps the globe spinning (loading...?)
				styleContent = null;

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

			}

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

			var k = document.flashObjectIds.length;

			document.flashObjectIds[k] = divId;
			document.flashObjectUrls[k] = flashUrl;
			document.flashObjectWidths[k] = flashWidth;
			document.flashObjectHeights[k] = flashHeight;
			document.flashObjectBgs[k] = flashBgColour;

			document.flashObjectsDone[k] = false;

		//--------------------------------------------------
		// 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 (k == 0) {
				if (document.flashAvailable == 0) {
					addLoadEvent (flashDetectObjectAdd);
				} else {
					addLoadEvent (flashReplaceProcess);
				}
			}

		//--------------------------------------------------
		// Everything went well

			return true;

	}

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

		//--------------------------------------------------
		// Try to get a reference to the body

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

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

					if (document.createElementNS) {
						var divTag = document.createElementNS('http://www.w3.org/1999/xhtml', 'div');
					} else {
						var divTag = document.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 method.

					if (document.flashUseXmlMethods) {

						if (document.createElementNS) {
							var objTag = document.createElementNS('http://www.w3.org/1999/xhtml', 'object');
							var objMovieParam = document.createElementNS('http://www.w3.org/1999/xhtml', 'param');
						} else {
							var objTag = document.createElement('object');
							var objMovieParam = document.createElement('param');
						}

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

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

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

					} else {

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

						divTag.innerHTML = flashHTML;

					}

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

					bodyRef[0].appendChild(divTag);

			} else {

				//--------------------------------------------------
				// Could not get a reference to the body, so
				// give up

					flashRestoreAlt();

			}

	}

//--------------------------------------------------
// 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');
			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 >= document.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 support flash

	function flashReplaceProcess () {

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

			var flashLength = document.flashObjectIds.length;
			for (var k = 0; k < flashLength; k++) {
				var objHolder = document.getElementById(document.flashObjectIds[k]);
				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';

							} 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 (document.flashUseXmlMethods) {

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

								if (document.createElementNS) {
									var objTag = document.createElementNS('http://www.w3.org/1999/xhtml', 'object');
									var objMovieParam = document.createElementNS('http://www.w3.org/1999/xhtml', 'param');
									var objBgcolorParam = document.createElementNS('http://www.w3.org/1999/xhtml', 'param');
								} else {
									var objTag = document.createElement('object');
									var objMovieParam = document.createElement('param');
									var objBgcolorParam = document.createElement('param');
								}

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

								objTag.setAttribute('type','application/x-shockwave-flash');
								objTag.setAttribute('width', document.flashObjectWidths[k]);
								objTag.setAttribute('height', document.flashObjectHeights[k]);
								objTag.setAttribute('data', document.flashObjectUrls[k]);
								objTag.setAttribute('class', 'flashObject');

								objMovieParam.setAttribute('name','movie');
								objMovieParam.setAttribute('value', document.flashObjectUrls[k]);

								if (document.flashObjectBgs[k] == 'transparent') {
									objBgcolorParam.setAttribute('name','wmode');
									objBgcolorParam.setAttribute('value', 'transparent');
								} else {
									objBgcolorParam.setAttribute('name','bgcolor');
									objBgcolorParam.setAttribute('value', document.flashObjectBgs[k]);
								}

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

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

						} else {

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

								flashHTML  = '<object type="application/x-shockwave-flash" data="' + document.flashObjectUrls[k] + '" width="' + document.flashObjectWidths[k] + '" height="' + document.flashObjectHeights[k] + '" class="flashObject">';
								flashHTML += '	<param name="movie" value="' + document.flashObjectUrls[k] + '" />';

								if (document.flashObjectBgs[k] == 'transparent') {
									flashHTML += '<param name="wmode" value="transparent" />';
								} else {
									flashHTML += '<param name="bgcolor" value="' + document.flashObjectBgs[k] + '" />';
								}

								flashHTML += '</object>';

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

								objHolder.innerHTML = flashHTML;

							//--------------------------------------------------
							// Fix problem on IE5+6 WIN, where it will stop
							// animated gifs - reloads the images once flash
							// has been replaced.

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

						}

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

						objHolder.style.visibility = 'visible';

					//--------------------------------------------------
					// Remember that this flash object has been
					// replaced

						document.flashObjectsDone[k] = true;

				}
			}

	}

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

	function flashRestoreAlt () {

		//--------------------------------------------------
		// Go though each of the flash objects, which
		// have NOT been replaced, to restore them to
		// their former glory.

			var flashLength = document.flashObjectIds.length;
			for (k=0; k<flashLength; k++) {
				if (document.flashObjectsDone[k] != true) {

					//--------------------------------------------------
					// Try to get a reference to this object and
					// restore

						var objHolder = document.getElementById(document.flashObjectIds[k]);
						if (objHolder) {
							objHolder.style.visibility = 'visible';
							objHolder.style.width = 'auto';
							objHolder.style.height = 'auto';
							objHolder.style.backgroundColor = 'inherit';
							document.flashObjectsDone[k] = true;
						}

				}
			}

		//--------------------------------------------------
		// Try to find the enable/disable links and
		// update their classes

			enableLink = document.getElementById('flashEnableLink');
			disableLink = document.getElementById('flashDisableLink');

			if (enableLink && disableLink) {
				enableLink.setAttribute('class', '');
				disableLink.setAttribute('class', 'current');
			}

	}
