/* 
-----------------------------------------------------------------
	biz2328
	flash.js
	
	Flast detection, generation, and insertion
	
	Created 02.10.2007 by DS
	Last Updated: See SVN		
-----------------------------------------------------------------
*/

//	VARIABLES

	var global_flash_version = 0;
	var global_flash_available = false;
	var global_flash_version_DONTKNOW = -1;	// used by detect	

	function generate_unique_page_id()
	{
		/* 	
			A unique id that is sent to all Flash movies on the page
			so that Flash movies can talk to each other without referencing
			Flash movies open in other windows.
		*/
		
		var ranNum= Math.floor(Math.random()*1000);
		return ranNum;	
	}

//	FUNCTIONS

	// Flash Version Detector  v1.2.1
	// documentation: http://www.dithered.com/javascript/flash_detect/index.html
	// license: http://creativecommons.org/licenses/by/1.0/
	// code by Chris Nott (chris[at]dithered[dot]com)
	// with VBScript code from Alastair Hamilton (now somewhat modified)
	
	function isDefined(property) 
	{
	  return (typeof property != 'undefined');
	}
	function detect_flash_version() 
	{	  
	   var agent = navigator.userAgent.toLowerCase(); 
	   
	   // NS3 needs global_flash_version to be a local variable
	   if (agent.indexOf("mozilla/3") != -1 && agent.indexOf("msie") == -1) {
		  global_flash_version = 0;
	   }
	   
	   // NS3+, Opera3+, IE5+ Mac (support plugin array):  check for Flash plugin in plugin array
	   if (navigator.plugins != null && navigator.plugins.length > 0) {
		  var flashPlugin = navigator.plugins['Shockwave Flash'];
		  
		  if (typeof flashPlugin == 'object') { 
			 for (var i = global_latest_flash_version; i >= 3; i--) 
			 {
				if (flashPlugin.description.indexOf(i + '.') != -1) 
				{
				   global_flash_version = i;
				   break;
				}
			 }
		  }
	   }
	   
	   
	   
	   // IE4+ Win32:  attempt to create an ActiveX object using VBScript
	   // Note: Flash detection run during page load, so that IE can output VB Script to page
	   // 280806 doc += 'Dim i \n'; added as otherwise i set to 9 (number of imported javascripts specified in master script
	   else if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && agent.indexOf("win")!=-1 && agent.indexOf("16bit")==-1) {
		  var doc = '<scr' + 'ipt language="VBScript"\> \n';
		  doc += 'On Error Resume Next \n';
		  doc += 'Dim obFlash \n';
		  doc += 'Dim i \n';
		  doc += 'For i = ' + global_latest_flash_version + ' To 3 Step -1 \n';
		  doc += '   Set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash." & i) \n';
		  doc += '   If IsObject(obFlash) Then \n';
		  doc += '      global_flash_version = i \n';
		  doc += '      Exit For \n';
		  doc += '   End If \n';
		  doc += 'Next \n';
		  doc += '</scr' + 'ipt\> \n';
		  document.write(doc);
	   }
		  
	   // WebTV 2.5 supports flash 3
	   else if (agent.indexOf("webtv/2.5") != -1) global_flash_version = 3;
	
	   // older WebTV supports flash 2
	   else if (agent.indexOf("webtv") != -1) global_flash_version = 2;
	
	   // Can't detect in all other cases
	   else {
		  global_flash_version = global_flash_version_DONTKNOW;
	   }
	   
	   return global_flash_version;
	}
	
	function show_html()
	{
		var div_body = document.getElementById('body');
		div_body.className = 'show_html';
	}	
	
	
	
