/* 
	init.js
	===============================================================
	Commerce Commission
	Call Functions
	
	Last Updated: See SVN	
	
	#t-* used to apply template level override of shared styles
	#st-* used to apply sub template level override of shared styles	
	
	To sort, search for '@'		
	Debugging is via the Firebug console: console.debug
  ===============================================================
*/

//	GLOBALS

	/* these two variables are created and populated in GlossaryScannedPage.php, and output at the bottom of the template *before* init.js */
	// var glossary_url_match = '/jargon-buster/#';
	// var glossary_data_source = '/jargon-buster/ajax/'; // staging/live

	// var glossary_data_source = '/resources/ui/data/glossarylookups.php'; // local dev
	// /glossary/ajax/?ajaxget=1=foo|2=bar|3=bar
	// /glossary/ajax/?ajaxget=0=jargonbuster|1=jargonbuster
	
	function init()
	{
		// browser check		
		if (!document.getElementById || !document.getElementsByTagName || !document.createElement || !document.replaceChild)
		{
			return;
		}
		
		// collect template identifiers
		var template_id, template_cols, sub_template_id;			
		template_id = document.body.id;
		template_cols = document.body.className;
		sub_template_id = document.getElementById('template').className;
		
//	Date pickers	
			
		jQuery('.date-pair').datepicker_button();
		
//	Import additional stylesheet for x-browser support
	
		util_addStylesheet( ( whichbrowser.isOpera ), '/resources/ui/styles/imported/browser/screen-win-opera.css', 'screen' );			
		util_addStylesheet( ( whichbrowser.isMac && whichbrowser.isSafari && ( whichbrowser.versionMajor >= 526 ) ), '/resources/ui/styles/imported/browser/screen-mac-safari.css', 'screen' );					
		
// #HEADER #PRIMARY-NAV						
		
		if ( document.getElementById( 'primary-nav' ) )
		{			
			// set up primary nav hovers			
			add_nav_hovers( 'primary-nav' );
			
			var images = [
				'/resources/ui/images/navs/primary/tab-top-over.jpg', 
				'/resources/ui/images/navs/primary/tab-btm-nav-hover-first.jpg', 
				'/resources/ui/images/navs/primary/tab-btm-nav-hover-last.jpg'
			];		
			
			preload( images	);					
		}			
		
// #HEADER #SEARCH			
		
		if ( document.getElementById( 'search' ) )
		{				
			// set up search label stacking			
			stack_label( document.getElementById( 'search' ).getElementsByTagName( 'label' )[0].parentNode );	
		}					
		
// #SECONDARY-NAV			
		
		if ( document.getElementById( 'secondary-nav' ) )
		{						
			if ( template_id == 't-glossary' )
			{				
				// set up jargon buster	widget				
				testCookies( 'create_jargon_buster' );
			}
			else if ( template_id == 't-landing' )
			{					
				remove_secondary_nav_root_link();
			}
			
			add_secondary_nav_skip_link(); // after remove_secondary_nav_root_link()	
		}							
			
// #MAIN #CONTENT
		
		if ( document.getElementById('content') )
		{
			testCookies( 'force_enable_jargon_buster' ); /* was: 'enhanceGlossaryLinks' */
			
			// set up table striping
			stripe_tables( '' );
			standardistaTableSortingInit();		
										
			if ( document.getElementById('project-supporting-documents') )
			{
				// set up expand/collapse buttons					
				add_expand_buttons( 'project-supporting-documents' );
			}						
		}			
		
// #MAIN #CONTENT #MINI-SEARCH-FORM
					
		if ( document.getElementById( 'mini-search-form' ) )
		{
			// set up search label stacking
			if ( document.getElementById( 'searchkeywords' ) )
			{			
				stack_label( document.getElementById( 'searchkeywords' ).parentNode );	
			}
			// submit preload button over state	
			preload('/resources/ui/images/forms/button-search-128-over.gif');
		}			
		
// #MAIN #CONTENT #ONLINE-COMPLAINT-FORM
						
		if ( document.getElementById( 'online-complaint-form' ) )
		{						
			// set up complaint form label stacking				
			if ( document.getElementById( 'daytimeareacode' ) )
			{
				stack_label( document.getElementById( 'daytimeareacode' ).parentNode );	
			}
			if ( document.getElementById( 'faxareacode' ) )
			{			
				stack_label( document.getElementById( 'faxareacode' ).parentNode );	
			}
			if ( document.getElementById( 'businessname' ) )
			{			
				stack_label( document.getElementById( 'businessname' ).parentNode );	
			}
			if ( document.getElementById( 'businesscontactnumber' ) )
			{			
				stack_label( document.getElementById( 'businesscontactnumber' ).parentNode );	
			}
			if ( document.getElementById( 'businessaddress' ) )
			{			
				stack_label( document.getElementById( 'businessaddress' ).parentNode );	
			}
			// preload submit button over state
			preload('/resources/ui/images/forms/button-submit-258-over.gif'); 
		}			
		
// #MAIN #CONTENT #STEPS
											
		if ( document.getElementById( 'steps' ) )
		{						
			// set up #steps numbering Cufon				
			// TODO: move into template (if integrated into CMS)
			replaceFonts( '#content #steps h2 strong' );				
		}		
		
// #MAIN Global
		
		if ( document.getElementById('main') )
		{
			// add image rollovers
			init_image_hovers();				
			
			// inject pixel gifs for restyling into right-pointing arrows				
			add_arrows();			

			// JS test for accessibility page
			if ( document.getElementById('js-test') )
			{
				document.getElementById('js-test').getElementsByTagName('strong')[0].innerHTML = 'on';
			}					
		}				

//	#FOOTER #TOOLS-NAV
			
		if ( document.getElementById( 'tools-nav' ) )
		{				
			// set up print button			
			add_print_button( 'Print page' );
		}	
	}
	
	if (whichbrowser.isHiFi)
	{
		init();	
	}

