/* 
	swfobject-scenarios-advanced.js
	=================================================================	
	swfobject scenarios
	-----------------------------------------------------------------
*/

/*	
	@	flash_browser()
	---------------------------------------------------------------		
	swfobject helper function
	---------------------------------------------------------------				
	
	Cut down version of browserdetect.js, for portability.	
	_______________________________________________________________
*/	

	var flash_browser;
	
	function flash_browser()
	{
		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 popup tweaks
		*/
		
		if (this.isSafari && !this.isChrome && (this.versionMajor > 522))
		{
			this.popUpTopUp = 4;		
		}		
		else if (this.isSafari && (this.versionMajor <= 419))
		{
			this.popUpTopUp = 10;		
		}
	}

	flash_browser = new flash_browser();		
	
	// test: alert('ua = ' + navigator.userAgent.toLowerCase() + '\nflash_browser.supports_basicSwfobjectEmbed = ' + flash_browser.supports_basicSwfobjectEmbed);
		
/*	
	@	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="loading"><span class="summary-text">Please wait, Flash Player is loading...<\/span><\/' + el + '>');
		}
		else if (container == 'detail')
		{			
			document.write('<' + el + '><span class="detail-text">If the Flash Player does not appear then your browser may not be supported. Please upgrade to a new browser and try again. For instructions on upgrading your browser visit our <a href="http://www.educate.ece.govt.nz/Programmes/AboutThisSite/TechnicalHelp.aspx">Technical help page</a>. Alternatively, you can download this video in Windows Media Player format by clicking on the <a href="#av-download">Download Video link</a>. <\/span><\/' + el + '>');
		}			
	}				
	
/*	
	@	flash_oldFlashWarning()
	---------------------------------------------------------------			
	swfobject helper function
	---------------------------------------------------------------		
	
	Arguments:
	1 x flash_containerId
	OR
	1 x flash_warningContainerId
	
	Change the flash_oldBrowserWarning() to an old-Flash warning.	
	
	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
	_______________________________________________________________
*/		

	function flash_oldFlashWarning(flash_containerId)
	{				
		if (!document.getElementById(flash_containerId))
		{
			return;
		}	
		
		// warning summary
		warning_summary = document.createElement('span');
		warning_summary.className = 'summary-text';			
		warning_summary_text = document.createTextNode('Please upgrade your Flash Player');
		
		warning_summary.appendChild(warning_summary_text);				
				
		// warning detail						
		warning_detail = document.createElement('span');
		warning_detail.className = 'detail-text';			
		
		warning_detail_node_1 = document.createTextNode('You appear to be using an older version of Flash Player. Please ');
		
		warning_detail_node_2 = document.createElement('a');	
		warning_detail_node_2.setAttribute('href', 'http://get.adobe.com/flashplayer/');			
		warning_detail_node_2a = document.createTextNode('upgrade to the latest Flash Player');
		warning_detail_node_2.appendChild(warning_detail_node_2a);
		
		warning_detail_node_3 = document.createTextNode(' and try again. For instructions on upgrading your Flash Player visit our ');	
		
		warning_detail_node_4 = document.createElement('a');
		warning_detail_node_4.setAttribute('href', 'http://www.educate.ece.govt.nz/Programmes/AboutThisSite/TechnicalHelp.aspx');		
		warning_detail_node_4a = document.createTextNode('Technical help page');
		warning_detail_node_4.appendChild(warning_detail_node_4a);		
		
		warning_detail_node_5 = document.createTextNode('. Alternatively, you can download this video in Windows Media Player format by clicking on the ');
		
		warning_detail_node_6 = document.createElement('a');
		warning_detail_node_6.setAttribute('href', '#av-download');		
		warning_detail_node_6a = document.createTextNode('Download Video link');		
		warning_detail_node_6.appendChild(warning_detail_node_6a);		
		
		warning_detail_node_7 = document.createTextNode('. ');																							

		warning_detail.appendChild(warning_detail_node_1);
		warning_detail.appendChild(warning_detail_node_2);	
		warning_detail.appendChild(warning_detail_node_3);		
		warning_detail.appendChild(warning_detail_node_4);			
		warning_detail.appendChild(warning_detail_node_5);			
		warning_detail.appendChild(warning_detail_node_6);			
		warning_detail.appendChild(warning_detail_node_7);			
								
		// check that there is in fact a warning to upgrade				
		var divs = document.getElementById(flash_containerId).getElementsByTagName('div');			
		
		for (var i=0; i<divs.length; i++)
		{				
			if (divs[i].className == 'warning')
			{
				// search thru all warning elements and replace the ones that match						
				var els = document.getElementById(flash_containerId).getElementsByTagName('*'); // BPA p39				
				
				for (var i=0; i<els.length; i++)
				{			
					if (els[i].className == 'summary-text')
					{
						els[i].parentNode.replaceChild(warning_summary, els[i]);
						els[i].parentNode.className = 'upgrade'; // replace loading icon 
					}				
					else if (els[i].className == 'detail-text')
					{
						els[i].parentNode.replaceChild(warning_detail, els[i]);
					}					
				}
			}	
		}				
	}		
	
