/* 
	swfobject-helpers-core.js
	=================================================================	
	swfobject - core helper functions
	Last Updated 29.04.2009 by DS
	(Latest update fixes several issues found during BLU development).
	-----------------------------------------------------------------
*/

/*	
	@	flash_check_browser_support()
	---------------------------------------------------------------		
	swfobject helper function
	---------------------------------------------------------------				
	
	Cut down version of browserdetect.js, for portability.	
	_______________________________________________________________
*/	

	var flash_browser;
	
	function flash_check_browser_support()
	{
		var ua = navigator.userAgent.toLowerCase(); 
	
		this.isIE = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1)); 		
		this.isNS = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );		
		this.isOpera = (ua.indexOf('opera') != -1);		
		this.isSafari = (ua.indexOf('safari') != - 1);	
		this.isChrome = (ua.indexOf('chrome') != - 1); // Google Chrome
		
		this.isWin = (ua.indexOf('win') != -1);		
		this.isMac = (ua.indexOf('mac') != -1);		
		
		this.versionMinor = parseFloat(navigator.appVersion); 	
		
		if (this.isGecko && !this.isMozilla) 
		{
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
		}
		else if (this.isMozilla) 
		{
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
		}		
		else if (this.isIE && this.versionMinor >= 4) 
		{
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
		}		
		else if (this.isSafari) 
		{
		  this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
		}
		else if (this.isOpera) 
		{
		  this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
		}		
		
		this.versionMajor = parseInt(this.versionMinor); 
		
		/*
			supports_basicSwfobjectEmbed test
		
			From 'Basic embed testing browser support / graceful degradation'...		
			
			- browsers that cannot do the embed
				- XP IE3		
				- XP NS4.7
				- XP Opera 6.04
				
			- browsers that cannot do the embed AND generate errors
				- XP IE4
				- XP Opera 7.20
				- OS X IE5.2						
				- OS X Safari 1						
				
			- browsers that can do the embed.. but only sometimes (unreliable)
				- OS X NS7.2 - note this.versionMajor == 5 not 7.
				- OS X Safari 2.0
				- OS X Safari 2.0.2
				- OS X Safari 2.0.3													
		*/
		
		//	(this.isMac && this.isNS && this.versionMajor == 5) ||		removed as captures mac ff
		
		if (
				(this.isIE && this.versionMajor <= 4) || 		
				(this.isMac && this.isIE && this.versionMajor == 5) ||	
				(this.isNS && this.versionMajor <= 4) || 		
				(this.isOpera && (this.versionMajor <= 7)) || 	
				(this.isSafari && (this.versionMajor < 419))				
				)
		{
			this.supports_basicSwfobjectEmbed = false;		
		}
		else
		{
			this.supports_basicSwfobjectEmbed = true;		
		}	
	}

	flash_browser = new flash_check_browser_support();		
		
/*	
	@	flash_oldBrowserWarning()
	---------------------------------------------------------------		
	swfobject helper function
	- A simple document.write for old browsers.
	
	This may be customised on a per-project basis, but don't forget
	to update the HTML old-browser and noscript warnings to
	match the tone/syntax of this message.
	_______________________________________________________________
*/		

	function flash_oldBrowserWarning(container, el)
	{		
		if (container == 'summary')
		{
			document.write('<' + el + ' class="upgrade"><span class="summary-text">Detecting Flash Plugin..<\/span><\/' + el + '>');
		}
		else if (container == 'detail')
		{			
			document.write('<' + el + '><span class="detail-text">If this message does not disappear after 10 seconds (depending on your connection speed), then it is likely that your browser does not support Flash detection. Please upgrade to a standards compliant browser such as <a href="http://www.firefox.com/">Firefox<\/a> and then refresh this page. <\/span><\/' + el + '>');
		}			
	}			
	
/*	
	@ flash_oldFlashWarning()
	---------------------------------------------------------------			
	swfobject helper function, rewritten for jQuery
	- Change the flash_oldBrowserWarning() to an old-Flash warning.	
	
	Arguments:
	1 x flash_containerId
	OR
	1 x flash_warningContainerId	
	
	This is for newer browsers so it uses correct DOM manipulation
	of elements, rather than el.innerHTML or document.write()
	
	Note: div.warning needs to wrap the warning message	
	
	Called by flash_advancedEmbed() but not called in this project -
	- retained in case warning messages are added in the future.	
	_______________________________________________________________
*/		
	
	function flash_oldFlashWarning(flash_containerId)
	{				
		if ( $('#' + flash_containerId).length > 0 )
		{		
			$('.warning').find('.summary-text')
			.empty()
			.text('Please upgrade your Flash Player');

			$('.warning').find('.detail-text')
			.empty()
			.text('You\'re seeing this notice because you\'re using an old version of Flash Player. Please upgrade to the latest ')
			.append(
				$('<a/>')		
				.attr('href', 'http://get.adobe.com/flashplayer/')
				.text('Flash Player')
			)	
			.append(
				$('<span/>')
				.text(' and then refresh this page. ')		
			);
		}
	}	
	
