/* 
 *==================================================
 * NewPlaceToLive JavaScript Core Version 1.0.208.0
 *==================================================
 */ 

/* Uploaded Wednesday 9th July, 2008 Time: 16.00 PM
 * New features: Different Marker Icon Category 
 *				 to be displayed on  map 
 *               according to 'source'	
 * Deploy Status: FINAL
 */

var map;
var geo;
var reasons=[];
var lat = 54.622978;
var lng = -2.592773;
var zoom = 5;
var maptype = G_PHYSICAL_MAP;

//=======driving directions start=======
var gmarkers = [];
var htmls = [];
var i = 0;
var to_htmls = [];
var from_htmls = [];
//=======driving directions end========	

//Create the Icons and their respective properties

//Create an NPTL icon for the Marker
var iconNPTL = new GIcon();
iconNPTL.image = "../images/marker.png"; //Normal marker
iconNPTL.shadow = "../images/markersdw.png";  //Normal marker shadow
iconNPTL.iconSize = new GSize(40, 40);
iconNPTL.shadowSize = new GSize(40, 40);
iconNPTL.iconAnchor = new GPoint(8, 27);
iconNPTL.infoWindowAnchor = new GPoint(9, 2);
iconNPTL.infoShadowAnchor = new GPoint(18, 25);

//Create a NPTL HIP icon for the Marker
var iconNPTLHip = new GIcon();
iconNPTLHip.image = "../images/nptlho.png";	//NPTL Ordered HIP Marker
iconNPTLHip.shadow = "../images/nptlhosdw.png";	//NPTL Ordered HIP Marker shadow
iconNPTLHip.iconSize = new GSize(40, 40);
iconNPTLHip.shadowSize = new GSize(40, 40);
iconNPTLHip.iconAnchor = new GPoint(8, 27);
iconNPTLHip.infoWindowAnchor = new GPoint(9, 2);
iconNPTLHip.infoShadowAnchor = new GPoint(18, 25);

//Create a digivu icon for the Marker
var iconDigivu = new GIcon();
iconDigivu.image = "../images/nptldv.png";	//HIP uploaded onto Digi-Vu Marker
iconDigivu.shadow = "../images/nptldvsdw.png";	//HIP uploaded onto Digi-Vu Marker
iconDigivu.iconSize = new GSize(40, 40);
iconDigivu.shadowSize = new GSize(40, 40);
iconDigivu.iconAnchor = new GPoint(8, 27);
iconDigivu.infoWindowAnchor = new GPoint(9, 2);
iconDigivu.infoShadowAnchor = new GPoint(18, 25);

// === Create an associative array of GIcons() ===
var gicons = [];
gicons[""] = iconNPTL; //for any blank 'source' rows that have not been defined in the database
gicons["nptlHipOrder"] = iconNPTLHip; 
gicons["digivu"] = iconDigivu;