/*	
	@	flash_removeWarning()
	---------------------------------------------------------------			
	swfobject helper function
	---------------------------------------------------------------				
	
	Arguments:
	1 x flash_warningContainerId		
	
	Remove the warning container.		
	_______________________________________________________________
*/		

	function flash_removeWarning(flash_warningContainerId)
	{
		if (!document.getElementById(flash_warningContainerId))
		{
			return;
		}
		
		flash_warningContainer = document.getElementById(flash_warningContainerId);		
		flash_warningContainer.parentNode.removeChild(flash_warningContainer);
	}
	
/*	
	@	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);		
		}
	}		
	
/*	
	@ getHTTPObject
	@ getLastModifiedDate
	@	insertAfter
	@ getQuerystringVarValue	
	@ preventDefault
	---------------------------------------------------------------			
	general helper functions
	_______________________________________________________________
*/		

	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(e)
				{
					xhr = false;
				}
			}		
		} 
		return xhr;
	}
	
	function getLastModifiedDate(filename) 
	{
		var request = getHTTPObject();
		
		if (request)
		{		
			request.open('HEAD', filename, false);
			request.send(null);
			return new Date(request.getResponseHeader('Last-Modified'));
		}
	}		
	
	function insertAfter(parent, newNode, referenceNode) 
	{
		parent.insertBefore(newNode, referenceNode.nextSibling);
	}
	
	function getQuerystringVarValue(var_name) 
	{
		// for passing vars in the querystring
		
		var query = window.location.search.substring(1);
		var vars = query.split("&");
		for (var i=0;i<vars.length;i++) 
		{
			var pair = vars[i].split("=");
			if (pair[0] == var_name) 
			{
				return pair[1];
			}
		} 
		return false;
	}			
	
	function preventDefault(e) 
	{ 
	 e=e||event; 
	 e.preventDefault? e.preventDefault() : e.returnValue = false; 
	}	
	
/*	
	@	add_event()
	@	remove_event()	
	---------------------------------------------------------------			
	general helper functions
	These are also often included in common.js
	---------------------------------------------------------------				

	Examples:
	add_event(myEl, 'click', function_name, false);		
	add_event(myEl, 'mouseover', function_name, false);		
	add_event(myEl, 'mouseout', function_name, false);	
	add_event(myEl, 'focus', function_name, false);		
	add_event(myEl, 'blur', function_name, false);		
	_______________________________________________________________
*/		
	
	function add_event(obj, evType, fn, useCapture)
	{
		// http://www.scottandrew.com/weblog/articles/cbs-events
		
		if (obj.addEventListener)
		{
			obj.addEventListener(evType, fn, useCapture);
			return true;
		} 
		else if (obj.attachEvent)
		{
			var r = obj.attachEvent('on' + evType, fn);
			return r;
		} 
		else 
		{
			alert("Handler could not be attached");
		}
	}	
	
	function remove_event(obj, evType, fn, useCapture)
	{
		// http://www.scottandrew.com/weblog/articles/cbs-events		
		
		if (obj.removeEventListener)
		{
			obj.removeEventListener(evType, fn, useCapture);
			return true;
		} 
		else if (obj.detachEvent)
		{
			var r = obj.detachEvent('on' + evType, fn);
			return r;
		} 
		else 
		{
			alert("Handler could not be removed");
		}
	}		
	
/*	
	@	flash_createContainer()
	---------------------------------------------------------------			
	swfobject helper function
	---------------------------------------------------------------				
	
	Arguments:
	1 x newId - the id of the new container
	1 x insertBeforeOrAfterTargetEl - 'before' or 'after'
	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	
	
	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.
	_______________________________________________________________
*/	
	
	function flash_createContainer(newContainerId, insertBeforeOrAfterTargetEl, targetElId_or_targetElParentId, targetElTagName, targetElTagIndex)
	{		
		if (!document.getElementById(targetElId_or_targetElParentId))
		{
			return;
		}				
	
		if (targetElTagName)
		{
			// we want to target a particular element without an ID (within a container with an ID)
			if (!document.getElementById(targetElId_or_targetElParentId).getElementsByTagName(targetElTagName)[targetElTagIndex])
			{
				return;
			}
			else
			{
				siblingEl = document.getElementById(targetElId_or_targetElParentId).getElementsByTagName(targetElTagName)[targetElTagIndex];
			}
		}
		else
		{
			// we want to target a particular element	with an ID	
			siblingEl = document.getElementById(targetElId_or_targetElParentId);
		}		

		flash_container = document.createElement('div');
		flash_container.setAttribute('id', newContainerId);
		
		switch (insertBeforeOrAfterTargetEl)
		{
			case 'before' :
				siblingEl.parentNode.insertBefore(flash_container, siblingEl);
				break;
				
			case 'after' :
				insertAfter(siblingEl.parentNode, flash_container, siblingEl);
				break;																
		}

		return newContainerId; // return ID so it can be assigned to the calling variable
	}	
	