/*	
	@ flash_removeWarning()
	---------------------------------------------------------------			
	swfobject helper function, rewritten for jQuery	
	- Remove the warning container.			

	Called by flash_advancedEmbed()
	_______________________________________________________________
*/		

	function flash_removeWarning(flash_warningContainerId, optionalParentClass)
	{					
		if (optionalParentClass)
		{
			$('#' + flash_warningContainerId).parents('.' + optionalParentClass).remove();
		}
		else
		{
			$('#' + flash_warningContainerId).remove();			
		}	
	}
	
/*	
	@ getHTTPObject
	---------------------------------------------------------------			
	swfobject helper function	
	- Create HTTP object for Ajax request.
	
	Called by getLastModifiedDate()
	_______________________________________________________________
*/		

	function getHTTPObject() 
	{
		// BPA P50-51
		
		var xhr = false;
		
		if (window.XMLHttpRequest)
		{
			// Firefox, Safari, Opera...
			xhr = new XMLHttpRequest();
		} 
		else if (window.ActiveXObject) 
		{
			try
			{
				// Internet Explorer 5+
				xhr = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch(e)
			{
				try
				{
					xhr = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch(e2)
				{
					xhr = false;
				}
			}		
		} 
		return xhr;
	}
	
/*	
	@ getLastModifiedDate()
	---------------------------------------------------------------			
	swfobject helper function		
	- Create HTTP object for Ajax request.
	
	Called by flash_cacheFix()
	_______________________________________________________________
*/		
	
	function getLastModifiedDate(filename) 
	{
		var request = getHTTPObject();
		
		if (request)
		{		
			request.open('HEAD', filename, false);
			request.send(null);
			return new Date(request.getResponseHeader('Last-Modified'));
		}
	}		
	
/*	
	@	flash_cacheFix(flash_src)
	---------------------------------------------------------------			
	swfobject helper function
	---------------------------------------------------------------				
	Arguments:
	1 x flash_src
	
	Appended a varaiable to the swf src, to fool the browser into
	downloading it, rather than pulling it from the browser cache.
	
	Option 1: Check the Last Modified date/time of the flash_src via Ajax
	Option 2: Generate a random number
	_______________________________________________________________
*/		
	
	function flash_cacheFix(flash_src)
	{
		if (getLastModifiedDate(flash_src))
		{
			var flash_src_last_modified_date = getLastModifiedDate(flash_src).valueOf();
			// test: var flash_src_last_modified_date_human = getLastModifiedDate(flash_src).toLocaleString();		
			// alert(flash_src_last_modified_date);
			return ('?cacheFix=' + flash_src_last_modified_date);		
		}
		else
		{
			var randomNumber = Math.floor(Math.random()*1000000);		
			// alert(randomNumber);			
			return ('?cacheFix=' + randomNumber);		
		}
	}		
	
/*	
	@ flash_createContainer()
	---------------------------------------------------------------			
	swfobject helper function, rewritten for jQuery
	- ADD the Flash to an existing container,
	(the default swfobject behaviour is to REPLACE the container)	
	
	This is achieved by creating a new container for the Flash,
	and inserting that into the existing container.	
	
	Arguments:
	1 x flash_reqdVersion - the version of Flash reqd for THIS embed (reqd)
		  This allows for multiple embeds with different version requirements.
	1 x newContainerId - the id of the new container
	1 x insertBeforeOrAfterTargetEl - 'before' | 'after' | 'replaceContents'
	1 x targetElId_or_targetElParentId - reference node parent ID or reference node ID
	1 x targetElTagName - reference node tag name or false
	1 x targetElTagIndex - reference node index or false	
	
	Updates:
	- 05.02.2009: added 'replaceContents' case to insertBeforeOrAfterTargetEl	
	
	Called in-page.
	_______________________________________________________________
*/	
	
	function flash_createContainer(flash_reqdVersion, newContainerId, insertBeforeOrAfterTargetEl, targetElId_or_targetElParentId, targetElTagName, targetElTagIndex)	
	{			
		// alert( 'swfobject.hasFlashPlayerVersion(' + flash_reqdVersion + ') = ' + swfobject.hasFlashPlayerVersion(flash_reqdVersion) );		
	
		if (swfobject.hasFlashPlayerVersion(flash_reqdVersion)) // don't remove fallback content unless we're about to embed the Flash content
		{			
			if ($('#' + targetElId_or_targetElParentId).length < 1)
			{
				return;
			}				
		
			if (targetElTagName)
			{
				// we want to target a particular element without an ID (within a container with an ID)
				siblingEl = $('#' + targetElId_or_targetElParentId).find(targetElTagName + ':eq(' + targetElTagIndex + ')');
			}
			else
			{
				// we want to target a particular element	with an ID	
				siblingEl = $('#' + targetElId_or_targetElParentId);
			}	
	
			flash_container = $('<div/>')
			.attr('id', newContainerId);
			
			// $("p").before("<b>Hello</b>");
	
			switch (insertBeforeOrAfterTargetEl)
			{
				case 'before' :							
					$(siblingEl).before($(flash_container)); // insert flash_container before siblingEl				
					break;
					
				case 'after' :
					$(siblingEl).after($(flash_container)); // insert flash_container after siblingEl
					break;					
					
				case 'replaceContents' :		
					$(siblingEl)
					.empty()
					.append(flash_container);
					break;							
			}
	
			return newContainerId; // return ID so it can be assigned to the calling variable
		}
	}	
	
/*	
	@	flash_advancedEmbed()
	---------------------------------------------------------------			
	swfobject helper function	
	- Advanced embed, for better control of warnings and alternative content
	
	Notes:
	"if a warning exists, remove the warning": 
	if flash_warningContainer is nested within flash_container, 
	it will automatically be replaced by the Flash movie 
	when that is inserted.
	However sometimes the warning is separate from the flash_container, 
	so by calling this function we cover both options
	
	this function is only run if the flash_warningContainerId (still) exists, 
	so if the act of inserting a Flash movie deletes 
	the warning container, an error will not be generated. 	
	
	Called in-page or via init (or similar).	
	_______________________________________________________________
*/		

	function flash_advancedEmbed(flash_src, flash_containerId, flash_width, flash_height, flash_reqdVersion, flash_expressInstaller, flash_vars, flash_params, flash_attributes, flash_custom_args)
	{		
		if (flash_browser.supports_basicSwfobjectEmbed)
		{		
			switch (swfobject.hasFlashPlayerVersion(flash_reqdVersion))				
			{ 
				// Flash Player version is available:					
				case true : 	
					// if a Flash movie has been specified, insert the Flash movie
					if (flash_src)
					{
						swfobject.embedSWF((flash_src + flash_cacheFix(flash_src)), flash_containerId, flash_width, flash_height, flash_reqdVersion, flash_expressInstaller, flash_vars, flash_params, flash_attributes);										
					}						
					// if a warning exists, remove the warning
					if (flash_custom_args.flash_warningContainerId) 
					{ 
						flash_removeWarning(flash_custom_args.flash_warningContainerId); 						
					}		
					
					break;				
				
				// Flash Player version is not available:
				case false :	

					// if a Flash movie has been specified, and expressInstall is enabled, output the expressInstaller so the user can upgrade to the latest version of Flash
					if (flash_src && flash_expressInstaller)
					{
						swfobject.embedSWF((flash_src + flash_cacheFix(flash_src)), flash_containerId, flash_width, flash_height, flash_reqdVersion, flash_expressInstaller, flash_vars, flash_params, flash_attributes);	
					}
					// if a warning exists, change the old-browser warning to an old-Flash warning							
					if (flash_custom_args.flash_warningContainerId)
					{
						flash_oldFlashWarning(flash_custom_args.flash_warningContainerId); 	
					}															
					break; 				
			}
		}
		return swfobject.hasFlashPlayerVersion(flash_reqdVersion);
	}
	
/*	
	@	flash_getPlayerVersion()
	---------------------------------------------------------------			
	swfobject helper function (wrapper)
	---------------------------------------------------------------		
	_______________________________________________________________
*/	

	function flash_getPlayerVersion()
	{
		var playerVersion = swfobject.getFlashPlayerVersion(); // returns a JavaScript object
		
		if (playerVersion)
		{		
			return(playerVersion.major + "." + playerVersion.minor + "." + playerVersion.release);
		}
	}
	

	function eiObject()
	{
		if (navigator.appName.indexOf("Microsoft") != -1) 
		{
			//flash_eiObject = window.flash_containerId; // IE
		} 
		else 
		{
			//flash_eiObject = window.document.flash_containerId; // mozilla
		}				
	}