function load() {
	
	// ========== Read paramaters that have been passed in ==========
	
	//========================================
	
	// If there are any parameters at eh end of the URL, they will be in  location.search
	// looking something like  "?lat=50&lng=-3&zoom=10&type=h"
	
	// skip the first character, we are not interested in the "?"
	var query = location.search.substring(1);
	
	// split the rest at each "&" character to give a list of  "argname=value"  pairs
	var pairs = query.split("&");
	for (var i=0; i<pairs.length; i++) {
		// break each pair at the first "=" to obtain the argname and value
		var pos = pairs[i].indexOf("=");
		var argname = pairs[i].substring(0,pos).toLowerCase();
		var value = pairs[i].substring(pos+1).toLowerCase();
		
		// process each possible argname
		if (argname == "lat") {lat = parseFloat(value);}
		if (argname == "lng") {lng = parseFloat(value);}
		if (argname == "zoom") {zoom = parseInt(value);}
		if (argname == "maptype") {
			if (value == "m") {maptype = G_NORMAL_MAP;}
			if (value == "k") {maptype = G_SATELLITE_MAP;}
			if (value == "h") {maptype = G_HYBRID_MAP;}
			if (value == "p") {maptype = G_PHYSICAL_MAP;}
		}
	}
	
	//alert(query);
	
	// ========== Create the map using the information obtained above ========
	// ========== Use default values if no information is obtained ===========
	
	map = new GMap2(document.getElementById("map"));
	map.addMapType(G_PHYSICAL_MAP);		//Terrain Tab that is added automatically after the mapTypeControl. Displayed after Hybrid
	map.addControl(new GMapTypeControl());
	
	/*=======START: Positioning the Map control i.e pan left right, + to zoom, etc=========*/		
	var mapTypeControl = new GSmallMapControl();
	var topRight = new GControlPosition(G_ANCHOR_TOP_RIGHT, new GSize(20,40));//20,140
	map.addControl(mapTypeControl, topRight);
	/*=======END: Positioing the Map control==============*/
	
	map.addControl(new google.maps.LocalSearch(), new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(2,0))); 
	map.enableDoubleClickZoom();
	map.enableContinuousZoom();
	map.enableScrollWheelZoom();
	map.setCenter(new GLatLng(lat, lng), zoom, maptype);  //<-- Modified G_PHYSICAL_MAP with mapType defined above
	
	//========================================http://econym.googlepages.com/example_linktothis.htm ================
	// ========== This function will create the "link to this page" dynamic link ==================================
	// ========================The generated link will be used to remove the catchment area =======================
	
	if ((navigator.userAgent.indexOf('Firefox') != -1) || (navigator.userAgent.toLowerCase().indexOf("safari") > -1))
	{
		function makeLink() {
			var maplink="http://www.newplacetolive.co.uk/html/indexffsaf.html"  // Substitue for indexffsaf.html  
				+ "?lat=" + map.getCenter().lat().toFixed(8) // <- Changed from 6 to 8 for greater accuracy
			+ "&lng=" + map.getCenter().lng().toFixed(8)  // <- Changed from 6 to 8 for greater accuracy
			+ "&zoom=" + map.getZoom()
			+ "&maptype=" + map.getCurrentMapType().getUrlArg();
			document.getElementById("clearCatchment").innerHTML = '<a href="' +maplink+ '">Clear Catchment Area</a>'; 
			//a link on the main page 
			
			//alert(maplink);
		}
	}
	else {
		function makeLink() {
			var maplink="http://www.newplacetolive.co.uk" 
				+ "?lat=" + map.getCenter().lat().toFixed(8) // <- Changed from 6 to 8 for greater accuracy
			+ "&lng=" + map.getCenter().lng().toFixed(8)  // <- Changed from 6 to 8 for greater accuracy
			+ "&zoom=" + map.getZoom()
			+ "&maptype=" + map.getCurrentMapType().getUrlArg();
			document.getElementById("clearCatchment").innerHTML = '<a href="' +maplink+ '">Clear Catchment Area</a>';
			//a link on the main page
			
			//alert(maplink);
		}
	}
	
	// Make the link the first time when the page opens
	makeLink();
	
	// Make the link again whenever the map changes
	GEvent.addListener(map, 'moveend', makeLink);
	
	//========================================	
	
	
	/*Controls that might come in handy later on*/
	//map.addControl(new PromoControl());
	//map.addControl(new GLargeMapControl());
	//map.addControl(new GSmallMapControl());
	//map.addControl(new GScaleControl());
	updateMarkers();
	
	//Note: it is not necessary to trigger this event on
	//zoomend as moveend is automatically triggerd by zoomend.
	// Eliminates the flashing issue when the map is moved
	//GEvent.addListener(map,'zoomend',function() { //old zoomed function
	//		updateMarkers();
	//	});
	GEvent.addListener(map, "zoomend", function() { //new zoomed fuction, changes map to normal at low zoom level
			updateMarkers();
			changeCatchmentArea();
			var zoom ="15";
			var currentMap=G_PHYSICAL_MAP;
			if (map.getZoom() == zoom && map.getCurrentMapType() == currentMap) {
				map.setMapType(G_HYBRID_MAP);
			}
			
		}); 
	//This function causes the flashing issue!
	//GEvent.addListener(map,'moveend',function() {
	//updateMarkers();
	//});
	
	//============================= GEOCODER OVERLAY DIV =============================
	
	var pos1 = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0,0));	//var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(100,10)); 
	pos1.apply(document.getElementById("Geocoder"));
	map.getContainer().appendChild(document.getElementById("Geocoder"));
	
	//=============================
	
	//============================= OPTIONS OVERLAY DIV =============================
	
	var pos2 = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(0,100));	//var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(100,10)); 
	pos2.apply(document.getElementById("Options"));
	map.getContainer().appendChild(document.getElementById("Options"));
	
	//=============================
	
	//============================= LOCAL INFO IMAGE OVERLAY DIV =============================
	
	var pos3 = new GControlPosition(G_ANCHOR_BOTTOM_RIGHT, new GSize(35,24));	//var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(100,10)); x y system; x=horizontal y=vertical duh!
	pos3.apply(document.getElementById("Local"));
	map.getContainer().appendChild(document.getElementById("Local"));
	
	//=============================
	
	// ====== Create a Client Geocoder ======
	geo = new GClientGeocoder(); 
	
	// ====== Array for decoding the failure codes ======
	reasons[G_GEO_SUCCESS]            = "Success";
	reasons[G_GEO_MISSING_ADDRESS]    = "Missing Address: The address was either missing or had no value.";
	reasons[G_GEO_UNKNOWN_ADDRESS]    = "Unknown Address:  No corresponding geographic location could be found for the specified address.";
	reasons[G_GEO_UNAVAILABLE_ADDRESS]= "Unavailable Address:  The geocode for the given address cannot be returned due to legal or contractual reasons.";
	reasons[G_GEO_BAD_KEY]            = "Bad Key: The API key is either invalid or does not match the domain for which it was given";
	reasons[G_GEO_TOO_MANY_QUERIES]   = "Too Many Queries: The daily geocoding quota for this site has been exceeded.";
	reasons[G_GEO_SERVER_ERROR]       = "Server error: The geocoding request could not be successfully processed.";
}

