/* 
	SPARC 2006
	Master Script
	Last updated 22.02.2008 (suppress glossary on 'Search Results')	
	
*/

	var disable_pushplay06_flash = false; // change to true to disable Flash generation (within Push Play Nation 2006 section only)
	var disable_pushplay07_flash = false; // change to true to disable Flash generation (within Push Play Nation 2007 section only)	

//	FUNCTIONS

	function handleError() 
	{
		//return true;
	}
	
	window.onerror = handleError;
	
//	VARIABLES

	var global_flash_file_path = '';
	var global_add_flash_file_extension = false;	
	
	// Push Play Parents
	
	var g_ppp_host = 'http://www.sparc.org.nz'; // http://stage2.chrometoaster.com | http://sparc.wwwtest


//	IMPORT JAVASCRIPT FILES

	var scripts = new Array();
	
	scripts[0] = 'generic_browserdetect.js';	
	scripts[1] = 'generic_flash.js';			
	scripts[2] = 'generic_forms.js'; 
	scripts[3] = 'generic_printbutton.js';
	scripts[4] = 'generic_styleswitcher.js';	
	scripts[5] = 'sparc_bannerbutton.js';	
	scripts[6] = 'sparc_forms.js';		
	scripts[7] = 'sparc_map.js';
	scripts[8] = 'sparc_styleswitcherbuttons.js';			
	scripts[9] = 'sparc_elitesporttimer.js'; // 19.01.2007	
	scripts[10] = 'sparc_elitesportviewer.js'; // 24.01.2007		
	scripts[11] = 'clubkit.js';	// 29.05.2007
	scripts[12] = 'glossarypopups.js'; // 29.05.2007
	scripts[13] = 'ajax.js'; // 29.05.2007
	scripts[14] = 'events.js'; // 29.05.2007
	scripts[15] = 'sparc_nzcoach.js';	// DS, SPA2436
	
	var imported_scripts = '';
	
	for (var i=0; i<scripts.length; i++)
	{
		imported_scripts += ('\n<script type="text/javascript" src="/resources/scripts/imported/' + scripts[i] + '"></script>');
	}
	
	imported_scripts += ('\n');
	
	document.write(imported_scripts);	
	
// 	Bandwidth Detection 051206
//	Upgraded 191206 to use cookie so that detection does not need to be run on every page
//	setbandwidth() called by bandwidth_detector.swf

	function setbandwidth(bw)
	{
		createCookie("bandwidth", bw, 365);
		
		init_ppp_flash_if_sufficient_bandwidth();
	}	
	
	function init_ppp_flash_if_sufficient_bandwidth()
	{
		// read cookie - only possible if cookies enabled
		var user_bandwidth = readCookie('bandwidth');
		
		if ((user_bandwidth) && (user_bandwidth == 'high'))
		{
			init_ppp_flash(); // check what page we're on and insert the appropriate flash elements
			// alert('user_bandwidth = ' + user_bandwidth);				
		}
		else
		{
			// else show regular existing page content	
			// alert('cannot detect user_bandwidth');				
		}
	}	
	
	function check_if_referrer_is(domains)	
	{	
		// DS Created 061206 1335	
		// DS Updated 161106 1209
		// This can fail in IE, if a page was reached via a link inside a Flash movie
	
		var referrer = document.referrer.toLowerCase();	
		
		for(var d=0; d<arguments.length; d++)
		{		
			if (referrer.indexOf(arguments[d]) != -1)
			{
				return true;		
			}	
		} 	
	
		return false;	
	}	

