/* 
	@ filmnz-interactive-map.js
	===============================================================
	Functions for the interactive map @ /map
	
	Created 27.07.2005 by DS and RM
	Updates: refer SVN 
	
	To sort, search for '@ '
	_______________________________________________________________
*/	

/* 
	@ generate_flash_interactivemap_help_text
	---------------------------------------------------------------
	_______________________________________________________________
*/

	function generate_flash_interactivemap_help_text()
	{		
		var DIV = document.createElement('div');				
			DIV.id = 'maphelp';
		
		var DL = document.createElement('dl');
			DL.id = 'maphelp-1';
		
		var DT = document.createElement('dt');		
		var DT_text = document.createTextNode('Map Help');
			DT.appendChild(DT_text);
			DL.appendChild(DT);
		
		var DD = document.createElement('dd');
	
		var P_1 = document.createElement('p');
		var P_1_text = document.createTextNode('To find information and links about regions in New Zealand, click the Regions tab and select a region by clicking on the map. Use the Zoom button and the navigational bar to move up and down New Zealand. ');		
			P_1.appendChild(P_1_text);
			DD.appendChild(P_1);
		
		var P_2 = document.createElement('p');
		var P_2_text = document.createTextNode('To find information about New Zealand\'s regional film offices, click the Film Offices tab. Click on the office symbols (large red and white circles) to view more information about selected film offices. ');		
			P_2.appendChild(P_2_text);
			DD.appendChild(P_2);
		
		var P_3 = document.createElement('p');
		var P_3_text = document.createTextNode('To view films shot in New Zealand, click the Films Made in New Zealand tab and click on a location. ');		
			P_3.appendChild(P_3_text);		
			DD.appendChild(P_3);
			
		var P_4 = document.createElement('p');
		var P_4_text = document.createTextNode('To overlay snowfall information check the Snow box. ');		
			P_4.appendChild(P_4_text);		
			DD.appendChild(P_4);			
	
			DL.appendChild(DD);		
			DIV.appendChild(DL);
			
		var region = document.getElementById('region');
			//main2.insertBefore(DIV, region2);
			region.parentNode.insertBefore(DIV, region); // parentNode = liner
	}	
	
/* 
	@ add_maphelp_to_page_tools
	---------------------------------------------------------------
	_______________________________________________________________
*/	
	
	function add_maphelp_to_page_tools()
	{
		// 	add extra functions to page tools list
		var DIV = document.getElementById('page-tools');	
		var UL = DIV.getElementsByTagName('ul')[0];			
		
		// 	create print link
		
		var page_tools_maphelp_LI = document.createElement('li');
			page_tools_maphelp_LI.className = 'maphelp';
			
		var page_tools_maphelp_A = document.createElement('a');		
		var page_tools_maphelp_A_text = document.createTextNode('Interactive Map Help');		
			page_tools_maphelp_A.setAttribute('href', 'javascript:set_var_values(0,1)');
			page_tools_maphelp_A.appendChild(page_tools_maphelp_A_text);
			
			page_tools_maphelp_LI.appendChild(page_tools_maphelp_A); 

			UL.appendChild(page_tools_maphelp_LI);			
	}
	
/* 
	@ get_query_variable
	---------------------------------------------------------------
	Get the selected map from the query string passed from
	the homepage/sidebar location promos to /map
	
	To have the map display a custom location, use: 
	.../map.html?location=[location number]	
	_______________________________________________________________
*/		
	
	function get_query_variable(variable) 
	{
		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] == variable) 
			{
				return pair[1];
			}
		} 
		return false;
	}	
	
/* 
	@ translate_location_id_to_region_id
	---------------------------------------------------------------
	Map the homepage/sidebar location promo 'District' (filmnz-location-data.js) 
	to a region DL on /map (InteractiveMap_Tab1_Regions.lbi)
	
	Refer 'Default tab selection', in generate_interactivemap_map()		
	_______________________________________________________________
*/		

	function translate_location_id_to_region_id(loc) 
	{		
		switch( loc )
		{
			case '1':
				new_loc = 12; // canterbury
				break;				
				
			case '2':
			case '18':			
				new_loc = 7; // taranaki
				break;			
				
			case '3':
			case '10':			
				new_loc = 9; // wellington
				break;			
				
			case '4':
			case '6':	
			case '7':	
			case '8':		
			case '13':	
			case '14':				
				new_loc = 13; // otago
				break;					
				
			case '5':
			case '12':			
				new_loc = 2; // auckland
				break;			
				
			case '9':
			case '15':			
				new_loc = 11; // westcoast
				break;	
				
			case '11':
				new_loc = 1; // northland
				break;			
				
			case '16':
				new_loc = 6; // hawke's bay
				break;						
					
			case '17':
				new_loc = 4; // bay of plenty 
				break;		
				
			case '19':
				new_loc = 5; // gisborne, eastland
				break;	
				
			default:
				new_loc = 0; // map help
		}
		
		// console.log( 'translate_location_id_to_region_id(): ' + loc + ' -> ' + new_loc );		
		
		return new_loc;		
	}
	