function updateMarkers() {	
	
	//remove the existing points
	map.clearOverlays();
	//create the boundry for the data to provide
	//initial filtering
	
	//Get the data entered by the user from the form
	var proptype = document.getElementById("proptype").value;
	var bedrooms = document.getElementById("bedrooms").value;
	var price = document.getElementById("price").value;
	
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var getVars = 'ne=' + northEast.toUrlValue()
	+ '&sw=' + southWest.toUrlValue() 
	+ '&proptype=' + proptype
		+ '&bedrooms=' + bedrooms
		+ '&price=' + price
		
		//retrieve the points
		var request = GXmlHttp.create();
	request.open('GET', 'http://www.newplacetolive.co.uk/php/data.php?' + getVars, true);
	
	request.onreadystatechange = function() { 
		if (request.readyState == 4) {
			var jscript = request.responseText;
			var points;
			eval(jscript);
			
			//create each point from the list
			for (i in points) {
				var point = new GLatLng(points[i].lat,points[i].lng);
				//var marker = createMarker(point,points[i].html);
				//var marker = createMarker(point,points[i].type);
				
				// === read the icontype attribute ===
				var icontype = points[i].icontypeSource;  
				var marker = createMarker(point, points[i].html, points[i].type, icontype); 
				map.addOverlay(marker);
			}
		}
	}
	request.send(null);
}

//====================================================================================================================================

function createMarker(point,html,type, icontype) {  
	
	var marker = new GMarker(point, gicons[icontype]);
	
	//Driving Directions
	// The info window version with the "to here" form open
	to_htmls[i] = html + '<font face="Verdana,Arial,Helvetica, sans-serif" size="1px">' +
	'<br>Directions: <b>To here</b> - <a href="javascript:fromhere(' + i + ')">From here</a>' +
		'<br><br>Start address:<form action="http://maps.google.co.uk/maps" method="get" target="_blank">' +
		'<input type="text" SIZE=20 MAXLENGTH=60 name="saddr" id="saddr" value="" />' +
		'<INPUT value="Go" TYPE="SUBMIT"><br>(Will open in a new window)</font>' +
		'<input type="hidden" name="daddr" value="' + point.lat() + ',' + point.lng() + 
	// "(" + name + ")" + 
	'"/>';
	// The info window version with the "to here" form open
	from_htmls[i] = html + '<font face="Verdana,Arial,Helvetica, sans-serif" size="1px">' +
	'<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
		'<br><br>End address:<form action="http://maps.google.co.uk/maps" method="get"" target="_blank">' +
		'<input type="text" SIZE=20 MAXLENGTH=60 name="daddr" id="daddr" value="" />' +
		'<INPUT value="Go" TYPE="SUBMIT"><br>(Will open in a new window)</font>' +
		'<input type="hidden" name="saddr" value="' + point.lat() + ',' + point.lng() +
	// "(" + name + ")" + 
	'"/>';
	// The inactive version of the direction info
	html = html + '<font face="Verdana,Arial,Helvetica, sans-serif" size="1px">' + 
		'<br>Directions: <a href="javascript:tohere('+i+')">To here</a> - <a href="javascript:fromhere('+i+')">From here</a></font>';
	//end driving directions
	
	GEvent.addListener(marker, "click", function() {
			if (type=='c'){
				marker.openInfoWindowHtml('<font face="Verdana,Arial,Helvetica,sans-serif" size="2px">'
					+ 'There are <b>more</b> properties in<br />'
						+ ' this area.<br />'
						+ '<br />Zoom in to view more!'
						+ '</font>');
						}
			else {
				var markerHTML = html;
				marker.openInfoWindowHtml(markerHTML);
				
			}
		});
	
	
	
	
	
	/*========Driving Directions==========*/
	gmarkers[i] = marker;
	htmls[i] = html;
	i++;
	/*===================*/
	
	return marker; 
	marker = null; //new line
	markerHTML = null; //new line
}