/*	INIT - AFTER PAGE LOAD */
	
	function init_ppp_flash()
	{
		if (whichbrowser.isHiFi)
		{		
			var template_id = document.body.id;			
			var ppp_id = document.getElementById('sub-banner').className;	
		
			// *** LH navigation - images that are replaced with Flash at run time
		
			if ((template_id == 'section-5') && (document.getElementById('ppp-nav-1')))
			{				
				var ppp_nav_1 = document.getElementById('ppp-nav-1');
				var ppp_nav_1_ul = ppp_nav_1.getElementsByTagName('ul');
				var ppp_nav_1_a = ppp_nav_1.getElementsByTagName('a')[0].href;
				var ppp_nav_1_ul_ul = '';				
				
				var ppp_nav_2 = document.getElementById('ppp-nav-2');
				var ppp_nav_2_ul = ppp_nav_2.getElementsByTagName('ul');				
				var ppp_nav_2_a = ppp_nav_2.getElementsByTagName('a')[0].href;	
				var ppp_nav_2_ul_ul = '';	
				
				var ppp_nav_3 = document.getElementById('ppp-nav-3');
				var ppp_nav_3_ul = ppp_nav_3.getElementsByTagName('ul');
				var ppp_nav_3_a = ppp_nav_3.getElementsByTagName('a')[0].href;	
				var ppp_nav_3_ul_ul = '';

				// If a nav item contains a nested list, write the flash el and the nested list back to the parent list item

				if (ppp_nav_1_ul.length >= 1)
				{
					ppp_nav_1_ul_ul = ('<ul>' + ppp_nav_1_ul[0].innerHTML + '</ul>');	
				}
				if (ppp_nav_2_ul.length >= 1)
				{
					ppp_nav_2_ul_ul = ('<ul>' + ppp_nav_2_ul[0].innerHTML + '</ul>');	
				}			
				if (ppp_nav_3_ul.length >= 1)
				{
					ppp_nav_3_ul_ul = ('<ul>' + ppp_nav_3_ul[0].innerHTML + '</ul>');	
				}							
				
				generateFlashV2('ppp-nav-1', '/resources/flash/ppp/05years_[250x110].swf', '', '', 250, 110, 'linkPath', ppp_nav_1_a, '', ppp_nav_1_ul_ul, '', '');	
				generateFlashV2('ppp-nav-2', '/resources/flash/ppp/69years_[250x110].swf', '', '', 250, 110, 'linkPath', ppp_nav_2_a, '', ppp_nav_2_ul_ul, '', '');			
				generateFlashV2('ppp-nav-3', '/resources/flash/ppp/1012years_[250x110].swf', '', '', 250, 110, 'linkPath', ppp_nav_3_a, '', ppp_nav_3_ul_ul, '', '');			
			}
			
			// *** BODY - replace all content in div#flash at run time				
			
			if ((template_id == 'section-5') && (document.getElementById('ppp-main')))
			{	
				if (ppp_id == 'ppp-asquare')
				{
					generateFlashV2('flash', '/resources/flash/ppp/activitysquare_[448x448].swf', '', '', 448, 448, '', '', '', '', '', '');	
				}						
			}
			
			// *** RH navigation - images that are replaced with Flash at run time			
			
			if ((template_id == 'section-5') && (document.getElementById('ppp-quote')))
			{	
				if (ppp_id == 'ppp-05')
				{
					generateFlashV2('ppp-quote', '/resources/flash/ppp/rh_quote05_[150x250].swf', '', '', 150, 250, '', '', '', '', '', '');						
				}
				if (ppp_id == 'ppp-69')
				{
					generateFlashV2('ppp-quote', '/resources/flash/ppp/rh_quote69_[150x250].swf', '', '', 150, 250, '', '', '', '', '', '');						
				}
				if (ppp_id == 'ppp-1012')
				{
					generateFlashV2('ppp-quote', '/resources/flash/ppp/rh_quote1012_[150x250].swf', '', '', 150, 250, '', '', '', '', '', '');						
				}	
				if (ppp_id == 'ppp-home')
				{
					var ppp_quote_a = document.getElementById('ppp-quote').getElementsByTagName('a')[0].href;
					generateFlashV2('ppp-quote', '/resources/flash/ppp/activator-banner_[150x250].swf', '', '', 150, 250, 'linkPath', ppp_quote_a, '', '', '', '');						
				}							
			}				
		}
	}

	function init()
	{		
		if (whichbrowser.isHiFi)
		{	
			// first four function relocated here - ie after browser check..
			add_banner_button();	
					
			if (document.getElementById('page-tools'))
			{			
			//	DS, SPA2436
				add_print_tool('spa', 'Print page');		
				generate_resize_buttons();
				applyResize();	
			}
			
			if ((!whichbrowser.isIE5x) && (page_id != 'Glossary') && (page_id != 'Search Results') && (page_id != 'Home'))			
			{	
			//	first parameter is string which will identify Links as pointing to the glossary page
			//	second parameter is the location of the backend script that will query the database for the reqd definitions
				enhanceGlossaryLinks('/glossary?', '/ajax.aspx');
			}
		
		
			var template_id = document.body.id;
			//var page_title = document.getElementsByTagName('h3')[0].getElementsByTagName('span')[0];
			var subbanner_class = '';
			var wrappper_class = '';
			var content_div = document.getElementById('content');
			var content_div_first_h4 = content_div.getElementsByTagName('h4')[0];
			var content_div_first_h4_text = '';
			
			if (content_div_first_h4)
			{
				content_div_first_h4_text = content_div.getElementsByTagName('h4')[0].innerHTML;
			}
			
			var content_div_flash_holder = document.getElementById('flash-holder');
			
			if (document.getElementById('noflash'))
			{
				document.getElementById('noflash').innerHTML = ''; // erase contents
			}				
			
			if (document.getElementById('sub-banner'))
			{				
				var subbanner_class = document.getElementById('sub-banner').className;
			}	
			if (document.getElementById('pushplay-wrapper'))
			{				
				var wrappper_class = document.getElementById('pushplay-wrapper').className;
				var subbanner_class = document.getElementById('pushplay-banner').className;
				
				enhanceTextFields('signup');
			}				
					
			// 	FLASH CONTENT

			/*	Generate Flash Content
				generateFlash(flashContainerID, flashFileName, global_flash_action, global_flash_title, flashWidth, flashHeight, flashParamName, flashParamValue, markupBefore, markupAfter, global_noflash_link, global_noflash_title)					
			
				180906 Urgent Update:
				generateFlash() renamed to generateFlashV2()
				generateFlash() function created for legacy inpage use - 
				ie on http://www.sparc.org.nz/getting-active/push-play-day/action-packs/
				and http://www.sparc.org.nz/getting-active/push-play-day/action-packs/..
			*/
			
			//	DETERMINE WHICH FLASH CONTENT TO INSERT
			//  ---------------------------------------
			//	v1.0 - check DOM for required IDs/CLASSES
			//  v1.1 - optionally, check the page_id instead
			
				// Home Page
				
				if (page_id == 'Home')
				{								
					if ((promo1Filename != '') && (global_flash_available) && (document.getElementById('promo-1')))
					{					
						var promo_1_flashvars = '';
					
						if (document.getElementById('promo-1').getElementsByTagName('a')[0])
						{
							promo_1_href = document.getElementById('promo-1').getElementsByTagName('a')[0].href;
							promo_1_flashvars = ('url=' + promo_1_href + '\'');
						}
					
						generateFlashV3Simple('promo-1', promo1Filename, 673, 203, promo_1_flashvars, '', '');
						document.getElementById('promo-1').className += ' flashed';
					}					
				}
				
				// Push Play Nation 2006 Pages	
				
				if (!disable_pushplay06_flash)
				{					
					if ((template_id == 'section-5') && (subbanner_class == 'homepage'))
					{	
						generateFlashV2('pushplay-wrapper', '/resources/flash/pushplay06/main.swf', '', '', 775, 490, '', '', ('<div id="pushplay-banner"><h3>' + document.getElementById('pushplay-banner').getElementsByTagName('h3')[0].innerHTML + '</h3></div>'), '', '', '');				
					}			
					else if ((template_id == 'section-5') && (wrappper_class != 'signup') && ((subbanner_class == 'maggie') || (subbanner_class == 'mike') || (subbanner_class == 'petra') || (subbanner_class == 'robbie') || (subbanner_class == 'tawera')))
					{	
						generateFlashV2('pushplay-banner', '/resources/flash/pushplay06/movie.swf?textFile=' + subbanner_class + '.txt', '', '', 775, 215, '', '', ('<h3>' + document.getElementById('pushplay-banner').getElementsByTagName('h3')[0].innerHTML + '</h3><div class="liner">'), '</div>', '', '');			
					}				
				}
				
				// Push Play Nation 2007 Pages	
				
				if (!disable_pushplay07_flash)
				{					
					if ((template_id == 'section-5') && (subbanner_class == 'homepage'))
					{	
						generateFlashV2('pushplay-wrapper', '/resources/flash/pushplay07/main.swf', '', '', 775, 490, '', '', ('<div id="pushplay-banner"><h3>' + document.getElementById('pushplay-banner').getElementsByTagName('h3')[0].innerHTML + '</h3></div>'), '', '', '');				
					}			
					else if ((template_id == 'section-5') && (wrappper_class != 'signup') && ((subbanner_class == 'maggie') || (subbanner_class == 'mike') || (subbanner_class == 'petra') || (subbanner_class == 'robbie') || (subbanner_class == 'tawera')))
					{	
						generateFlashV2('pushplay-banner', '/resources/flash/pushplay07/movie.swf?textFile=' + subbanner_class + '.txt', '', '', 775, 215, '', '', ('<h3>' + document.getElementById('pushplay-banner').getElementsByTagName('h3')[0].innerHTML + '</h3><div class="liner">'), '</div>', '', '');			
					}				
				}				
				
				// 	Push Play Parents Pages
				
				//	Flash Bandwidth Detection: < 70kilobits per second == low, otherwise high with Flash
				//	If we are in the Active Living (PPP) section and a Flash container exists on the current page..
				
				if ((template_id == 'section-5') && ((document.getElementById('ppp-nav-1')) || (document.getElementById('ppp-main')) || (document.getElementById('ppp-quote'))))
				{	
					// Detection image is 30kb large, so only run the detection once if possible
					var bandwidth_already_detected = check_if_referrer_is('http://stage2.chrometoaster.com/sparc/development/web/push-play-parents', 'http://www.sparc.org.nz/getting-active/push-play-parents', 'http://sparc.org.nz/getting-active/push-play-parents', 'http://sparc.dev.bka.co.nz/getting-active/push-play-parents');			
					
					if (bandwidth_already_detected)
					{
						// read cookie and insert Flash if value == 'high'
						init_ppp_flash_if_sufficient_bandwidth(); 
					}
					else if (!bandwidth_already_detected)
					{
						// insert detection swf. This swf will detect the user's bandwidth and call setbandwidth() to set a cookie containing a value of 'high' or 'low'
						generateFlashV2('footer', '/resources/flash/ppp/bandwidth_detector.swf', '', '', 1, 1, 'flashfolder', (g_ppp_host + '/resources/flash/ppp/'), (document.getElementById('footer').innerHTML), '', '', '');					
					}
				}
				
			//	DETERMINE WHICH FLASH CONTENT TO INSERT
			//  ---------------------------------------			
			/*	v2.0 - check page_id				
				page_id added 23.01.2007, to enable application of a unique identifier to the Elite Sport Landing Page.
				page_id is populated by Vula's 'Display' > 'Additional Parameters' field, 
				and may therefore contain other values, but will only be used as specified below:
			*/
			
			if (!page_id)
			{
				page_id = '';
			}

			if (page_id == 'Elite Sport Landing Page') 
			{
				// http://www.sparc.org.nz/elite-sport
				
				make_elitesporttimer(); //-- 15.02.2007: disabled until timer Flash contents are finalised
			}	
			else if (page_id == 'Elite Sport Viewer Widget') 
			{
				if (!whichbrowser.isAnyOpera)
				{
					make_elitesportviewer();
				}
			}
			else if (page_id == 'SPARC Today Video Player') 
			{
				if (whichbrowser.isHiFi)
				{
				//	generateFlashV3Simple(flashContainerID, flashFileName, flashWidth, flashHeight, flashParams, markupBefore, markupAfter)
					generateFlashV3Simple('images', '/resources/flash/sparctoday-video-player.swf', 326, 273, ('file=' + videoSrc + '&amp;endTime=' + videoLength),'<div id="video-player">', ('</div>' + document.getElementById('images').innerHTML));	
					
					if (whichbrowser.isIE5x)
					{					
					//	display fixes for PC IE5 (shown header bg, show footer logos and tools
						document.getElementById('content').style.height = '1px';
						document.getElementById('header').style.height = '1px';									
					}
				}
			}
			else if ((!whichbrowser.isIE5x) && (page_id == 'Club Kit Homepage'))
			{		
				// ie5 doesn't support mouseenter/leave 
				enhanceClubkitPromos();
			}
			else if (page_id == 'Club Kit Getting Started')
			{					
				var h3 = document.getElementById('sub-banner').getElementsByTagName('h3')[0];
				var frame = document.getElementById('sub-banner').getElementsByTagName('span')[0]
				var img = document.getElementById('sub-banner').getElementsByTagName('img')[0];				
				
				clubkitFlashvars.filepath = img.src;					
				
				h3.id = 'sub-banner-h3';
				frame.className = 'access';

			//	generateFlashV3Simple(flashContainerID, flashFileName, flashWidth, flashHeight, flashParams, markupBefore, markupAfter)
				generateFlashV3Simple(
					'sub-banner-h3', 
					'/resources/flash/clubkit/SPA2234_Flash_[510x152].swf', 
					510, 
					152, 
					('filepath=' + clubkitFlashvars.filepath + '&amp;currentpage=' + clubkitFlashvars.currentpage + '&amp;linkOne=' + clubkitFlashvars.linkOne + '&amp;linkTwo=' + clubkitFlashvars.linkTwo + '&amp;linkThree=' + clubkitFlashvars.linkThree + '&amp;linkFour=' + clubkitFlashvars.linkFour + '&amp;linkFive=' + clubkitFlashvars.linkFive + '&amp;linkSix=' + clubkitFlashvars.linkSix), 
					(document.getElementById('sub-banner-h3').innerHTML), 
					'');
			}
			else if (page_id == 'Officials eLearning launcher')
			{				
				/*	Home > Sport Development > Officials
					This works differently to the rest of the Flash on the site:
					
					- The eLearning content is only in the Flash format, and should display in a sized javascript popup.			
					- We want to detect for Flash on the popup launch page, and display an error message if Flash is not available.
					- The popup link will only appear if JS is enabled and Flash is available.
					- The popup itself will contain a straight embed of the Flash content (matching what would usually be generated by JS).
					- This is preferable to doing the detection in the popup, as that would require relaunching the window after enabling JS or installing Flash.
			
					Another, perhaps better, but more complex, option, would be to generate the popup, and populate it with the Flash embed using Cross Window Scripting.		
				*/
				
				if ((global_flash_available) && document.getElementById('flash'))
				{
					var flash = '';
					
					flash += '\n<!-- START GENERATED MARKUP -->\n';
					flash += '<p class="download" onclick="popUp(\'e-learning.htm\', 1018, 678, \'no\');"><span class="a">Launch eLearning&nbsp;</span></p>\n';
					flash += '<div class="clear"></div>\n';
					flash += '<!-- END GENERATED MARKUP -->\n';

                    document.getElementById('flash').innerHTML = flash;       									 
				}
				else if (document.getElementById('flash'))
				{
					// No Flash or older version of Flash
					
					var noFlash = '';
					
					noFlash += '\n<!-- START GENERATED MARKUP -->\n';
					noFlash += '<p>The following content is presented in <a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">Adobe Flash</a>. </p>\n';
					noFlash += '<p>Please <a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">download the Flash Player from Adobe</a>, then refresh this page. </p>\n';
					noFlash += '<p class="download"><a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">Download Flash Player&nbsp;</a></p>\n';
					noFlash += '<div class="clear"></div>\n';
					noFlash += '<!-- END GENERATED MARKUP -->\n';

                    document.getElementById('flash').innerHTML = noFlash;
				}						
			}
			else if (page_id.match('New Zealand Coach'))
			{
			// DS, New Zealand Coach (SPA2436)				
				add_custom_print_tool();
				set_max_length();
				enhance_related_articles();		
				match_col_heights();
			}
		}
		
		// SPA2595: PM Scholarship Forms
		
		if (
				(page_id == 'PM Scholarship Forms - Athlete Step 2') ||
				(page_id == 'PM Scholarship Forms - Athlete Step 3') ||	
				(page_id == 'PM Scholarship Forms - Athlete Step 4') ||	
				(page_id == 'PM Scholarship Forms - Athlete Step 5') ||					
				(page_id == 'PM Scholarship Forms - Coach Step 2') || 
				(page_id == 'PM Scholarship Forms - Coach Step 3') || 
				(page_id == 'PM Scholarship Forms - Coach Step 4') || 
				(page_id == 'PM Scholarship Forms - Coach Step 5')			
		 		)
		{
			set_max_length();
		}	
		//changed by BKA 5/10/08	
		if (page_id == 'PM Scholarship Forms - Athlete Step 1')
		{
			checkbox_to_proceeed(
			
														document.getElementById('PageTemplate_Athelete11_AgreeTermsConditions')
													, document.getElementById('PageTemplate_btnNext')
													);			
		}
		if (page_id == 'PM Scholarship Forms - Coach Step 1')
		{
			checkbox_to_proceeed(
			
														document.getElementById('PageTemplate_Coach11_AgreeTermsConditions')
													, document.getElementById('PageTemplate_btnNext')
													);			
		}
		if (page_id == 'PM Scholarship Forms - Athlete Step 4')
		{
			calculate_total(0
											, document.getElementById('total-cost')
											, document.getElementById('PageTemplate_Athelete41_FeesJanFeb')
											, document.getElementById('PageTemplate_Athelete41_FeesMarchJune')
											, document.getElementById('PageTemplate_Athelete41_FeesJulyNov')
											, document.getElementById('PageTemplate_Athelete41_FeesNovDec')
											, document.getElementById('PageTemplate_Athelete41_FeesFullYear')						
										 );
		}
		if (page_id == 'PM Scholarship Forms - Coach Step 4')
		{
			calculate_total(0
											, document.getElementById('total-cost')
											, document.getElementById('PageTemplate_Coach41_Objective1Cost1Value')
											, document.getElementById('PageTemplate_Coach41_Objective1Cost2Value')
											, document.getElementById('PageTemplate_Coach41_Objective1Cost3Value')
											, document.getElementById('PageTemplate_Coach41_Objective1Cost4Value')											
											, document.getElementById('PageTemplate_Coach41_Objective2Cost1Value')
											, document.getElementById('PageTemplate_Coach41_Objective2Cost2Value')
											, document.getElementById('PageTemplate_Coach41_Objective2Cost3Value')	
											, document.getElementById('PageTemplate_Coach41_Objective2Cost4Value')												
											, document.getElementById('PageTemplate_Coach41_Objective3Cost1Value')		
											, document.getElementById('PageTemplate_Coach41_Objective3Cost2Value')		
											, document.getElementById('PageTemplate_Coach41_Objective3Cost3Value')	
											, document.getElementById('PageTemplate_Coach41_Objective3Cost4Value')												
											, document.getElementById('PageTemplate_Coach41_ReportSharingCosts')													
										 );
		}			
	}
	
	window.onload = init;		

	window.onresize = function() { if (whichbrowser.isHiFi && (page_id.match('New Zealand Coach'))) { match_col_heights();} }; // New Zealand Coach (SPA2436)