/* 
	@ hide_global_content_layers
	---------------------------------------------------------------
	_______________________________________________________________
*/	
	
	function hide_global_content_layers()
	{		
		for (var i=0; i<global_content_layers.length; i++)
		{
			var content_layer = global_content_layers[i];
			
			var el = document.getElementById(content_layer);

			el.className = 'maphidden'; // flash jumping onclick in mozilla
		}
	}
	
/* 
	@ show_content_layer
	---------------------------------------------------------------
	_______________________________________________________________
*/		
	
	function show_content_layer(layer_id)
	{
		var content_layer_el = document.getElementById(layer_id);		
		content_layer_el.className = 'mapshown'; // flash jumping onclick in mozilla
	}
	
/* 
	@ show_selected_definition_list
	---------------------------------------------------------------
	_______________________________________________________________
*/		
	
	function show_selected_definition_list(content_layer_num_id, location) 
	{
		// hide all content divs
		hide_global_content_layers();

		// translate number passed from Flash to string via global_content_layers array
		g_content_layer_string_id = global_content_layers[content_layer_num_id]; // eg global_content_layers[1] = regions;

		// hide all definition lists within the selected content layer
		var content_layer_el = document.getElementById(g_content_layer_string_id);
		
		
		var definition_list = content_layer_el.getElementsByTagName('dl');				
		
		for (i=0; i<definition_list.length; i++)
		{			
		
			// hide all DLs in this 'tab' layer, except the selected one
			if ((g_content_layer_string_id + '-' + location) != (definition_list[i].id)) // ie 'regions-1 != x
			{
				definition_list[i].className = 'maphidden'; // help needs to be shown if no location shown
			}
			else 
			{
				definition_list[i].className = 'mapshown';
			}
		}
		// show selected content layer
		show_content_layer(g_content_layer_string_id);
	}	
	
/* 
	@ generate_and_insert_noflash_navigation
	---------------------------------------------------------------
	_______________________________________________________________
*/		
	
	function generate_and_insert_noflash_navigation()
	{
		var ie_fix_1 = document.createElement('p');
		var ie_fix_2 = document.createElement('p');
		
		var flash_DIV = document.getElementById('flashmap');
	
		//  (A) Create anchor nav to allow easy access to content normally accessed via flash tabs
		  
		var anchornav_DIV = document.createElement('div');
			anchornav_DIV.className = 'anchor-nav';
			
		var anchornav_P = document.createElement('p');
			anchornav_P_text = document.createTextNode('Please select a category: ');
			anchornav_P.appendChild(anchornav_P_text);

		var anchornav_UL = document.createElement('ul');
			anchornav_UL.className = 'nav horz';
			
		var anchornav_LI_1 = document.createElement('li');
			anchornav_LI_1.className = 'first';
		var anchornav_A_1 = document.createElement('a');
			anchornav_A_1.setAttribute('href','#regions');
		var anchornav_A_1_text = document.createTextNode('Regions');
			anchornav_A_1.appendChild(anchornav_A_1_text);
			anchornav_LI_1.appendChild(anchornav_A_1);
			anchornav_UL.appendChild(anchornav_LI_1);
		
		var anchornav_LI_2 = document.createElement('li');
			anchornav_DIV.className = 'anchor-nav';
		var anchornav_A_2 = document.createElement('a');
			anchornav_A_2.setAttribute('href','#filmoffices');
		var anchornav_A_2_text = document.createTextNode('Film Office');
			anchornav_A_2.appendChild(anchornav_A_2_text);
			anchornav_LI_2.appendChild(anchornav_A_2);
			anchornav_UL.appendChild(anchornav_LI_2);
		
		var anchornav_LI_3 = document.createElement('li');
		var anchornav_A_3 = document.createElement('a');		
			anchornav_A_3.setAttribute('href','#filmsmadeinnz');		
		var anchornav_A_3_text = document.createTextNode('Films Made in NZ');			
			anchornav_A_3.appendChild(anchornav_A_3_text);				
			anchornav_LI_3.appendChild(anchornav_A_3);
			anchornav_UL.appendChild(anchornav_LI_3);
			
		var clearing_DIV = document.createElement('div');
			clearing_DIV.className = 'clear';
			
			anchornav_DIV.appendChild(anchornav_P);
			anchornav_DIV.appendChild(anchornav_UL);
			anchornav_DIV.appendChild(clearing_DIV);
			
			flash_DIV.appendChild(ie_fix_1);			
			flash_DIV.appendChild(anchornav_DIV);
			flash_DIV.appendChild(ie_fix_2);			
	}
	