//====================================================================================================================================

/* For driving directions which reads in the click from the link 'to here' or 'from here' from the info Window*/
function myclick(i) {
	gmarkers[i].openInfoWindowHtml(htmls[i]);
}

// functions that open the directions forms
function tohere(i) {
	gmarkers[i].openInfoWindowHtml(to_htmls[i]);
}
function fromhere(i) {
	gmarkers[i].openInfoWindowHtml(from_htmls[i]);
}

//Code from Build205.js Implemeting school catchment area
//Format name area_subArea_SchoolName_Type_School/College/etc e.g. leicestershire_subArea_Some_SchoolName_Primary_School
//Case Statement function start
function changeCatchmentArea() {
	area = document.frmCatchment.combo2.value;
	var kml;
	switch (area) {
		//Begin Leicestershire
		//Leicestershire Primary Schools
	case "leicestershire_Barwell_Infant_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Barwell_Juniors_Infants.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Newlands_Community_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Barwell_Newlands.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Sketchly_Hill_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Burbage_Sketchly_Hill.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Earl_Shilton_Weavers_Close_CoE_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Earl_Shilton_Weavers_Close.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Earl_Shilton_Townlands_CoE_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Earl_Shilton_Townlands_Primary.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Hinckley_Battling_Brook_Community_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Hinckley_Battling_Brook.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Hinckley_Holliers_Walk_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Hinckley_Holliers_Walk.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Hinckley_Richmond_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Hinckley_Richmond_Primary.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Hinckley_St_Marys_CofE_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Hinckley_St_Marys.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Sapcote_All_Saints_CoE_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Sapcote_All_Saints.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Stoney_Stanton_Manorfield_CoE_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Stoney_Stanton_Manorfield_CoE.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Burbage_Junior_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Burbage_Burbage_Junior.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Hinckley_Westfield_Junior_Primary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/primary/leicestershire-Hinckley_Westfield_Junior.kml");
		map.addOverlay(kml);
		break;
		//Leicestershire Colleges with 6th Form
	case "leicestershire_Ashby_Secondary_School_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Ashby_School.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Barwell_William_Bradford_Community_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Barwell_William_Bradford.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Leicester_The_Beauchamp_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Leicester-Beauchamp_College.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Bosworth_Community_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Bosworth_Community.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Countersthorpe_Community_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Countersthorpe_Community.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Groby_Community_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Groby_Community.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Guthlaxton_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Guthlaxton_College.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Hinckley_John_Cleveland_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Hinckley_John_Cleveland.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Hind_Leys_Community_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Hind_Leys.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Melton_Mowbray_King_Edward_VII_School_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Melton_Mowbray_King_Edward.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_King_Edward_VII_Science_and_Sport_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-King_Edward.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Longslade_Community_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Longslade_Community.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Lutterworth_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Lutterworth_Grammar.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Rawlins_Community_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Rawlins_Community.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_The_Robert_Smyth_School_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Robert_Smyth.kml");
		map.addOverlay(kml);
		break;
	case "leicestershire_Wreake_Valley_Community_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/leicestershire/college/leicestershire-Wreake_Valley.kml");
		map.addOverlay(kml);
		break;
		//END Leicestershire
		
		//Coventry
		//Begin Coventry Colleges with 6th Form
	case "coventry_Barrs_Hill_School_and_Community_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/college/coventry-Barrs_Hill.kml");
		map.addOverlay(kml);
		break;
	case "coventry_Coundon_Court_Secondary_and_Community_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/college/coventry-Coundon_Court.kml");
		map.addOverlay(kml);
		break;
	case "coventry_Ernesford_Grange_School_and_Community_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/college/coventry-Ernesford_Grange.kml");
		map.addOverlay(kml);
		break;
	case "coventry_Finham_Park_School_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/college/coventry-Finham_Park.kml");
		map.addOverlay(kml);
		break;
	case "coventry_Lyng_Hall_School_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/college/coventry-Lyng_Hall.kml");
		map.addOverlay(kml);
		break;
	case "coventry_The_Westwood_School_A_Technology_College_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/college/coventry-The_Westwood.kml");
		map.addOverlay(kml);
		break;
	case "coventry_The_Woodlands_School_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/college/coventry-The_Woodlands.kml");
		map.addOverlay(kml);
		break;
	case "coventry_Woodway_Park_School_(with_6th_Form)":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/college/coventry-Woodway_Park.kml");
		map.addOverlay(kml);
		break;
		//Coventry Secondary Schools
	case "coventry_President_Kennedy_Secondary_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/secondary/coventry-President_Kennedy.kml");
		map.addOverlay(kml);
		break;
	case "coventry_Foxford_School_and_Community_Arts_College":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/secondary/coventry-Foxford.kml");
		map.addOverlay(kml);
		break;
	case "coventry_Stoke_Park_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/secondary/coventry-Stoke_Park.kml");
		map.addOverlay(kml);
		break;
	case "coventry_Whitley_Abbey_Community_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/secondary/coventry-Whitley_Abbey.kml");
		map.addOverlay(kml);
		break;
		//Coventry Other Schools
	case "covenrty_Caludon_Castle_Business_Enterprise_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/other/coventry-Caludon_Castle.kml");
		map.addOverlay(kml);
		break;
	case "Sidney_Stringer_School":
		kml = new GGeoXml("http://www.newplacetolive.co.uk/kml/coventry/other/coventry-Sidney_Stringer.kml");
		map.addOverlay(kml);
		break;
	default:
		//	alert("Oops! Looks like there is no data for the option you selected. " 
		//		+ "Any feedback? Please use the 'contact us' link at the bottom of " 
		//		+ "the page. Thank you!");
	}
}
//end