/*	## Generate Flash
	Function called on init
	**************************************** */
	
	function generateFlash(flashContainerID, flashDir, flashFileName, flashWidth, flashHeight, flashVars, markupBefore, markupAfter)
	{		
		// massively customised, tidied up, and commented (but still not fully yet), to match LB's demo page script/effect (which was generated by Flash/Flex Builder - it uses the AC_FL_RunContent() function).
		// Main reference: http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_12701
		// allowFullScreen: http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00000352.html
		// AllowScriptAccess: http://kb.adobe.com/selfservice/viewContent.do?externalId=tn_16494
		// flash.scale: http://www.actionscripts.co.uk/tutorials/poab/poab04/stage.html	
		
		var flash = new Object();
			flash.containerId = flashContainerID;
			flash.reqdVersion = global_required_flash_version;			
			flash.src = flashFileName;
			flash.width = flashWidth;
				// px
				// %			
			flash.height = flashHeight;
				// px
				// %
				
/*	DS Replaced with CSS Fix - flash.css

					// a height setting of 100% only works in Firefox if the doctype is dropped..
					if ((flash.height == '100%') && !((whichbrowser.isWin) && (whichbrowser.isIE)))
					{				
						vpHeight = getViewportDimension('h');
						
						flash.height = vpHeight;
						
						if (whichbrowser.isSafari)
						{
							flash.height = vpHeight - 2; // to remove scrollbar in Safari, leaves 2px whitespace
						}
					}			
					
*/					
					
			flash.vars = flashVars;
			flash.htmlBefore = markupBefore;
			flash.htmlAfter = markupAfter;
			flash.quality = 'high'; 
				// low
				// high
				// autolow
				// autohigh
				// best
			flash.loop = 'false'; 
				// false
				// true (default)
			flash.bgcolor = global_flash_bgcolor;
			flash.menu = 'true'; 
				// false
				// true (probably default)
			flash.scale = 'noScale'; 
				// showall (default)
					// scales the movie's contents without distortion. 
					// So the smaller of the browser's width and height is the size of the square. 
					// This is the exact opposite of noBorder, and you won't crop anything.	
				// noBorder: 
					// Scales the movie's contents to the maximum available. 
					// If the width of the browser window is larger than the height then the square will have the width and the height equal to the browser window's width. 
					// This option almost always crops some of your movie.				
				// exactFit: 
					// Distorts the movie's contents to fit the browser window.			
				// noScale - not sure when this was added as an option
					// Doesn't scale the movie's contents at all, even when the stage itself is resized.			
			flash.align = 'middle';
			flash.salign = 'lt';
			flash.allowFullScreen = 'true';	
				// false (default). 			
				// true
					// Flash Player 9.0.27.0 and later support full-screen mode, in which Flash content can fill the entire screen.			
			flash.allowScriptAccess = 'sameDomain';	
				// always (default). Controls whether SWFs may call out to JavaScript in HTML pages.
				// sameDomain (default for Flash 8+)
				// never
			flash.name = 'loader';	
			flash.devicefont = 'false';	
			flash.wmode = 'window';	
			flash.getFlashUrl = 'http://www.adobe.com/products/flashplayer/';
			
			flash.noflashMsg = '<div id="shell">\n';
			flash.noflashMsg += '  <div id="header">\n';
			flash.noflashMsg += '    <h1><img src="/resources/ui/images/bizmo-logo.gif" alt="Bizmo&reg;" width="211" height="55"></h1>\n';
			flash.noflashMsg += '    <p><img src="/resources/ui/images/bizmo-byline.gif" alt="New Zealand Business Monitor. " width="191" height="7"></p>\n';
			flash.noflashMsg += '  </div>\n';
			flash.noflashMsg += '  <hr>\n';
			flash.noflashMsg += '  <div id="body">\n';
			flash.noflashMsg += '    <div id="main">\n';				
			flash.noflashMsg += '     <h2>Welcome to Bizmo<span class="reg">&reg;</span></h2>\n';
			flash.noflashMsg += '	  <p class="error">You don\'t appear to have Flash ' + flash.reqdVersion + ' installed, but you may view our <a href="home.htm">HTML version</a>.</p>\n';
			flash.noflashMsg += '	  <p class="error">Alternatively, please <a href="' + flash.getFlashUrl + '">download and install the latest version of Flash</a>, then refresh this page. </p>\n';
			flash.noflashMsg += '	</div>\n';
			flash.noflashMsg += '  </div>\n'
			flash.noflashMsg += '</div>\n';
			
			flash.base = flashDir;			
				// Specifies the base directory or URL used to resolve all relative path statements in the Flash Player movie. 
				// This attribute is helpful when your Flash Player movies are kept in a different directory from your other (HTML) files.	

			flash.html =  ('\n<!-- START GENERATED MARKUP -->\n');
		
			if (flash.htmlBefore != '')
			{	
				flash.html += (flash.htmlBefore + '\n');
			}	
		
			if (global_flash_available)
			{			
				flash.html += ('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" '); // Reqd - OBJECT tag only
				flash.html += ('codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" '); // Reqd - OBJECT tag only
				flash.html += ('width="' + flash.width + '" '); // Reqd - OBJECT and EMBED tags
				flash.html += ('height="' + flash.height + '">\n'); // Reqd - OBJECT and EMBED tags
				flash.html += ('<param name="movie" value="' + flash.base + flash.src + '">\n'); // Reqd - OBJECT tag only
				flash.html += ('<param name="quality" value="' + flash.quality + '">\n'); // Optional - OBJECT and EMBED tags
				flash.html += ('<param name="loop" value="' + flash.loop + '">\n'); // Optional - OBJECT and EMBED tags
				flash.html += ('<param name="bgcolor" value="' + flash.bgcolor + '">\n'); // Optional - OBJECT and EMBED tags			
				flash.html += ('<param name="menu" value="' + flash.menu + '">\n'); // Optional - OBJECT and EMBED tags
				flash.html += ('<param name="scale" value="' + flash.scale + '">\n'); // Optional - OBJECT and EMBED tags
				flash.html += ('<param name="align" value="' + flash.align + '">\n'); // Optional - OBJECT and EMBED tags
				flash.html += ('<param name="salign" value="' + flash.salign + '">\n');	// Optional - OBJECT and EMBED tags			
				flash.html += ('<param name="allowFullScreen" value="' + flash.allowFullScreen + '">\n'); // Flash 9+
				flash.html += ('<param name="allowScriptAccess" value="' + flash.allowScriptAccess + '">\n'); // 
				flash.html += ('<param name="devicefont" value="' + flash.devicefont + '">\n');	// 
				flash.html += ('<param name="wmode" value="' + flash.wmode + '">\n'); // Optional - OBJECT and EMBED tags	
				flash.html += ('<param name="base" value="' + flash.base + '">\n'); // Optional - OBJECT and EMBED tags					
				
				if (flash.vars != '')
				{
					flash.html += ('<param name="flashvars" value="' + flash.vars + '">\n'); // Optional - OBJECT and EMBED tags
				}
				
				flash.html += ('<embed src="' + flash.base + flash.src + '" '); // Reqd - EMBED tag only
				flash.html += ('width="' + flash.width + '" '); // Reqd - OBJECT and EMBED tags
				flash.html += ('height="' + flash.height + '" '); // Reqd - OBJECT and EMBED tags
				flash.html += ('quality="' + flash.quality + '" '); // Optional - OBJECT and EMBED tags
				flash.html += ('loop="' + flash.loop + '" '); // Optional - OBJECT and EMBED tags
				flash.html += ('bgcolor="' + flash.bgcolor + '" '); // Optional - OBJECT and EMBED tags
				flash.html += ('menu="' + flash.menu + '" '); // Optional - OBJECT and EMBED tags				
				flash.html += ('scale="' + flash.scale + '" '); // Optional - OBJECT and EMBED tags
				flash.html += ('align="' + flash.align + '" ');	 // Optional - OBJECT and EMBED tags
				flash.html += ('salign="' + flash.salign + '" '); // Optional - OBJECT and EMBED tags					
				flash.html += ('allowFullScreen="' + flash.allowFullScreen + '" ');	// // Flash 9+
				flash.html += ('allowScriptAccess="' + flash.allowScriptAccess + '" '); // 
				flash.html += ('name="' + flash.name + '" '); // Optional - EMBED tag only
//				flash.html += ('devicefont="' + flash.devicefont + '" '); ?
				flash.html += ('wmode="' + flash.wmode + '" ');	// Optional - OBJECT and EMBED tags		
				flash.html += ('base="' + flash.base + '" ');	// Optional - OBJECT and EMBED tags						
				
				flash.html += ('pluginspage="http://www.adobe.com/go/getflashplayer" '); // Reqd - EMBED tag only
				flash.html += ('type="application/x-shockwave-flash" ');			
					
				if (flash.vars != '')
				{
					flash.html += ('flashvars="' + flash.vars + '"'); // Optional - OBJECT and EMBED tags
				}
				
				flash.html += ('>\n');
				flash.html += ('</embed>\n');
				flash.html += ('</object>\n');	
			}
			else
			{
				flash.html += (flash.noflashMsg);			
			}		
	
			if (flash.htmlAfter != '')
			{	
				flash.html += ('\n' + flash.htmlAfter + '\n');
			}
			
			flash.html += ('<!-- END GENERATED MARKUP -->\n');		
		
		// output markup

		if ($id(flash.containerId))
		{
			$id(flash.containerId).innerHTML = flash.html;	
			
			if (global_flash_available)
			{				
				document.body.className = 'flash';
			}
		}
	}
	
/* 	Init
	
	Note:
	Detection MUST be performed during page load. This is because the IE fork writes VB script to the page -
	if this was done post page load the page contents would be replaced with the script thereby erasing the page content.		
*/

	global_flash_version = detect_flash_version();
	
	if (global_flash_version >= global_required_flash_version)
	{			
		global_flash_available = true;	
		// hide_nonflash_content(); 
		// this function also references browserdetect.js, but flash.js imported last
	}
	else if (global_flash_version < global_required_flash_version)
	{		
		global_flash_available = false;			
	}	
