// Yi Gong yi.gong@manchester.ac.uk based on May 2010
//
// Ben Walker 08/10/2009
// http://torchbox.com
// Based on a file by richard.kingston@manchester.ac.uk
/**
* HNM object keeps everything out of global scope
* @constructor
*/
function HNM() 
{
	/* Private variables */
	var centreLat = 54.4;
	var centreLon =- 4.3;
	var cTileLayerDir = "";
	var currentIndicatorURL = "";
	var currentIndicatorName = "";
	var LAMaps = new Object; // stores geo and zoom data for region select
	var map;
	var mapBounds = new GLatLngBounds(new GLatLng(49.83,-9.32),new GLatLng(60.76,2.69));
	var opacity = 0.75;
	var ie6Overlay = true;
	var p_buttonText="Terrain";
	var physTileLayers;
	var normalTileLayers;
	var myTileLayer_LA;  //local authority boundary
	var myTileLayer_UA;  //urban area boundary
	
	/*----- KML------*/
	var KMLURL = null;
	var KMLURLArray = new Array();
	var totalKML = 0;

    var myTileLayer_LA;  //local authority boundary
    var myTileLayer_UA;  //urban area boundary
	
	/* Zoom settings */
	var minZoomLevel = 4; // furthest zoom (higher value means closer zoom)
	var maxZoomLevel = 12; // closest zoom
	
	var ZOOM_LA = "11"; // level of Local Authority
	var ZOOM_BELOW_LA = "12"; // below Local Authority
	var maxZoomLevels = {
		"tiles/h1"  : ZOOM_LA,
		"tiles/h2"  : ZOOM_LA,
		"tiles/h3"  : ZOOM_LA,
		"tiles/h4"  : ZOOM_LA,
		"tiles/h5a" : ZOOM_BELOW_LA,
		"tiles/h5b" : ZOOM_BELOW_LA,
		"tiles/h5c" : ZOOM_BELOW_LA,
		"tiles/h5d" : ZOOM_BELOW_LA,
		"tiles/h6"  : ZOOM_LA,
		"tiles/h7"  : ZOOM_LA,
		"tiles/h8"  : ZOOM_BELOW_LA,
		"tiles/h9"  : ZOOM_LA,
		"tiles/h10" : ZOOM_LA,
		"tiles/h11" : ZOOM_LA,
		"tiles/BLA" : ZOOM_LA,
		"tiles/n1"  : ZOOM_LA,
		"tiles/n2"  : ZOOM_LA,
		"tiles/n3"  : ZOOM_BELOW_LA,
		"tiles/n4"  : ZOOM_BELOW_LA,
		"tiles/n5"  : ZOOM_LA,
		"tiles/n6"  : ZOOM_LA,
		"tiles/n7"  : ZOOM_LA,
		"tiles/n8"  : ZOOM_LA,
		"tiles/n9"  : ZOOM_BELOW_LA,
		"tiles/n10" : ZOOM_LA
	};
		
	/**
	* Change the opacity of the map legend
	* @param {String} className class of elements to change
	*/
	function changeLegendOpacity(className) {
		$("." + className).each(function() {
			if (jQuery.support.opacity) {
				$(this).css("opacity", opacity);
			} else {
				var o = opacity * 100;
				this.style.filter = "alpha(opacity='" + o + "')";
			}
		});
	}
	
	/**
	* Zoom the map to a chosen region
	* @param {String} laId Local Authority ID
	*/
	
	//YG
	this.changeLocation = function(laId) {
		// Pull lat, long and zoom level from array
		panToLocation(LAMaps['LA'+laId][0], LAMaps['LA'+laId][1], LAMaps['LA'+laId][2]);
		$("#dialog_LA").dialog("close");
	}
	
	/**
	* Change opacity of map tiles dynamically
	* @param {Int} op new opacity
	*/
	function changeOpacity(op) {
		opacity = op;
		$("#map img[src*='tiles']").css("opacity", op);
	}
	
	/**
	* Set new map tile opacity then reload map
	* (slower function for IE6)
	* @param {String} v CSS visibility ("visible" || "hidden")
	*/
	function changeOpacityInIE6(v) {
		// set new opacity then reload map
		$("#map div[__src__*='tiles']").css("visibility", v);
	}
	
	/**
	* See if the enter key was pressed
	* @param {Event} event the event to check (keypress, keydown etc.)
	*/
	function checkForEnter (event) {
		if (event.keyCode == 13) {
			submitPostcode();
			return false;
		}
	}
	
	/**
	* Converts tile x,y into keyhole string
	* @param {Int} a horizontal coordinate of map tile
	* @param {Int} b vertical coordinate of map tile
	* @returns {String} filename of the overlay image
	*/
	function customGetTileURL(a, b) {
		if (this.baseName === "") {
			return "blank-tile.png";
		}
		var new_b = 17 - b;
		if (b > 16 || new_b > maxZoomLevels[this.baseName]) {
			return this.baseName + "/blank-tile.png";
		}
		var c = Math.pow(2, b);
		var x = 360 / ( c * (a.x+0.0002) - 180 );
		var y = 180 - 360 / c * a.y;
		var x2 = x + 360 / c;
		var y2 = y - 360 / c;
		var lon = x; //Math.toRadians(x); // would be lon = x+lon0, but lon0 = 0 degrees
		var lat = ( 2.0 * Math.atan( Math.exp( y / 180 * Math.PI )) - Math.PI / 2.0 ) * 180 / Math.PI; //in degrees
		var lon2 = x2;
		var lat2 = ( 2.0 * Math.atan( Math.exp( y2 / 180 * Math.PI )) - Math.PI / 2.0 ) * 180 / Math.PI; //in degrees
		var tileBounds = new GLatLngBounds( new GLatLng(lat2,lon), new GLatLng(lat,lon2) );
		if ( !tileBounds.intersects( this.mapBounds ) ) {
			return this.baseName + "/blank-tile.png";
		}
		
		var z = a.x;
		var d = z + 0.5;
		var e = a.y;
		var f = "t";
		for(var g = 0; g < b; g++) {
			c = c/2;
			if(e < c) {
				if(d < c) {
					f += "q";
				} else {
					f += "r";
					d -= c;
				}
			} else {
				if(d < c) {
					f += "t";
					e -= c;
				} else {
					f += "s";
					d -= c;
					e -= c;
				}
			}
		}
		
		return this.baseName + "/" + f + ".png";
	}
	
	/**
	* Converts tile x,y into keyhole string
	* @param {Int} a horizontal coordinate of map tile
	* @param {Int} b vertical coordinate of map tile
	* @returns {String} filename of the overlay image
	*/
	/*function customGetTileURL_LA(a,b) {
		if (this.baseName === "") {
			return "blank-tile.png";
		}
		var new_b = 17 - b;
		if (b > 16 || new_b > maxZoomLevels[this.baseName]) {
			return this.baseName + "/blank-tile.png";
		}
		var c = Math.pow(2, b);
		var x = 360 / ( c * (a.x+0.0002) - 180 );
		var y = 180 - 360 / c * a.y;
		var x2 = x + 360 / c;
		var y2 = y - 360 / c;
		var lon = x; //Math.toRadians(x); // would be lon = x+lon0, but lon0 = 0 degrees
		var lat = ( 2.0 * Math.atan( Math.exp( y / 180 * Math.PI )) - Math.PI / 2.0 ) * 180 / Math.PI; //in degrees
		var lon2 = x2;
		var lat2 = ( 2.0 * Math.atan( Math.exp( y2 / 180 * Math.PI )) - Math.PI / 2.0 ) * 180 / Math.PI; //in degrees
		var tileBounds = new GLatLngBounds( new GLatLng(lat2,lon), new GLatLng(lat,lon2) );
		
		if ( !tileBounds.intersects( this.mapBounds ) ) {
			return this.baseName + "/blank-tile.png";
		}
		
		var z = a.x;
		var d = z + 0.5;
		var e = a.y;
		var f = "t";
		for(var g = 0; g < b; g++) {
			c = c/2;
			if(e < c) {
				if(d < c) {
					f += "q";
				} else {
					f += "r";
					d -= c;
				}
			} else {
				if(d < c) {
					f += "t";
					e -= c;
				} else {
					f += "s";
					d -= c;
					e -= c;
				}
			}
		}
		
		return this.baseName + "/" + f + ".png";
	}*/
	
	
	/**
	* Resize the map and toolbar if user later changes browser size
	*/
	function handleResize() {
		var content_height = $(window).height() - 50;
		$("#map, #sidebars, .sidebar").height(content_height);
		$(".sidebar-container").height(content_height - 20);
		$(".sidebar .message").height(content_height - 350);
	}
	
	/**
	* Create map, set click/change handlers etc. Called on construction.
	*/
	function init() {
		if (GBrowserIsCompatible()) {
			// Populate LAMaps with region coords and zoom
			loadRegionData();
			// Fill the screen, and listen for resize
			handleResize();
			window.onresize = handleResize;
			
			// Initialise sidebar display
			$("#sidebars").wrapInner("<div id=\"sidebars-wrapper\"></div>");
			$("#inner-sidebar .sidebar-container").prepend("<a href=\"#\" class=\"close\">Back</a>");
			if($.browser.msie && $.browser.version.substr(0,1)=="7") { // IE7 chokes on the accordion animation
				$(".accordion").accordion({ autoHeight: false, animated: false });
			} else {
				$(".accordion").accordion({ autoHeight: false });
			}
			$(".accordion ul li:last-child").addClass("last"); // styling hook for pre-CSS3 browsers
			$("#hide-show").hide();
			
			// Postcode search box hint
			$("#postcode").fieldHint();
			// KML box hint
			$("#uploadKMLurl").fieldHint();
			
			// toolbar expand/contract
			$('#toolbar-handle').toggle(function() {
				$('#toolbar').animate({'right': '-263px'}).addClass('toolbar-collapsed');
			}, function() {
				$('#toolbar').animate({'right': '0px'}).removeClass('toolbar-collapsed');
			})
			
			// Create opacity slider
			// IE6 doesn't support partial tile opacity in Google Maps
			if ($.browser.msie && $.browser.version.substr(0,1)<7) {
				$("#slider div").append("<input type='button' id='transparencyToggle' value='off' />");
				$("#slider label").text("Toggle overlay");
				$("#transparencyToggle").click(function() {
					if (ie6Overlay) {
						$(this).attr("value", "on").removeClass("active");
						ie6Overlay = false;
						changeOpacityInIE6("hidden");
					} else {
						$(this).attr("value", "off").addClass("active");
						ie6Overlay = true;
						changeOpacityInIE6("visible");
					}
				});
			} else { // clever opacity updating
				$("#slider div").slider({
					min: 0,
					max: 100,
					value: (opacity * 100),
					slide: function(event, ui) {
						changeOpacity(ui.value / 100);
						changeLegendOpacity('scale_colour');
					}
				});
			}
			// ...and hide it
			$("#slider").hide();
			
			/* Sidebar controls */
			
			// Hide sidebar
			$("#button-sidebar-hide").click(function() {
				$("body").removeClass("sidebar-left").addClass("nosidebar");
				$("#sidebars").hide();
				$("#nationalSummaryContent").hide();
			});
			// Show sidebar
			$("#button-sidebar-show").click(function() {
				$("body").removeClass("nosidebar").addClass("sidebar-left");
				$("#sidebars").show();
			});
			// Choose an overlay
			$(".indicator-links a").click(function() {
				setCurrentIndicator(
					$(this).text(),
					$(this).attr("href"));

				map.setMapType(map.getCurrentMapType()); //force reload of map
				
				$("#instructions").hide();
				$("#slider").show();
				$("#hide-show").show();
				return false;
			});
			// Back to overlay menu
			$("#inner-sidebar .close").click(function() {
				$("#sidebars-wrapper").animate(
					{ left: "0" },
					500,
					"",
					function() {
						setDataSource("");
					});
				$("#slider").hide();
				$("#hide-show").hide();
				$(".panel").hide();
				$(".notes-data").remove();
			});
			
			/* Map controls */
			
			// Postcode form submit
			$("#postcode-submit").click(function() {
				submitPostcode();
			});
			if ($.browser.mozilla) {
				$("#postcode").keypress(checkForEnter);
			} else {
				$("#postcode").keydown(checkForEnter);
			}
			
			// Region select
			$("#locationselect").change(function() {
				hnm.changeLocation($("option:selected", this).attr("value"), 0);
				//pop out more detailed local authority
				
				$("#dialog_LA").dialog({title: $("option:selected", this).attr("text")});
				$("#dialog_LA").dialog("open");
				$("div.region").hide();
				
				//England 
				if ($("option:selected", this).attr("text") == "England") {
					$("#div_England").show();
				}
				//Scotland
				if ($("option:selected", this).attr("text") == "Scotland") {
					$("#div_Scotland").show();
				}

				if ($("option:selected", this).attr("text") == "Wales") {
					$("#div_Wales").show();
				}

				if ($("option:selected", this).attr("text") == "N. Ireland") {
					$("#div_NIreland").show();
				}
				this.selectedIndex = 0; /* reset to 'select a region' */
			});
			
			// Hide dialog_LA when clicking outside of it
			$(document).click(function(event) {
				var target = jQuery(event.target);
				if (target.is('.ui-dialog') || target.parents('.ui-dialog').length
					|| target.is('#locationselect') || target.parents('#locationselect').length) {
					return;
				}
				$("#dialog_LA").dialog('close');
			})
			
			// Split selector view for England counties
			$('.split_selector').css({
				'position': 'relative'
			}).each(function() {
				var splitSelector = this;
				
				function selectItem(h3) {
					$('ul', splitSelector).hide();
					$(h3).next('ul').show();
					$('h3', splitSelector).removeClass('ui-state-active');
					$(h3).addClass('ui-state-active');
				}
				
				$('ul', splitSelector).css({
					'display': 'none',
					'position': 'absolute',
					'top': '0px',
					'left': '220px'
				});
				$('h3', splitSelector).css({
					'width': '200px'
				}).hover(function() {
					$(this).addClass('ui-state-hover');
				}, function() {
					$(this).removeClass('ui-state-hover');
				}).click(function() {
					selectItem(this);
				});
				selectItem($('h3:first', splitSelector));
			})
			
			
			// Opacity slider
			$("#slider input").click(function() {
				var newOpacity = $(this).attr("value").split("%")[0] / 100;
				changeOpacity(newOpacity);
				changeLegendOpacity('scale_colour');
				});
				
				/* Popups */
				
				// National summary popup
				$("#additional-links a#national-summary").click(function() {
					$(".panel").hide();
					$("#nationalSummaryContent").show();
					return false;
				});
				$("#nationalSummaryContent a.close").click(function() {
					$("#nationalSummaryContent").hide();
				});
				// Notes popup
				$("#additional-links a#notes").click(function() {
				$(".panel").hide();
				$(".notes-data").show();
				return false;
			});
			// Check for cookie, show instructions if first visit
			if ($.cookie('visited_hnm_before') !== "true") {
				$("#instructions").show();
				$("#instructions .close").click(function() {
					$("#instructions").hide();
				});
				$.cookie('visited_hnm_before', 'true', { expires: 180 });
			}
			
			/* Build the map */
			
			// Build copyright notice and put it on the map
			var copyright = new GCopyright(1, new GLatLngBounds(new GLatLng(-90, -180), new GLatLng(90, 180)), 0, "Design by <a href=\"http://torchbox.com\">Torchbox</a> | Developed by <a href=\"http://www.ppgis.manchester.ac.uk/\">CUPS</a> | GMapCreator <a href=\"http://www.casa.ucl.ac.uk\" >CASA</a>");
			var copyrightCollection = new GCopyrightCollection("");
			copyrightCollection.addCopyright(copyright);
			
			//create a custom G_PHYSICAL_MAP layer
			/*
				physTileLayers = [ G_PHYSICAL_MAP.getTileLayers()[0], new GTileLayer(copyrightCollection, 0, 17)];
				physTileLayers[1].getTileUrl = customGetTileURL;
				physTileLayers[1].isPng = function() { return true; };
				physTileLayers[1].getOpacity = function() { return opacity; };
				physTileLayers[1].baseName = cTileLayerDir;
				physTileLayers[1].mapBounds = mapBounds;
				var physCustomMap = new GMapType(
					physTileLayers,
					new GMercatorProjection(18),
					p_buttonText,
					{ errorMessage: "Data not available" }
				);
				physCustomMap.getMaximumResolution = function(latlng){ return maxZoomLevel; };
				physCustomMap.getMinimumResolution = function(latlng){ return minZoomLevel; };
			*/
			
			//create a custom G_NORMAL_MAP layer
			normalTileLayers = [ G_NORMAL_MAP.getTileLayers()[0], new GTileLayer(copyrightCollection, 0, 17)];
			normalTileLayers[1].getTileUrl = customGetTileURL;
			normalTileLayers[1].isPng = function() { return true; };
			normalTileLayers[1].getOpacity = function() { return opacity; };
			normalTileLayers[1].baseName = cTileLayerDir;
			normalTileLayers[1].mapBounds = mapBounds;
			
			
			/**Set tilelayer of LA boundary**/
			var tilelayer_LA = new GTileLayer(copyrightCollection);
			tilelayer_LA.getTileUrl = customGetTileURL;
			tilelayer_LA.isPng = function() { return true;};
			tilelayer_LA.getOpacity = function() { return 1.0; };
			tilelayer_LA.baseName = "tiles/BLA";
			tilelayer_LA.mapBounds = mapBounds;
			myTileLayer_LA = new GTileLayerOverlay(tilelayer_LA);

			/**Set tilelayer of Urban Area boundary**/
			var tilelayer_UA = new GTileLayer(copyrightCollection);
			tilelayer_UA.getTileUrl = customGetTileURL;
			tilelayer_UA.isPng = function() { return true; };
			tilelayer_UA.getOpacity = function() { return 1.0; };
			tilelayer_UA.baseName = "tiles/BUA";
			tilelayer_UA.mapBounds = mapBounds;
			myTileLayer_UA = new GTileLayerOverlay(tilelayer_UA);
			
			var normalCustomMap = new GMapType(
				normalTileLayers,
				new GMercatorProjection(18),
				p_buttonText,
				{ errorMessage: "Data not available" }
			);
			normalCustomMap.getMaximumResolution = function(latlng){ return maxZoomLevel; };
			normalCustomMap.getMinimumResolution = function(latlng){ return minZoomLevel; };
			
			
			// Display the map, with some controls and set the initial location
			map = new GMap2(document.getElementById("map"), { mapTypes:[normalCustomMap]});
			map.addControl(new GLargeMapControl3D());
			map.addControl(new GOverviewMapControl());
			map.enableDoubleClickZoom();
			map.enableContinuousZoom();
			map.enableScrollWheelZoom();
			map.setCenter(new GLatLng(centreLat, centreLon),map.getBoundsZoomLevel(mapBounds),normalCustomMap);
			GEvent.addListener(map,"load", setCenterToPoint);
			map.setZoom(6);
			
		} else {
			// display a warning if the browser was not compatible
			alert("Sorry, the Google Maps API is not compatible with this browser");
		}
	}
	
	/**
	* Load a new overlay and related text content
	* Set currentIndicatorURL before calling
	*/
	function loadContent() {
		var splitResult = currentIndicatorURL.split('_');
		// Load the map overlay
		setDataSource(currentIndicatorURL);
		// Load the text content
		writeLegend(currentIndicatorURL);
		// Load the national summary content
		writeSummary(currentIndicatorURL);
	}
	
	/**
	* Populate the LAMaps object with region data
	*/
	function loadRegionData() {
		// Central Lat-Long location and zoom level of Countries for zooming
		//LAMaps.LA0 = new Array(54.1,-3.4, 5);					// Whole of the UK
		LAMaps.LA1111 = new Array(54.622978,-6.663208,8);			// N. Ireland
		// LAMaps.LA55 = new Array(58.031372, -3.020117,6);		// Scotland
		LAMaps.LA2222 = new Array(56.824933,-4.801025,7);			// Scotland
		LAMaps.LA3333 = new Array(52.47635,-3.82616,8);			// Wales
		LAMaps.LA4444 = new Array(52.789476,-2.537842,7);			// England
		//////////////////////////

		//Greater London
		LAMaps.LA0 = new Array(51.51387, -0.0908, 12);         //City of London
		LAMaps.LA1 = new Array(51.54344, 0.13483, 12);         //Barking and Dagenham
		LAMaps.LA2 = new Array(51.61553, -0.20842, 12);         //Barnet
		LAMaps.LA3 = new Array(51.46036, 0.14401, 12);         //Bexley
		LAMaps.LA4 = new Array(51.55805, -0.26624, 12);         //Brent
		LAMaps.LA5 = new Array(51.37148, 0.05315, 12);         //Bromley
		LAMaps.LA6 = new Array(51.54588, -0.15581, 12);         //Camden
		LAMaps.LA7 = new Array(51.35481, -0.08556, 12);         //Croydon
		LAMaps.LA8 = new Array(51.52196, -0.32945, 12);         //Ealing
		LAMaps.LA9 = new Array(51.6505, -0.08567, 12);         //Enfield
		LAMaps.LA10 = new Array(51.47388, 0.0572, 12);         //Greenwich
		LAMaps.LA11 = new Array(51.55177, -0.06172, 12);         //Hackney
		LAMaps.LA12 = new Array(51.49451, -0.2192, 12);         //Hammersmith and Fulham
		LAMaps.LA13 = new Array(51.58987, -0.10588, 12);         //Haringey
		LAMaps.LA14 = new Array(51.59723, -0.33968, 12);         //Harrow
		LAMaps.LA15 = new Array(51.56262, 0.22206, 12);         //Havering
		LAMaps.LA16 = new Array(51.54101, -0.44409, 12);         //Hillingdon
		LAMaps.LA17 = new Array(51.46798, -0.36457, 12);         //Hounslow
		LAMaps.LA18 = new Array(51.54796, -0.10864, 12);         //Islington
		LAMaps.LA19 = new Array(51.50071, -0.19063, 12);         //Kensington and Chelsea
		LAMaps.LA20 = new Array(51.38737, -0.28533, 12);         //Kingston upon Thames
		LAMaps.LA21 = new Array(51.4533, -0.11672, 12);         //Lambeth
		LAMaps.LA22 = new Array(51.44776, -0.01869, 12);         //Lewisham
		LAMaps.LA23 = new Array(51.40942, -0.19567, 12);         //Merton
		LAMaps.LA24 = new Array(51.52634, 0.03919, 12);         //Newham
		LAMaps.LA25 = new Array(51.58515, 0.07749, 12);         //Redbridge
		LAMaps.LA26 = new Array(51.44223, -0.31075, 12);         //Richmond upon Thames
		LAMaps.LA27 = new Array(51.47371, -0.07244, 12);         //Southwark
		LAMaps.LA28 = new Array(51.36155, -0.17599, 12);         //Sutton
		LAMaps.LA29 = new Array(51.51522, -0.03304, 12);         //Tower Hamlets
		LAMaps.LA30 = new Array(51.59354, -0.01102, 12);         //Waltham Forest
		LAMaps.LA31 = new Array(51.45146, -0.18461, 12);         //Wandsworth
		LAMaps.LA32 = new Array(51.51293, -0.15885, 12);         //Westminster
		//East Midlands
		LAMaps.LA83 = new Array(52.91222, -1.46684, 8);         //Derby UA
		LAMaps.LA84 = new Array(52.63755, -1.12694, 8);         //Leicester UA
		LAMaps.LA85 = new Array(52.66038, -0.65306, 8);         //Rutland UA
		LAMaps.LA86 = new Array(52.96027, -1.17498, 8);         //Nottingham UA
		LAMaps.LA168 = new Array(53.02895, -1.46508, 8);         //Amber Valley
		LAMaps.LA169 = new Array(53.21541, -1.27569, 8);         //Bolsover
		LAMaps.LA170 = new Array(53.25599, -1.39969, 8);         //Chesterfield
		LAMaps.LA171 = new Array(53.12816, -1.69214, 8);         //Derbyshire Dales
		LAMaps.LA172 = new Array(52.93518, -1.34521, 8);         //Erewash
		LAMaps.LA173 = new Array(53.3728, -1.87241, 8);         //High Peak
		LAMaps.LA174 = new Array(53.2256, -1.44679, 8);         //North East Derbyshir
		LAMaps.LA175 = new Array(52.83071, -1.55804, 8);         //South Derbyshire
		LAMaps.LA265 = new Array(52.57115, -1.22043, 8);         //Blaby
		LAMaps.LA266 = new Array(52.73666, -1.14464, 8);         //Charnwood
		LAMaps.LA267 = new Array(52.53896, -1.00635, 8);         //Harborough
		LAMaps.LA268 = new Array(52.61613, -1.38952, 8);         //Hinckley and Boswort
		LAMaps.LA269 = new Array(52.79611, -0.85457, 8);         //Melton
		LAMaps.LA270 = new Array(52.75281, -1.4035, 8);         //North West Leicester
		LAMaps.LA271 = new Array(52.58675, -1.09189, 8);         //Oadby and Wigston
		LAMaps.LA272 = new Array(52.97386, -0.02057, 8);         //Boston
		LAMaps.LA273 = new Array(53.2498, 0.03161, 8);         //East Lindsey
		LAMaps.LA274 = new Array(53.22442, -0.5533, 8);         //Lincoln
		LAMaps.LA275 = new Array(53.08086, -0.44451, 8);         //North Kesteven
		LAMaps.LA276 = new Array(52.79807, -0.02876, 8);         //South Holland
		LAMaps.LA277 = new Array(52.8541, -0.51664, 8);         //South Kesteven
		LAMaps.LA278 = new Array(53.39254, -0.47532, 8);         //West Lindsey
		LAMaps.LA286 = new Array(52.49869, -0.69433, 8);         //Corby
		LAMaps.LA287 = new Array(52.31565, -1.04651, 8);         //Daventry
		LAMaps.LA288 = new Array(52.45731, -0.52183, 8);         //East Northamptonshire
		LAMaps.LA289 = new Array(52.42577, -0.76393, 8);         //Kettering
		LAMaps.LA290 = new Array(52.24084, -0.88536, 8);         //Northampton
		LAMaps.LA291 = new Array(52.12221, -1.04113, 8);         //South Northamptonshi
		LAMaps.LA292 = new Array(52.2874, -0.71078, 8);         //Wellingborough
		LAMaps.LA306 = new Array(53.09198, -1.25763, 8);         //Ashfield
		LAMaps.LA307 = new Array(53.33254, -0.9573, 8);         //Bassetlaw
		LAMaps.LA308 = new Array(52.97614, -1.26059, 8);         //Broxtowe
		LAMaps.LA309 = new Array(53.02807, -1.11782, 8);         //Gedling
		LAMaps.LA310 = new Array(53.17043, -1.1712, 8);         //Mansfield
		LAMaps.LA311 = new Array(53.12368, -0.91816, 8);         //Newark and Sherwood
		LAMaps.LA312 = new Array(52.90052, -1.03598, 8);         //Rushcliffe
		//East of England
		LAMaps.LA99 = new Array(52.60814, -0.25145, 10);         //Peterborough UA
		LAMaps.LA100 = new Array(51.89377, -0.42401, 10);         //Luton UA
		LAMaps.LA101 = new Array(51.53757, 0.73026, 10);         //Southend-on-Sea UA
		LAMaps.LA102 = new Array(51.50546, 0.37478, 10);         //Thurrock UA
		LAMaps.LA137 = new Array(52.04969, -0.39615, 10);         //Mid Bedfordshire
		LAMaps.LA138 = new Array(52.1888, -0.4755, 10);         //Bedford
		LAMaps.LA139 = new Array(51.90329, -0.52512, 10);         //South Bedfordshire
		LAMaps.LA144 = new Array(52.19894, 0.13459, 10);         //Cambridge
		LAMaps.LA145 = new Array(52.34263, 0.29629, 10);         //East Cambridgeshire
		LAMaps.LA146 = new Array(52.56254, 0.06248, 10);         //Fenland
		LAMaps.LA147 = new Array(52.37332, -0.21914, 10);         //Huntingdonshire
		LAMaps.LA148 = new Array(52.1724, 0.08289, 10);         //South Cambridgeshire
		LAMaps.LA202 = new Array(51.58963, 0.46795, 10);         //Basildon
		LAMaps.LA203 = new Array(51.94147, 0.59159, 10);         //Braintree
		LAMaps.LA204 = new Array(51.63815, 0.31242, 10);         //Brentwood
		LAMaps.LA205 = new Array(51.53606, 0.59572, 10);         //Castle Point
		LAMaps.LA206 = new Array(51.72498, 0.48254, 10);         //Chelmsford
		LAMaps.LA207 = new Array(51.8721, 0.8626, 10);         //Colchester
		LAMaps.LA208 = new Array(51.70839, 0.15038, 10);         //Epping Forest
		LAMaps.LA209 = new Array(51.76787, 0.11019, 10);         //Harlow
		LAMaps.LA210 = new Array(51.70923, 0.78482, 10);         //Maldon
		LAMaps.LA211 = new Array(51.59086, 0.81121, 10);         //Rochford
		LAMaps.LA212 = new Array(51.87097, 1.11679, 10);         //Tendring
		LAMaps.LA213 = new Array(51.93423, 0.28139, 10);         //Uttlesford
		LAMaps.LA231 = new Array(51.72463, -0.04574, 10);         //Broxbourne
		LAMaps.LA232 = new Array(51.77772, -0.53935, 10);         //Dacorum
		LAMaps.LA233 = new Array(51.86463, -0.00659, 10);         //East Hertfordshire
		LAMaps.LA234 = new Array(51.67679, -0.2795, 10);         //Hertsmere
		LAMaps.LA235 = new Array(51.95865, -0.1909, 10);         //North Hertfordshire
		LAMaps.LA236 = new Array(51.77162, -0.34091, 10);         //St. Albans
		LAMaps.LA237 = new Array(51.90387, -0.18775, 10);         //Stevenage
		LAMaps.LA238 = new Array(51.6605, -0.45901, 10);         //Three Rivers
		LAMaps.LA239 = new Array(51.66762, -0.4018, 10);         //Watford
		LAMaps.LA240 = new Array(51.76644, -0.19252, 10);         //Welwyn Hatfield
		LAMaps.LA279 = new Array(52.58773, 0.83905, 10);         //Breckland
		LAMaps.LA280 = new Array(52.7008, 1.31203, 10);         //Broadland
		LAMaps.LA281 = new Array(52.64042, 1.6632, 10);         //Great Yarmouth
		LAMaps.LA282 = new Array(52.72538, 0.47386, 10);         //King's Lynn and West
		LAMaps.LA283 = new Array(52.85035, 1.17065, 10);         //North Norfolk
		LAMaps.LA284 = new Array(52.63143, 1.28461, 10);         //Norwich
		LAMaps.LA285 = new Array(52.51416, 1.27778, 10);         //South Norfolk
		LAMaps.LA336 = new Array(52.05008, 0.91704, 10);         //Babergh
		LAMaps.LA337 = new Array(52.3544, 0.5333, 10);         //Forest Heath
		LAMaps.LA338 = new Array(52.05541, 1.16155, 10);         //Ipswich
		LAMaps.LA339 = new Array(52.24643, 1.09435, 10);         //Mid Suffolk
		LAMaps.LA340 = new Array(52.23305, 0.67616, 10);         //St. Edmundsbury
		LAMaps.LA341 = new Array(52.16241, 1.41385, 10);         //Suffolk Coastal
		LAMaps.LA342 = new Array(52.41886, 1.58663, 10);         //Waveney
		//North East
		LAMaps.LA52 = new Array(54.93527, -1.68135, 10);         //Gateshead
		LAMaps.LA53 = new Array(55.0076, -1.65627, 10);         //Newcastle upon Tyne
		LAMaps.LA54 = new Array(55.02803, -1.51554, 10);         //North Tyneside
		LAMaps.LA55 = new Array(54.96348, -1.44258, 10);         //South Tyneside
		LAMaps.LA56 = new Array(54.88033, -1.45036, 10);         //Sunderland
		LAMaps.LA69 = new Array(54.66895, -1.25399, 10);         //Hartlepool UA
		LAMaps.LA70 = new Array(54.54235, -1.22084, 10);         //Middlesbrough UA
		LAMaps.LA71 = new Array(54.55345, -1.01999, 10);         //Redcar and Cleveland UA
		LAMaps.LA72 = new Array(54.56265, -1.32734, 10);         //Stockton-on-Tees UA
		LAMaps.LA73 = new Array(54.54857, -1.55106, 10);         //Darlington UA
		LAMaps.LA190 = new Array(54.85358, -1.59015, 10);         //Chester-le-Street
		LAMaps.LA191 = new Array(54.82923, -1.80528, 10);         //Derwentside
		LAMaps.LA192 = new Array(54.76401, -1.56442, 10);         //Durham
		LAMaps.LA193 = new Array(54.76684, -1.36222, 10);         //Easington
		LAMaps.LA194 = new Array(54.65741, -1.51238, 10);         //Sedgefield
		LAMaps.LA195 = new Array(54.59344, -2.00603, 10);         //Teesdale
		LAMaps.LA196 = new Array(54.74124, -1.98881, 10);         //Wear Valley
		LAMaps.LA293 = new Array(55.35504, -1.91785, 10);         //Alnwick
		LAMaps.LA294 = new Array(55.59264, -1.99198, 10);         //Berwick upon Tweed
		LAMaps.LA295 = new Array(55.09201, -1.54971, 10);         //Blyth Valley
		LAMaps.LA296 = new Array(55.14763, -1.77576, 10);         //Castle Morpeth
		LAMaps.LA297 = new Array(55.05943, -2.28166, 10);         //Tynedale
		LAMaps.LA298 = new Array(55.16246, -1.57608, 10);         //Wansbeck
		///North west
		LAMaps.LA33 = new Array(53.57841, -2.46554, 10);         //Bolton
		LAMaps.LA34 = new Array(53.58763, -2.31112, 10);         //Bury
		LAMaps.LA35 = new Array(53.45129, -2.23104, 10);         //Manchester
		LAMaps.LA36 = new Array(53.55164, -2.04918, 10);         //Oldham
		LAMaps.LA37 = new Array(53.61634, -2.15636, 10);         //Rochdale
		LAMaps.LA38 = new Array(53.48929, -2.36871, 10);         //Salford
		LAMaps.LA39 = new Array(53.3937, -2.12666, 10);         //Stockport
		LAMaps.LA40 = new Array(53.47722, -2.06131, 10);         //Tameside
		LAMaps.LA41 = new Array(53.41876, -2.35392, 10);         //Trafford
		LAMaps.LA42 = new Array(53.52392, -2.58817, 10);         //Wigan
		LAMaps.LA43 = new Array(53.42488, -2.83272, 10);         //Knowsley
		LAMaps.LA44 = new Array(53.39524, -2.9157, 10);         //Liverpool
		LAMaps.LA45 = new Array(53.45923, -2.72226, 10);         //St. Helens
		LAMaps.LA46 = new Array(53.56508, -3.01485, 10);         //Sefton
		LAMaps.LA47 = new Array(53.36862, -3.09124, 10);         //Wirral
		LAMaps.LA74 = new Array(53.34349, -2.71742, 10);         //Halton UA
		LAMaps.LA75 = new Array(53.39857, -2.5622, 10);         //Warrington UA
		LAMaps.LA76 = new Array(53.69243, -2.46616, 10);         //Blackburn with Darwen UA
		LAMaps.LA77 = new Array(53.81748, -3.03416, 10);         //Blackpool UA
		LAMaps.LA149 = new Array(53.14445, -2.81516, 10);         //Chester
		LAMaps.LA150 = new Array(53.16116, -2.30376, 10);         //Congleton
		LAMaps.LA151 = new Array(53.06241, -2.5309, 10);         //Crewe and Nantwich
		LAMaps.LA152 = new Array(53.28642, -2.95329, 10);         //Ellesmere Port & Nes
		LAMaps.LA153 = new Array(53.28377, -2.21641, 10);         //Macclesfield
		LAMaps.LA154 = new Array(53.23992, -2.57766, 10);         //Vale Royal
		LAMaps.LA162 = new Array(54.71162, -3.2454, 10);         //Allerdale
		LAMaps.LA163 = new Array(54.12645, -3.21099, 10);         //Barrow-in-Furness
		LAMaps.LA164 = new Array(54.97098, -2.80743, 10);         //Carlisle
		LAMaps.LA165 = new Array(54.41396, -3.37207, 10);         //Copeland
		LAMaps.LA166 = new Array(54.61282, -2.62069, 10);         //Eden
		LAMaps.LA167 = new Array(54.29261, -2.84857, 10);         //South Lakeland
		LAMaps.LA253 = new Array(53.77873, -2.2205, 10);         //Burnley
		LAMaps.LA254 = new Array(53.66126, -2.64646, 10);         //Chorley
		LAMaps.LA255 = new Array(53.78375, -2.92148, 10);         //Fylde
		LAMaps.LA256 = new Array(53.75844, -2.38836, 10);         //Hyndburn
		LAMaps.LA257 = new Array(54.06892, -2.71346, 10);         //Lancaster
		LAMaps.LA258 = new Array(53.8653, -2.18346, 10);         //Pendle
		LAMaps.LA259 = new Array(53.81161, -2.69623, 10);         //Preston
		LAMaps.LA260 = new Array(53.90709, -2.44326, 10);         //Ribble Valley
		LAMaps.LA261 = new Array(53.6936, -2.26586, 10);         //Rossendale
		LAMaps.LA262 = new Array(53.72791, -2.70882, 10);         //South Ribble
		LAMaps.LA263 = new Array(53.60941, -2.88123, 10);         //West Lancashire
		LAMaps.LA264 = new Array(53.90277, -2.85259, 10);         //Wyre
		//South East
		LAMaps.LA103 = new Array(51.4252, 0.57106, 10);         //Medway UA
		LAMaps.LA104 = new Array(51.40762, -0.73818, 10);         //Bracknell Forest UA
		LAMaps.LA105 = new Array(51.44909, -1.30304, 10);         //West Berkshire UA
		LAMaps.LA106 = new Array(51.45248, -0.98232, 10);         //Reading UA
		LAMaps.LA107 = new Array(51.5079, -0.58471, 10);         //Slough UA
		LAMaps.LA108 = new Array(51.48518, -0.69802, 10);         //Windsor and Maidenhead UA
		LAMaps.LA109 = new Array(51.42895, -0.8823, 10);         //Wokingham UA
		LAMaps.LA110 = new Array(52.08342, -0.73196, 10);         //Milton Keynes UA
		LAMaps.LA111 = new Array(50.84289, -0.13155, 10);         //Brighton and Hove UA
		LAMaps.LA112 = new Array(50.81988, -1.07696, 10);         //Portsmouth UA
		LAMaps.LA113 = new Array(50.91645, -1.3973, 10);         //Southampton UA
		LAMaps.LA114 = new Array(50.67538, -1.29477, 10);         //Isle of Wight UA
		LAMaps.LA140 = new Array(51.89181, -0.88391, 10);         //Aylesbury Vale
		LAMaps.LA141 = new Array(51.68004, -0.62623, 10);         //Chiltern
		LAMaps.LA142 = new Array(51.55589, -0.58849, 10);         //South Bucks
		LAMaps.LA143 = new Array(51.64877, -0.81372, 10);         //Wycombe
		LAMaps.LA197 = new Array(50.7755, 0.26781, 10);         //Eastbourne
		LAMaps.LA198 = new Array(50.86874, 0.57203, 10);         //Hastings
		LAMaps.LA199 = new Array(50.88158, 0.00656, 10);         //Lewes
		LAMaps.LA200 = new Array(50.95098, 0.54096, 10);         //Rother
		LAMaps.LA201 = new Array(50.96693, 0.20016, 10);         //Wealden
		LAMaps.LA220 = new Array(51.27294, -1.19951, 10);         //Basingstoke and Dean
		LAMaps.LA221 = new Array(51.07411, -0.95358, 10);         //East Hampshire
		LAMaps.LA222 = new Array(50.93249, -1.32577, 10);         //Eastleigh
		LAMaps.LA223 = new Array(50.85203, -1.2235, 10);         //Fareham
		LAMaps.LA224 = new Array(50.80468, -1.15791, 10);         //Gosport
		LAMaps.LA225 = new Array(51.28432, -0.89595, 10);         //Hart
		LAMaps.LA226 = new Array(50.83749, -0.98776, 10);         //Havant
		LAMaps.LA227 = new Array(50.85777, -1.62334, 10);         //New Forest
		LAMaps.LA228 = new Array(51.27292, -0.76596, 10);         //Rushmoor
		LAMaps.LA229 = new Array(51.12809, -1.50921, 10);         //Test Valley
		LAMaps.LA230 = new Array(51.02772, -1.21976, 10);         //Winchester
		LAMaps.LA241 = new Array(51.12476, 0.81312, 10);         //Ashford
		LAMaps.LA242 = new Array(51.28332, 1.10336, 10);         //Canterbury
		LAMaps.LA243 = new Array(51.43093, 0.2548, 10);         //Dartford
		LAMaps.LA244 = new Array(51.21184, 1.29099, 10);         //Dover
		LAMaps.LA245 = new Array(51.40461, 0.39667, 10);         //Gravesham
		LAMaps.LA246 = new Array(51.23446, 0.58226, 10);         //Maidstone
		LAMaps.LA247 = new Array(51.27496, 0.17372, 10);         //Sevenoaks
		LAMaps.LA248 = new Array(51.05328, 0.99588, 10);         //Shepway
		LAMaps.LA249 = new Array(51.34115, 0.81487, 10);         //Swale
		LAMaps.LA250 = new Array(51.35394, 1.34244, 10);         //Thanet
		LAMaps.LA251 = new Array(51.27309, 0.35957, 10);         //Tonbridge and Mallin
		LAMaps.LA252 = new Array(51.11382, 0.43233, 10);         //Tunbridge Wells
		LAMaps.LA313 = new Array(51.95847, -1.27438, 10);         //Cherwell
		LAMaps.LA314 = new Array(51.75413, -1.2395, 10);         //Oxford
		LAMaps.LA315 = new Array(51.63338, -1.07, 10);         //South Oxfordshire
		LAMaps.LA316 = new Array(51.63376, -1.44014, 10);         //Vale of White Horse
		LAMaps.LA317 = new Array(51.83721, -1.5054, 10);         //West Oxfordshire
		LAMaps.LA343 = new Array(51.35662, -0.3906, 10);         //Elmbridge
		LAMaps.LA344 = new Array(51.33865, -0.25987, 10);         //Epsom and Ewell
		LAMaps.LA345 = new Array(51.24725, -0.56009, 10);         //Guildford
		LAMaps.LA346 = new Array(51.21178, -0.32354, 10);         //Mole Valley
		LAMaps.LA347 = new Array(51.2527, -0.19028, 10);         //Reigate and Banstead
		LAMaps.LA348 = new Array(51.39328, -0.53977, 10);         //Runnymede
		LAMaps.LA349 = new Array(51.4243, -0.46644, 10);         //Spelthorne
		LAMaps.LA350 = new Array(51.34241, -0.66899, 10);         //Surrey Heath
		LAMaps.LA351 = new Array(51.22653, -0.0418, 10);         //Tandridge
		LAMaps.LA352 = new Array(51.14926, -0.63119, 10);         //Waverley
		LAMaps.LA353 = new Array(51.31335, -0.55922, 10);         //Woking
		LAMaps.LA359 = new Array(50.84389, -0.29704, 10);         //Adur
		LAMaps.LA360 = new Array(50.83969, -0.56622, 10);         //Arun
		LAMaps.LA361 = new Array(50.93658, -0.74074, 10);         //Chichester
		LAMaps.LA362 = new Array(51.12492, -0.18045, 10);         //Crawley
		LAMaps.LA363 = new Array(50.99327, -0.36472, 10);         //Horsham
		LAMaps.LA364 = new Array(51.02036, -0.12535, 10);         //Mid Sussex
		LAMaps.LA365 = new Array(50.82776, -0.39446, 10);         //Worthing
		//South West
		LAMaps.LA90 = new Array(51.35548, -2.47387, 10);         //Bath and North East Some
		LAMaps.LA91 = new Array(51.45218, -2.76928, 10);         //Bristol, City of UA
		LAMaps.LA92 = new Array(51.38871, -2.79961, 10);         //North Somerset UA
		LAMaps.LA93 = new Array(51.54639, -2.46939, 10);         //South Gloucestershire UA
		LAMaps.LA94 = new Array(50.39118, -4.11871, 10);         //Plymouth UA
		LAMaps.LA95 = new Array(50.44004, -3.52902, 10);         //Torbay UA
		LAMaps.LA96 = new Array(50.74128, -1.85669, 10);         //Bournemouth UA
		LAMaps.LA97 = new Array(50.73976, -1.96375, 10);         //Poole UA
		LAMaps.LA98 = new Array(51.57108, -1.74239, 10);         //Swindon UA
		LAMaps.LA155 = new Array(50.43915, -4.42511, 10);         //Caradon
		LAMaps.LA156 = new Array(50.27601, -5.05145, 10);         //Carrick
		LAMaps.LA157 = new Array(50.12351, -5.2242, 10);         //Kerrier
		LAMaps.LA158 = new Array(50.62556, -4.61111, 10);         //North Cornwall
		LAMaps.LA159 = new Array(50.14009, -5.53561, 10);         //Penwith
		LAMaps.LA160 = new Array(50.37537, -4.85333, 10);         //Restormel
		LAMaps.LA161 = new Array(49.92779, -6.33477, 10);         //Isles of Scilly
		LAMaps.LA176 = new Array(50.76365, -3.22317, 10);         //East Devon
		LAMaps.LA177 = new Array(50.72108, -3.50899, 10);         //Exeter
		LAMaps.LA178 = new Array(50.87569, -3.55498, 10);         //Mid Devon
		LAMaps.LA179 = new Array(51.06937, -3.91385, 10);         //North Devon
		LAMaps.LA180 = new Array(50.37345, -3.82186, 10);         //South Hams
		LAMaps.LA181 = new Array(50.60582, -3.65526, 10);         //Teignbridge
		LAMaps.LA182 = new Array(50.887, -4.24966, 10);         //Torridge
		LAMaps.LA183 = new Array(50.6557, -4.04497, 10);         //West Devon
		LAMaps.LA184 = new Array(50.76092, -1.78682, 10);         //Christchurch
		LAMaps.LA185 = new Array(50.86317, -1.96017, 10);         //East Dorset
		LAMaps.LA186 = new Array(50.912, -2.24502, 10);         //North Dorset
		LAMaps.LA187 = new Array(50.68108, -2.12989, 10);         //Purbeck
		LAMaps.LA188 = new Array(50.78397, -2.57714, 10);         //West Dorset
		LAMaps.LA189 = new Array(50.60895, -2.45406, 10);         //Weymouth and Portlan
		LAMaps.LA214 = new Array(51.89657, -2.07263, 10);         //Cheltenham
		LAMaps.LA215 = new Array(51.81623, -1.891, 10);         //Cotswold
		LAMaps.LA216 = new Array(51.82801, -2.48843, 10);         //Forest of Dean
		LAMaps.LA217 = new Array(51.84893, -2.23889, 10);         //Gloucester
		LAMaps.LA218 = new Array(51.72882, -2.29865, 10);         //Stroud
		LAMaps.LA219 = new Array(51.94518, -2.08592, 10);         //Tewkesbury
		LAMaps.LA323 = new Array(51.20238, -2.54322, 10);         //Mendip
		LAMaps.LA324 = new Array(51.18433, -2.95132, 10);         //Sedgemoor
		LAMaps.LA325 = new Array(50.98758, -2.70824, 10);         //South Somerset
		LAMaps.LA326 = new Array(51.00999, -3.15598, 10);         //Taunton Deane
		LAMaps.LA327 = new Array(51.13344, -3.48912, 10);         //West Somerset
		LAMaps.LA366 = new Array(51.35496, -1.77683, 10);         //Kennet
		LAMaps.LA367 = new Array(51.52515, -2.04985, 10);         //North Wiltshire
		LAMaps.LA368 = new Array(51.08971, -1.91344, 10);         //Salisbury
		LAMaps.LA369 = new Array(51.24963, -2.1539, 10);         //West Wiltshire
		//West Mildlands
		LAMaps.LA57 = new Array(52.48824, -1.87327, 10);         //Birmingham
		LAMaps.LA58 = new Array(52.41648, -1.51829, 10);         //Coventry
		LAMaps.LA59 = new Array(52.48249, -2.10732, 10);         //Dudley
		LAMaps.LA60 = new Array(52.51509, -2.00755, 10);         //Sandwell
		LAMaps.LA61 = new Array(52.41274, -1.72563, 10);         //Solihull
		LAMaps.LA62 = new Array(52.59891, -1.96492, 10);         //Walsall
		LAMaps.LA63 = new Array(52.58885, -2.11958, 10);         //Wolverhampton
		LAMaps.LA87 = new Array(52.10328, -2.7438, 10);         //County of Herefordshire
		LAMaps.LA88 = new Array(52.72989, -2.48741, 10);         //Telford and Wrekin UA
		LAMaps.LA89 = new Array(53.01774, -2.16312, 10);         //Stoke-on-Trent UA
		LAMaps.LA318 = new Array(52.54209, -2.42916, 10);         //Bridgnorth
		LAMaps.LA319 = new Array(52.87661, -2.67333, 10);         //North Shropshire
		LAMaps.LA320 = new Array(52.83876, -3.02876, 10);         //Oswestry
		LAMaps.LA321 = new Array(52.6689, -2.77884, 10);         //Shrewsbury and Atcha
		LAMaps.LA322 = new Array(52.46015, -2.85849, 10);         //South Shropshire
		LAMaps.LA328 = new Array(52.71206, -1.97878, 10);         //Cannock Chase
		LAMaps.LA329 = new Array(52.86302, -1.81081, 10);         //East Staffordshire
		LAMaps.LA330 = new Array(52.68785, -1.80351, 10);         //Lichfield
		LAMaps.LA331 = new Array(52.99287, -2.31735, 10);         //Newcastle-under-Lyme
		LAMaps.LA332 = new Array(52.62522, -2.17525, 10);         //South Staffordshire
		LAMaps.LA333 = new Array(52.84785, -2.17507, 10);         //Stafford
		LAMaps.LA334 = new Array(53.07908, -1.9798, 10);         //Staffordshire Moorla
		LAMaps.LA335 = new Array(52.62608, -1.67637, 10);         //Tamworth
		LAMaps.LA354 = new Array(52.54717, -1.62656, 10);         //North Warwickshire
		LAMaps.LA355 = new Array(52.50076, -1.47438, 10);         //Nuneaton and Bedwort
		LAMaps.LA356 = new Array(52.38405, -1.32684, 10);         //Rugby
		LAMaps.LA357 = new Array(52.1784, -1.6215, 10);         //Stratford on Avon
		LAMaps.LA358 = new Array(52.30961, -1.58321, 10);         //Warwick
		LAMaps.LA370 = new Array(52.36138, -2.01461, 10);         //Bromsgrove
		LAMaps.LA371 = new Array(52.18256, -2.34364, 10);         //Malvern Hills
		LAMaps.LA372 = new Array(52.2819, -1.95041, 10);         //Redditch
		LAMaps.LA373 = new Array(52.1963, -2.20993, 10);         //Worcester
		LAMaps.LA374 = new Array(52.1639, -2.04965, 10);         //Wychavon
		LAMaps.LA375 = new Array(52.3792, -2.27325, 10);         //Wyre Forest
		//Yorkshire and The Humber
		LAMaps.LA48 = new Array(53.53558, -1.53098, 10);         //Barnsley
		LAMaps.LA49 = new Array(53.54409, -1.09412, 10);         //Doncaster
		LAMaps.LA50 = new Array(53.40838, -1.2847, 10);         //Rotherham
		LAMaps.LA51 = new Array(53.40416, -1.54898, 10);         //Sheffield
		LAMaps.LA64 = new Array(53.84449, -1.85323, 10);         //Bradford
		LAMaps.LA65 = new Array(53.72096, -1.97199, 10);         //Calderdale
		LAMaps.LA66 = new Array(53.62715, -1.76171, 10);         //Kirklees
		LAMaps.LA67 = new Array(53.82554, -1.50448, 10);         //Leeds
		LAMaps.LA68 = new Array(53.66042, -1.41025, 10);         //Wakefield
		LAMaps.LA78 = new Array(53.75966, -0.33366, 10);         //Kingston upon Hull, City
		LAMaps.LA79 = new Array(53.86757, -0.48049, 10);         //East Riding of Yorkshire
		LAMaps.LA80 = new Array(53.54372, -0.13108, 10);         //North East Lincolnshire
		LAMaps.LA81 = new Array(53.59365, -0.5909, 10);         //North Lincolnshire UA
		LAMaps.LA82 = new Array(53.95854, -1.05986, 10);         //York UA
		LAMaps.LA299 = new Array(54.07971, -2.17312, 10);         //Craven
		LAMaps.LA300 = new Array(54.2869, -1.31766, 10);         //Hambleton
		LAMaps.LA301 = new Array(54.06696, -1.59428, 10);         //Harrogate
		LAMaps.LA302 = new Array(54.36136, -1.93254, 10);         //Richmondshire
		LAMaps.LA303 = new Array(54.21041, -0.85065, 10);         //Ryedale
		LAMaps.LA304 = new Array(54.36634, -0.6338, 10);         //Scarborough
		LAMaps.LA305 = new Array(53.78689, -1.13313, 10);         //Selby
		//Sctoland
		LAMaps.LA402 = new Array(57.15736, -2.18542, 10);         //Aberdeen City
		LAMaps.LA403 = new Array(57.24176, -2.62945, 10);         //Aberdeenshire
		LAMaps.LA404 = new Array(56.72492, -2.91861, 10);         //Angus
		LAMaps.LA405 = new Array(56.1224, -5.49797, 10);         //Argyll & Bute
		LAMaps.LA406 = new Array(55.57636, -2.78641, 10);         //Scottish Borders
		LAMaps.LA407 = new Array(56.15212, -3.73916, 10);         //Clackmannanshire
		LAMaps.LA408 = new Array(55.97892, -4.50795, 10);         //West Dunbartonshire
		LAMaps.LA409 = new Array(55.07727, -3.95919, 10);         //Dumfries & Galloway
		LAMaps.LA410 = new Array(56.47823, -2.96853, 10);         //Dundee City
		LAMaps.LA411 = new Array(55.48697, -4.31279, 10);         //East Ayrshire
		LAMaps.LA412 = new Array(55.95603, -4.22398, 10);         //East Dunbartonshire
		LAMaps.LA413 = new Array(55.93986, -2.71908, 10);         //East Lothian
		LAMaps.LA414 = new Array(55.75324, -4.35844, 10);         //East Renfrewshire
		LAMaps.LA415 = new Array(55.92423, -3.28753, 10);         //Edinburgh, City of
		LAMaps.LA416 = new Array(55.99531, -3.79668, 10);         //Falkirk
		LAMaps.LA417 = new Array(56.22868, -3.12554, 10);         //Fife
		LAMaps.LA418 = new Array(55.85801, -4.25194, 10);         //Glasgow City
		LAMaps.LA419 = new Array(57.58027, -4.75564, 10);         //Highland
		LAMaps.LA420 = new Array(55.90125, -4.74251, 10);         //Inverclyde
		LAMaps.LA421 = new Array(55.83219, -3.10914, 10);         //Midlothian
		LAMaps.LA422 = new Array(57.47481, -3.26371, 10);         //Moray
		LAMaps.LA423 = new Array(55.65132, -4.97799, 10);         //North Ayrshire
		LAMaps.LA424 = new Array(55.87457, -3.94672, 10);         //North Lanarkshire
		LAMaps.LA425 = new Array(59.02532, -3.04261, 10);         //Orkney Islands
		LAMaps.LA426 = new Array(56.5675, -3.81815, 10);         //Perth & Kinross
		LAMaps.LA427 = new Array(55.84568, -4.54269, 10);         //Renfrewshire
		LAMaps.LA428 = new Array(60.38085, -1.25504, 10);         //Shetland Islands
		LAMaps.LA429 = new Array(55.26776, -4.68689, 10);         //South Ayrshire
		LAMaps.LA430 = new Array(55.61304, -3.80974, 10);         //South Lanarkshire
		LAMaps.LA431 = new Array(56.24492, -4.33753, 10);         //Stirling
		LAMaps.LA432 = new Array(55.8843, -3.5733, 10);         //West Lothian
		LAMaps.LA433 = new Array(57.93668, -6.83671, 10);         //Eilean Siar
		//Wales
		LAMaps.LA115 = new Array(53.28548, -4.37318, 10);         //Isle of Anglesey
		LAMaps.LA116 = new Array(52.89457, -4.02339, 10);         //Gwynedd
		LAMaps.LA117 = new Array(53.1407, -3.73522, 10);         //Conwy
		LAMaps.LA118 = new Array(53.09189, -3.35298, 10);         //Denbighshire
		LAMaps.LA119 = new Array(53.21763, -3.162, 10);         //Flintshire
		LAMaps.LA120 = new Array(52.98996, -3.0164, 10);         //Wrexham
		LAMaps.LA121 = new Array(52.32518, -3.40848, 10);         //Powys
		LAMaps.LA122 = new Array(52.25559, -4.071, 10);         //Ceredigion
		LAMaps.LA123 = new Array(51.85341, -4.886, 10);         //Pembrokeshire
		LAMaps.LA124 = new Array(51.89937, -4.16153, 10);         //Carmarthenshire
		LAMaps.LA125 = new Array(51.64115, -4.05999, 10);         //Swansea
		LAMaps.LA126 = new Array(51.67935, -3.74221, 10);         //Neath Port Talbot
		LAMaps.LA127 = new Array(51.55539, -3.59917, 10);         //Bridgend
		LAMaps.LA128 = new Array(51.44429, -3.39803, 10);         //The Vale of Glamorgan
		LAMaps.LA129 = new Array(51.64999, -3.43446, 10);         //Rhondda, Cynon, Taff
		LAMaps.LA130 = new Array(51.74009, -3.35991, 10);         //Merthyr Tydfil
		LAMaps.LA131 = new Array(51.65289, -3.20373, 10);         //Caerphilly
		LAMaps.LA132 = new Array(51.76632, -3.19257, 10);         //Blaenau Gwent
		LAMaps.LA133 = new Array(51.69817, -3.05219, 10);         //Torfaen
		LAMaps.LA134 = new Array(51.75765, -2.86629, 10);         //Monmouthshire
		LAMaps.LA135 = new Array(51.57305, -2.96081, 10);         //Newport
		LAMaps.LA136 = new Array(51.50796, -3.19828, 10);         //Cardiff
		//N Ireland
		LAMaps.LA376 = new Array(54.69596, -6.25354, 10);         //Antrim
		LAMaps.LA377 = new Array(54.52178, -5.615, 10);         //Ards
		LAMaps.LA378 = new Array(54.32049, -6.62321, 10);         //Armagh
		LAMaps.LA379 = new Array(54.89151, -6.24282, 10);         //Ballymena
		LAMaps.LA380 = new Array(55.04873, -6.41858, 10);         //Ballymoney
		LAMaps.LA381 = new Array(54.33143, -6.16376, 10);         //Banbridge
		LAMaps.LA382 = new Array(54.60262, -5.9405, 10);         //Belfast
		LAMaps.LA383 = new Array(54.7418, -5.81899, 10);         //Carrickfergus
		LAMaps.LA384 = new Array(54.54929, -5.85111, 10);         //Castlereagh
		LAMaps.LA385 = new Array(55.0682, -6.68221, 10);         //Coleraine
		LAMaps.LA386 = new Array(54.64341, -6.70979, 10);         //Cookstown
		LAMaps.LA387 = new Array(54.47959, -6.3924, 10);         //Craigavon
		LAMaps.LA388 = new Array(54.94876, -7.20834, 10);         //Derry
		LAMaps.LA389 = new Array(54.33114, -5.79673, 10);         //Down
		LAMaps.LA390 = new Array(54.45907, -6.93572, 10);         //Dungannon
		LAMaps.LA391 = new Array(54.35156, -7.6397, 10);         //Fermanagh
		LAMaps.LA392 = new Array(54.8756, -5.91188, 10);         //Larne
		LAMaps.LA393 = new Array(54.98757, -6.93417, 10);         //Limavady
		LAMaps.LA394 = new Array(54.50332, -6.08446, 10);         //Lisburn
		LAMaps.LA395 = new Array(54.81338, -6.67128, 10);         //Magherafelt
		LAMaps.LA396 = new Array(55.14728, -6.23695, 10);         //Moyle
		LAMaps.LA397 = new Array(54.15195, -6.28712, 10);         //Newry and Mourne
		LAMaps.LA398 = new Array(54.72357, -5.98011, 10);         //Newtownabbey
		LAMaps.LA399 = new Array(54.64458, -5.71871, 10);         //North Down
		LAMaps.LA400 = new Array(54.5924, -7.275, 10);         //Omagh
		LAMaps.LA401 = new Array(54.76239, -7.42653, 10);         //Strabane
		//////////////////////////
		
	}
	
	/**
	* Load and show the national summary panel for the current overlay
	* @param {String} iframeName ID/name of an existing <iframe>
	* @param {String} file filename to load
	*/
	function nationalSummary(iframeName,file) {
		$(".notes-data").hide();
		$("#" + iframeName).show();
		frames[iframeName].location.href = file;
	}
	
	/**
	* Pan and zoom in to a particular district
	* @param {Float} lat destination latitude
	* @param {Float} lng destination longitude
	* @param {Int} distZoom zoom level
	*/
	function panToLocation(lat, lng, distZoom) {
		map.setCenter(new GLatLng(parseFloat(lat), parseFloat(lng)), parseInt(distZoom, 10));
	}
	
	/**
	* Callback function to center map on a point
	* @param {GLatLng} destination
	*/
	function setCenterToPoint(point) {
		map.setCenter(point, 10);
	}
	
	/**
	* Load a new overlay
	* @param {String} name overlay title
	* @param {String} url directory of map tiles
	*/
	function setCurrentIndicator(name, url) {
		currentIndicatorName = name;
		currentIndicatorURL = url;
		loadContent();
	}
	
	/**
	* Remove the existing overlay and load the new one
	* @param {String} baseName name of the new overlay or "" to just remove
	*/
	function setDataSource(baseName) {
		var old_pmapType = map.getMapTypes()[0];
		if (baseName !== "") {
			if (map.getZoom() > maxZoomLevels[baseName]) {
				map.setZoom(maxZoomLevels[baseName]);
			}
			normalTileLayers[1].baseName = baseName;
			normalTileLayers[1].mapBounds = mapBounds;
			var p_customMap = new GMapType(
				normalTileLayers,
				new GMercatorProjection(18),
				p_buttonText,
				{
					errorMessage: "Data not available"
				}
			);
			p_customMap.getMaximumResolution = function(latlng){ return maxZoomLevels[baseName]; };
			p_customMap.getMinimumResolution = function(latlng){ return minZoomLevel; };
			map.addMapType(p_customMap);
		}
		map.removeMapType(old_pmapType); // fires update of zoom controls, so needs to go last
	}
	
	/**
	* Remove the existing overlay and load the new one
	* @param {String} baseName name of the new overlay or "" to just remove
	*/
	
	/**
	* If there's a real value in the postcode box, do something with it
	* (#postcode[alt] contains the default text, "Town / place")
	*/
	function submitPostcode() {
		var postcode = $("#postcode").attr("value");
		var alt = $("#postcode").attr("alt");
		if (postcode != alt) {
			usePointFromPostcode(postcode, setCenterToPoint );
		}
	}
	
	/**
	* Use Google Local search for postcode lookup
	* @param {String} postcode
	* @callbackFunction {Function} (probably setCenterToPoint())
	*/
	function usePointFromPostcode(postcode, callbackFunction) {
		var localSearch = new GlocalSearch();
		localSearch.setSearchCompleteCallback(null, function() { 
			if (localSearch.results[0]) {
				var resultLat = localSearch.results[0].lat;
				var resultLng = localSearch.results[0].lng;
				var point = new GLatLng(resultLat,resultLng);
				callbackFunction(point);
			} else {
				alert("Postcode not found!");
			}
		});
		localSearch.execute(postcode + ", UK");
	}
	
	/**
	* Load the content for an overlay from text file and build the sidebar
	* @param {String} file directory and stub of the filename (no extension)
	*/
	function writeLegend(file) {
		if (file === "" || file === "tiles/" || file === "legend") {
			return;
		} else {
			var process_legend = function(legend_file) {
				$("#sidebar-content").html(legend_file);
				changeLegendOpacity('scale_colour');
				handleResize();
				$(".notes-data").wrapInner('<div class="wrapper"></div>').append('<a class="close">close</a>').addClass("panel").appendTo("#content");
				$(".notes-data a.close").click(function() {
					$(".notes-data").hide();
				});
				$("#sidebars-wrapper").animate({ left: "-310px" }, 500);
			};
			var filename = "key/" + file + ".txt";
			GDownloadUrl(filename, process_legend);
		}
	}
	
	/**
	* Load the national summary content from a HTML file
	* @param {String} file directory and stub of the filename (no extension)
	*/
	function writeSummary(file) {
		if (file === "" || file === "tiles/" || file === "legend") {
			return;
		} else {
			var processSummary = function(summary_file) {
				$("#nationalSummaryContent").children().not("a.close").remove();
				var summary_body = summary_file.split(/<\/?body[^>]*>/)[1];
				$(summary_body).prependTo("#nationalSummaryContent");
			};
			var filename = 'key/' + currentIndicatorURL + '_summary.html';
			GDownloadUrl(filename, processSummary);
		}
	}
	
	/*
	* jQuery helper function: sort out hints in text inputs
	*/
	$.fn.fieldHint = function() {
		this.each(function() {
			$(this).focus(function(){
				if($(this).val() == $(this).attr("alt")) {
					$(this).val("");
				}
			}).blur(function() {
				if($(this).val() == $(this).attr("alt") || !$(this).val().length) {
					$(this).val($(this).attr("alt"));
				}
			}).blur();
		});
		return this;
	};
	
	/* Stuff to do on page load */
	
	$(function() {
		init();
	});
	
	//--------------------------
	this.showKML = function() {
	    var temp = $("#uploadKMLurl").val();
	    hnm.addKMLOverLay(temp);
	    if (totalKML > 5) {
	        alert("Sorry but only 5 KML files are allowed at one time.");
	    }
	}

	this.removeKML = function() {
	    //var temp = $("#uploadKMLurl").val();
	    hnm.removeKMLOverLay();

	}

	//add KML overlay
	this.addKMLOverLay = function(kmlurl) {
	    if (kmlurl.substring(0, 6) != "http://") {
	        kmlurl = "http://" + kmlurl;
	    }
	    KMLURLArray[totalKML] = new GGeoXml(kmlurl); ;

	    map.addOverlay(KMLURLArray[totalKML]);
	    //pan to the center of KML
	    KMLURLArray[totalKML].gotoDefaultViewport(map);
	    totalKML = totalKML + 1;
		$("#instructions").hide();
	}

	// remove KML overlay
	this.removeKMLOverLay = function() {
	    alert("Your KML overlays have been removed");
	    for (var j = 0; j < totalKML; j++) {
	        map.removeOverlay(KMLURLArray[j]);
	        KMLURLArray[j] = null;
	    }
	    totalKML = 0;
	}
	//-----------------------------


	$(function() //display local authority layer
	{
		$("#boundaryDisplay").change(function() {
			if ($(this).attr('checked')) {
				//add overlayer
				map.addOverlay(myTileLayer_LA);
				$("#boundaryDisplay").css("background-color", "#99B3CC");
			} else {
				//remove overlay
				map.removeOverlay(myTileLayer_LA);
				$("#boundaryDisplay").css("background-color", "");
			}
		});
	});

	$(function() //display urban area boundary layer
	{
		$("#UrbanDisplay").toggle(function() {
			//add overlayer
			map.addOverlay(myTileLayer_UA);
			$("#UrbanDisplay").css("background-color", "#99B3CC");
		}, function() {
			//remove overlay
			map.removeOverlay(myTileLayer_UA);
			$("#UrbanDisplay").css("background-color", "");

		});
	});
        
}

// Create an instance of the HNM object
var hnm = new HNM();