// ===== list of words to be standardized =====
var standards = [   ["road","rd"],   
	["street","st"], 
	["avenue","ave"], 
	["av","ave"], 
	["drive","dr"],
	["saint","st"], 
	["north","n"],   
	["south","s"],    
	["east","e"], 
	["west","w"],
	["expressway","expy"],
	["parkway","pkwy"],
	["terrace","ter"],
	["turnpike","tpke"],
	["highway","hwy"],
	["lane","ln"]
];

// ===== convert words to standard versions =====
function standardize(a) {
	for (var i=0; i<standards.length; i++) {
		if (a == standards[i][0])  {a = standards[i][1];}
	}
	return a;
}

// ===== check if two addresses are sufficiently different =====
function different(a,b) {
	// only interested in the bit before the first comma in the reply
	var c = b.split(",");
	b = c[0];
	// convert to lower case
	a = a.toLowerCase();
	b = b.toLowerCase();
	// remove apostrophies
	a = a.replace(/'/g ,"");
	b = b.replace(/'/g ,"");
	// replace all other punctuation with spaces
	a = a.replace(/\W/g," ");
	b = b.replace(/\W/g," ");
	// replace all multiple spaces with a single space
	a = a.replace(/\s+/g," ");
	b = b.replace(/\s+/g," ");
	// split into words
	awords = a.split(" ");
	bwords = b.split(" ");
	// perform the comparison
	var reply = false;
	for (var i=0; i<bwords.length; i++) {
		GLog.write (standardize(awords[i])+"  "+standardize(bwords[i]))
		if (standardize(awords[i]) != standardize(bwords[i])) {reply = true}
	}
	//GLog.write(reply);
	return (reply);
}

// ====== Plot a marker after positive reponse to "did you mean" ======
function place(lat,lng) {
	var points = new GLatLng(lat,lng);
	map.setCenter(points,14); 
	map.addOverlay(new GMarker(points));
	document.getElementById("message").innerHTML = "";
}

// ====== Geocoding ======
function showAddress() {
	var search = document.getElementById("search").value;
	// ====== Perform the Geocoding ======        
	geo.getLocations(search, function (result)
		{
			//map.clearOverlays(); 
			if (result.Status.code == G_GEO_SUCCESS) {
				// ===== If there was more than one result, "ask did you mean" on them all =====
				if (result.Placemark.length > 1) { 
					document.getElementById("message").innerHTML = "Did you mean:";
					// Loop through the results
					for (var i=0; i<result.Placemark.length; i++) {
						var p = result.Placemark[i].Point.coordinates;
						document.getElementById("message").innerHTML += "<br>"+(i+1)+": <a href='javascript:place(" +p[1]+","+p[0]+")'>"+ result.Placemark[i].address+"</a>";
					}
				}
				// ===== If there was a single marker =====
				else {
					document.getElementById("message").innerHTML = "";
					var p = result.Placemark[0].Point.coordinates;
					place(p[1],p[0]);
				}
			}
			// ====== Decode the error status ======
			else {
				var reason="Code "+result.Status.code;
				if (reasons[result.Status.code]) {
					reason = reasons[result.Status.code]
				} 
				alert('Could not find "'+search+ '" ' + reason);
			}
		}
	);
}