/* 
	@ generate_and_insert_noflash_notification
	---------------------------------------------------------------
	_______________________________________________________________
*/	
	
	function generate_and_insert_noflash_notification()
	{
		var flash_DIV = document.getElementById('flashmap');		
		
		var error_P = document.createElement('p');
			error_P.className = 'clear noflash';
			
		var error_P_text_1 = document.createTextNode('Please note: Flash Player ');
			error_P.appendChild(error_P_text_1);
			
		var error_P_text_2 = document.createTextNode(global_required_flash_version);
			error_P_text_2_bolding = document.createElement('strong');
			error_P_text_2_bolding.appendChild(error_P_text_2);
			error_P.appendChild(error_P_text_2_bolding);			
	
		var error_P_text_3 = document.createTextNode(' is required to use the Interactive Map feature. Please ');					
			error_P.appendChild(error_P_text_3);		
			
		var error_A = document.createElement('a');		
		var error_A_text = document.createTextNode('download Flash Player ');							
			error_A.setAttribute('title','Download flash player from Macromedia (opens in a new window). ');				
			error_A.setAttribute('href', 'http://www.macromedia.com/go/getflashplayer');
			error_A.setAttribute('rel', 'external'); // target="_blank"					
			error_A.appendChild(error_A_text);
			error_P.appendChild(error_A);			
			
		var error_P_text_4 = document.createTextNode('to experience the enhanced version of this page. ');				
			error_P.appendChild(error_P_text_4);					
			
			flash_DIV.appendChild(error_P);				
	}	
	
/* 
	@ generate_interactivemap_map
	---------------------------------------------------------------
	Generate flash movie
	
	flashvars used 
		- zoomlocation: to have the map display a custom location, use: .../map.html?zoomlocation=[location number]			
		- dmnid: clickthrough stats tracking (DEEPMETRIX LiveStats.NET)
	
		Default tab selection
		---------------------------------------------------------------------------------------------------------
		DS added 03.12.2008
		
		Examples:
		
		1.	http://www.filmnz.com/map.html?type=1 
		(Regions tab highlighted, map zoomed out to show nationwide regions, text pane shows Map Help info)
		
		2.	http://www.filmnz.com/map.html?type=2 
		(Film Offices tab highlighted, map zoomed out to show nationwide offices, text pane shows Map Help info)
		
		3.	http://www.filmnz.com/map.html?type=3 
		(Films Made in NZ tab highlighted, map zoomed out to show nationwide films, text pane shows Map Help info)
		
		4.	http://www.filmnz.com/map.html?location=3 
		(Regions tab highlighted, Wellington office zoomed in on map, text pane shows Wellington info)
		
		5.	http://www.filmnz.com/map.html?location=3&type=2 
		(Film Offices tab highlighted, Wellington office zoomed in on map, text pane shows Wellington info)
		
		6.	http://www.filmnz.com/map.html?type=2&location=3 
		(same as above, to illustrate that the parameter order doesn’t matter)

						
		Related updates:			
		/_admin/Flash Maps/map16_dan_infospecs.fla (/map12.swf)				
		/_admin/Flash Maps/base14.fla (/resources/flash/base11.swf)		
	_______________________________________________________________
*/		
	
	function generate_interactivemap_map(parent_el_id)
	{
		if (global_flash_available)
		{
			var defaulttab_tmp = get_query_variable('type');
			if (defaulttab_tmp)
			{
				defaulttab = defaulttab_tmp;
			}
			else
			{
				defaulttab = 1;
			}
			
			// note cachefix = last updated date (yyyymmddversion)
			
			var cachefix = '20081204d';
	
			var interactivemap_map = '';
		
				interactivemap_map += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="471" height="405" id="base10" align="middle">';
				interactivemap_map +=	 '	<param name="allowScriptAccess" value="sameDomain" />';
				interactivemap_map +=	 '	<param name="movie" value="/resources/flash/base11.swf?cachefix=' + cachefix + '" />';
				interactivemap_map +=	 '	<param name="flashvars" value="zoomlocation=' +  get_query_variable("location") + '&dmnid=' + dmnid + '&defaulttab=' + defaulttab + '" />';
				interactivemap_map +=	 '	<param name="quality" value="high" />';
				interactivemap_map +=	 '	<param name="bgcolor" value="#dcdacb" />';
				interactivemap_map +=	 '	<embed src="/resources/flash/base11.swf?cachefix=' + cachefix + '" flashvars="zoomlocation=' +  get_query_variable("location") + '&dmnid=' + dmnid + '&defaulttab=' + defaulttab + '" quality="high" bgcolor="#dcdacb" width="471" height="405" name="base10" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />';
				interactivemap_map +=	 '</object>';
				
				global_interactivemap_map = interactivemap_map;
				global_flash_used = true;
				
				
			// 	Remove the additional 'linearised' class (added for noscript support)
			var flashmap = document.getElementById('flashmap');				
				flashmap.parentNode.parentNode.className = 'blockwrapper'; // map is available, so use the extended map interface (CSS-P) to present the page copy		
					
			// 	Hide h3 headings
			var DIV = document.getElementById('content');
			var H3 = DIV.getElementsByTagName('h3');
				
				for (i=0; i<H3.length; i++)
				{
					H3[i].className = 'access';
				}
			
			// 	Add flash map help and link to help
				add_maphelp_to_page_tools();					
				
				parent_el = document.getElementById(parent_el_id);
				parent_el.innerHTML = global_interactivemap_map;
		}

		else if (!global_flash_available)
		{
			generate_and_insert_noflash_navigation();
			generate_and_insert_noflash_notification();
		}		
	}		
	