/*	
	@	flash_htmlLinkToFlashPopUp()
	---------------------------------------------------------------			
	swfobject helper function (demo)
	---------------------------------------------------------------				
	
	Arguments:
	1 x containerId - the id of the container to manipulate
	
	User definable function, to do something other than	a Flash embed, 
	if Flash is available.
	_______________________________________________________________
*/		

	function flash_htmlLinkToFlashPopUp(flash_containerId, flash_width, flash_height, flash_pageUrl, flash_linkText)
	{
		// editable
		
		// this example changes a regular link to an HTML page,
		// to a popUp containing a Flash Page
		
		var a = document.getElementById(flash_containerId).getElementsByTagName('a')[0];			
		
		// get first text node
		for (var n=0; n<a.childNodes.length; n++)
		{
			if (a.childNodes[n].nodeType == 3)
			{
				a_old_text = a.childNodes[n];
				break;
			}
		}

		if (a_old_text)
		{			
			a_new_text = document.createTextNode(flash_linkText);
			a.replaceChild(a_new_text, a_old_text);		
			a.setAttribute('href', flash_pageUrl);					
			add_event(a, 'click', new Function("flash_popUp('" + flash_pageUrl + "','" + flash_width + "','" + flash_height + "')"), false);
			add_event(a, 'click', preventDefault, false);		
		}
	}	
	
/*	
	@	flash_popUp()
	---------------------------------------------------------------			
	general helper function
	---------------------------------------------------------------				
	
	Arguments:
	1 x flash_pageUrl
	1 x flash_width (flash width)
	1 x flash_height (flash height)
	_______________________________________________________________
*/		
	
	flash_popUpWin = null;

	function flash_popUp(flash_pageUrl, flash_width, flash_height)
	{		
		scrollBars = false; // (true if a text-based closer used, otherwise false)
	
		day = new Date();
		winName = ('pop' + day.getTime());			
		
		if (flash_browser.popUpTopUp)
		{
			flash_width = parseInt(flash_width) + flash_browser.popUpTopUp;
			flash_height = parseInt(flash_height) + flash_browser.popUpTopUp;					
		}				

		var scrollBarsString = '';
		
		if (scrollBars)
		{
			scrollBarsString = ',scrollbars=yes';
		}
		
		var str = ("height=" + flash_height + ",innerHeight=" + flash_height + ",width=" + flash_width + ",innerWidth=" + flash_width + scrollBarsString);
		 
		// close and create a new window EVERY time

		if (flash_popUpWin != null && !flash_popUpWin.closed)
		{
			flash_popUpWin.close();
		}		

		flash_popUpWin = window.open(flash_pageUrl, winName, str);;
		flash_popUpWin.focus();	
		
		return flash_popUpWin;
	}		
	
/*	
	@	flash_basicEmbed()
	---------------------------------------------------------------			
	swfobject helper function	
	---------------------------------------------------------------	
	Basic embed, for greater browser support	
	_______________________________________________________________
*/		

	function flash_basicEmbed(flash_src, flash_containerId, flash_width, flash_height, flash_reqdVersion, flash_expressInstaller, flash_vars, flash_params, flash_attributes)
	{	
		if (flash_browser.supports_basicSwfobjectEmbed)
		{
			swfobject.embedSWF((flash_src + flash_cacheFix(flash_src)), flash_containerId, flash_width, flash_height, flash_reqdVersion, flash_expressInstaller, flash_vars, flash_params, flash_attributes);	
		}
	}
	
/*	
	@	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. 	
	_______________________________________________________________
*/		

	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
					// -- removed as buggy in ie7. This call is redundant anyway, as the Flash embed replaces the warning message (on this project).
					
					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; 				
			}
		}
	}
	
/*	
	@	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);
		}
	}
	
/*	
	@	flash_exit()
	@	flash_exit_watch()
	---------------------------------------------------------------			
	swfobject helper functions
	---------------------------------------------------------------		
	Detect mouse-exit of embed container, to hide Flash volume slider.
	
	Originally by AK, simplifed and updated by DS to work with swfobject.

	Update 14.01.2009
	This doesn't actually work correcly yet - 
	see http://nfa.2691/view-video-new-static-on.htm
	_______________________________________________________________
*/		
	
	function flash_exit_watch(flash_id) 
	{ 				
		// apply listener to Flash container
		add_event(document.getElementById(flash_id).parentNode, 'mouseout', flash_exit, false); 
	}
	function flash_exit() 
	{ 
		// run Flash AS	
		document.getElementById(window.flash_containerId).divRollOut;
	}			
	
/*	
	@	flash_customEmbed_1()
	---------------------------------------------------------------			
	swfobject helper function
	---------------------------------------------------------------		
	Custom embed - customised scenario
	Note: This is editable/clonable/renameable and can also 
	be used inline in your init() function.
	_______________________________________________________________
*/	

	function flash_customEmbed_1(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 : 	
					// in this custom scenario we are not outputting a flash movie, rather changing a link to an HTML page, to point to a popup containing a static Flash embed
					flash_htmlLinkToFlashPopUp(flash_containerId, flash_width, flash_height, flash_custom_args.flash_pageUrl, flash_custom_args.flash_linkText);
						
					// 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; 				
			} 
		}
	}		