/* 
	@ init_interactivemap
	---------------------------------------------------------------
	init after page load
	_______________________________________________________________
*/		
	
	function init_interactivemap()
	{
		generate_interactivemap_map('flashmap');
		
		global_content_layers[0] = 'maphelp';
		global_content_layers[1] = 'region';
		global_content_layers[2] = 'filmoffice';
		global_content_layers[3] = 'film';

		if (global_flash_used)
		{
			generate_flash_interactivemap_help_text();			
			resize_panes_to_fit_headings();
			show_selected_definition_list(0,1); // show_selected_definition_list(content_layer_num_id, location) ie dl#maphelp-1									

			// location passed in URL string			
			if (get_query_variable('location')) 
			{
				var region_id = translate_location_id_to_region_id( get_query_variable('location') );
				
				if ( region_id > 0 )
				{
					// show_selected_definition_list(content_layer_num_id, location) 
					show_selected_definition_list(1, region_id) ; //regions, the location that is passed through the string
				}
				else
				{
					show_content_layer('maphelp');
				}								
			}
			else 
			{
				// no location passed so show Interactive Map Help text as default			
				show_content_layer('maphelp');	
			}	
		}
	}
	
/* 
	@ set_var_values
	---------------------------------------------------------------
	OLDER FUNCTION NAME REQD BY FLASH
	_______________________________________________________________
*/		
		
	function set_var_values(content_layer_num_id, location) 
	{
		show_selected_definition_list(content_layer_num_id, location);
	}		
	
/* 
	@ resize_panes_to_fit_headings
	---------------------------------------------------------------
	Resize the scroll pane to allow for multiline DTs	
	
	Default:
		.second .liner | height: 372px - (365px + 5px pad + 2px border) - line 48 of interactive_map.css
		.second .liner dl | height: 364px (not set), comprising of:
		.second .liner dl dt | height: 17px (not set)
		.second .liner dl dd | height: 347px (337px + 5px pad + 5px pad) - line 89 of interactive_map.css)
	_______________________________________________________________
*/		

	function resize_panes_to_fit_headings()
	{
		if ( ! document.getElementById('region') )
		{
			return;
		}
				
		var pane_parent = document.getElementById('region').parentNode; // .second .liner		
		var pane_dls = pane_parent.getElementsByTagName('dl'); // .second .liner dl
		var pane_dls_len = pane_dls.length;
		var pane_dl_height = 364; // see calculations in comments, above
		
		for (var i=0; i<pane_dls_len; i++)
		{
			var dt = pane_dls[i].getElementsByTagName('dt')[0]; // .second .liner dl dt
			var dd = pane_dls[i].getElementsByTagName('dd')[0]; // .second .liner dl dd					
			var dt_height = dt.offsetHeight;
			var dd_height = dd.offsetHeight;						
			var dl_height = ( dt_height + dd_height );
			
			// if this pane is taller than it should be
			if ( dl_height > pane_dl_height )
			{
				// calculate how much it is over by
				var difference = ( dl_height - pane_dl_height + 10 );
				
				// shrink the dd
				dd.style.height = ( dd_height - difference ) + 'px';
			}
		}				
	}	