var globalMap
	var globalNumberOfMarkers
	var globalExistingPolyline
	// variables for journey segments
	jSegments = new Array()
	var indexHTML
	
	function SymError()
	{
		// document.getElementById("map").innerHTML = "<br><h3><strong>Google Maps is currently too busy to deal with this request.  Please try later.</strong></h3>"
		// return true;
	}
	
	 window.onerror = SymError;	// only put this in when we're sure it's working!
	
	function showRoute(locNo) {
		// let's just check that we're actually getting here without breaking anything!  :-)
		// alert(locNo) 	// Yes, we are.  :-)
		
		myPoints = new Array()

		myPoints = decodePoints(theDirStr[locNo])
		linePoints = new Array()
		var points = [];
   		for (f=0; f<myPoints.length; f+=2) {
        	points.push(new GPoint(myPoints[f+1],myPoints[f]));
      	}
		if (globalExistingPolyline) {
			globalMap.removeOverlay(globalExistingPolyline)
		}
      // points.sort(function(p1, p2) { return p1.x - p2.x; });
	  
	  globalExistingPolyline = new GPolyline(points,"#9900ff",4,90)
	  
	  // 22 Feb 2007 - discovered that this next line is causing an error - not sure why.  Commented out
	  // for now - page just shows text directions without polyline indicating route.  Not sure when this started. 
	  // LATER: But it seems to have sorted itself out now...  Weird.
	  
      globalMap.addOverlay(globalExistingPolyline);
	  
	  
	  
	  // save the index HTML
	  
	  
	  directionsHTML = "<p><A HREF='javascript:putIndexBack()'>Return to index</A></p>"
	  
	  // Now.  Let's dig out the text for the directions.
	  for (var k=0; k<jSegments[locNo-1].length; k++) {
	  	tempDist = jSegments[locNo-1][k].getAttribute("distance")
		directionsHTML += (k+1) + ": " + jSegments[locNo-1][k].firstChild.nodeValue + "(" + tempDist + "les)<BR><br>"
	  }
	  document.getElementById("panel").innerHTML = directionsHTML
	  
	}
	
	
	function putIndexBack() {
		document.getElementById("panel").innerHTML = indexHTML
	}
	
	
	
	
    //<![CDATA[

	// Open the info box for the specified marker.
	function popup( i ) {
    	marker[i].openInfoWindowHtml( infoHtmls[i] );
    }

	function makePopupCaller( i ) {
    	return function() { popup( i ); };
    }
	
	// decode the Route String
	// Converted to Javascript by R.C.Lockwood 7 July 2005 from the original Python 
	// Original Python function written by Matt King
	// Available at:http://www.thrall.net/~mking/maps/points.py
	function decodePoints(points) {
		blank = new Array()
		if (!(points)) { return blank }
		
		Ch = points.length
		pb = 0
		locations = new Array()
		Ka = 0
		Pa = 0
		while (pb < Ch) {
			oc = 0
			Fa = 0
			tempVar = true
			while (tempVar) {
				ub = points.charCodeAt( pb ) - 63
				pb += 1
				Fa |= (ub&31)<<oc
	            oc += 5
				if (ub < 32) {tempVar = false}
			}
			if (Fa & true) {
				i = ~(Fa>>1)
			}
			else {
				i = (Fa>>1)
			}
			Ka = Ka + i
			locations[locations.length] = (Ka*1.0E-5)
			
			
			oc = 0
			Fa = 0
			tempVar = true
			while (tempVar) {
				ub = points.charCodeAt( pb ) - 63
				pb += 1
				Fa |= (ub&31)<<oc
	            oc += 5
				if (ub < 32) {tempVar = false}
			}
			if (Fa & true) {
				i = ~(Fa>>1)
			}
			else {
				i = (Fa>>1)
			}
			Pa = Pa + i
			locations[locations.length] = (Pa*1.0E-5)
			
		
		}
		
		return locations
	}
	////////////////////////////////////////////////////
	theDirStr = new Array() // routefinder strings.
	
	var thePanel
	
    function onLoad() {
	
	  thepanel = document.getElementById("panel")
	  // Center the map
      var map = new GMap(document.getElementById("map"));
	  globalMap = map
      map.addControl(new GLargeMapControl());
      map.addControl(new GMapTypeControl());
      map.centerAndZoom(new GPoint(-1.766489, 53.627299), 6);
	   
	  // create a new base icon
	  var baseIcon = new GIcon();
	  baseIcon.image = "community/lm_gen.png";
	  baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
	  baseIcon.iconSize = new GSize(20, 34);
	  baseIcon.shadowSize = new GSize(37, 34);
	  baseIcon.iconAnchor = new GPoint(10,34)
	  baseIcon.infoWindowAnchor = new GPoint(5,14)
	  
	  // Marker icon graphics courtesy of Jared on the Google Maps API group.
	 
	  // specify a new icon
	  var icon1 = new GIcon(baseIcon);
	  icon1.image = "http://www.ashescommonfarm.co.uk/community/lm_gen.png";
	  
	  var icon2 = new GIcon(baseIcon);
	  icon2.image = "http://www.ashescommonfarm.co.uk/general/lm_gen.png";
	  
	  var icon3 = new GIcon(baseIcon);
	  icon3.image = "http://www.ashescommonfarm.co.uk/listing/lm_gen.png";
	  
	  var icon4 = new GIcon(baseIcon);
	  icon4.image = "http://www.ashescommonfarm.co.uk/restaurant/lm_gen.png";
	  
	  var icon5 = new GIcon(baseIcon);
	  icon5.image = "http://www.ashescommonfarm.co.uk/worship/lm_gen.png";
	  
	  var icon6 = new GIcon(baseIcon);
	  icon6.image = "http://www.ashescommonfarm.co.uk/shopping/lm_gen.png";
	  
	  var icon7 = new GIcon(baseIcon);
	  icon7.image = "http://www.ashescommonfarm.co.uk/school/lm_gen.png";
	  
	  // small icons
	  smallIconBase = "http://www.ashescommonfarm.co.uk/"
	  smallIconFile = "/lm.png"
	  smIcon = new Array()
	  smIcon[1] = smallIconBase + "community" + smallIconFile
	  smIcon[2] = smallIconBase + "general" + smallIconFile
	  smIcon[3] = smallIconBase + "listing" + smallIconFile
	  smIcon[4] = smallIconBase + "restaurant" + smallIconFile
	  smIcon[5] = smallIconBase + "worship" + smallIconFile
	  smIcon[6] = smallIconBase + "shopping" + smallIconFile
	  smIcon[7] = smallIconBase + "school" + smallIconFile
	  category = new Array()
	  category[1] = "Ashes Common Farm"				// blue
	  category[2] = "Sport / Leisure"				// mustard
	  category[3] = "Area of general interest"		// red
	  category[4] = "Museum / gallery"				// turquoise
	  category[5] = "Eating and drinking"			// purple
	  category[6] = "Not used yet"					// yellow
	  category[7] = "Not used yet"					// light green
	  

	  
	  function createMarker(point,someName,someText,lat,lng,i,cat) {
  		var marker = new GMarker(point,eval("icon" + cat));

  		// Show this marker's index in the info window when it is clicked
  		var html = "<div style='width: 200px; font-family: verdana,arial,helvetica,sans-serif; font-size:80%;'><b>" + someName + "</b><br>" + someText + "</div>";
		
		if (i != 0) {
			html += "<div style='width: 200px; font-family: verdana,arial,helvetica,sans-serif; font-size:80%;'><A HREF='javascript:showRoute(" + i + ")'>Show directions from Ashes Farm Cottages.</div>"
		}

		
		
  		GEvent.addListener(marker, "click", function() {
  		  marker.openInfoWindowHtml(html);
		  
		  //marker.showMapBlowup(2) 
		 
  		});
			infoHtmls[i] = html
 		 return marker;
		}

	  marker = new Array()
	  infoHtmls = new Array()
      var request = GXmlHttp.create();
      request.open("GET", "geoloc7.asp", true);
      request.onreadystatechange = function() {
        if (request.readyState == 4) {
          var xmlDoc = request.responseXML;
		  
		  // Now, in here we need to get the text of the directions - pull out of the XML
		  var segments = xmlDoc.documentElement.getElementsByTagName("segments");
		  for (var j = 0; j < segments.length; j++) {
		  	jSegments[j] = segments[j].getElementsByTagName("segment")		  
		  }
		 
		  
          var markers = xmlDoc.documentElement.getElementsByTagName("marker");
		  //alert(markers.length)
          for (var i = 0; i < markers.length; i++) {
		  	myLng = parseFloat(markers[i].getAttribute("lng"))
			myLat = parseFloat(markers[i].getAttribute("lat"))
			theName = markers[i].getAttribute("name")
			theDesc = markers[i].getAttribute("desc")
			theCat = markers[i].getAttribute("categ")
			//alert(theDesc)
            var point = new GPoint(myLng,myLat);
            marker[i] = createMarker(point,theName,theDesc,myLat,myLng,i,theCat);
            map.addOverlay(marker[i]);
			  var panellink = '<a href="javascript:popup( ' + i + ' )">';
	    var panelhtml = panellink + '<img border="0" src="' + smIcon[parseInt(theCat)] + '"></a> ' + panellink + theName + '</a><br><br>';
	    thepanel.innerHTML += panelhtml;

          }
		  indexHTML = thepanel.innerHTML
		 
		  globalNumberOfMarkers = markers.length
		  for (var i = 1; i < markers.length; i++) {	// Here we're going to save the route data from the XML feed.  (Ha.)
		  	theDirStr[i] = markers[i].getAttribute("dStr")
					  
		  
		  }
		  
        }
      }
	  
	  legendHTML = ""
	  for (g = 1; g<=5; g++) {
	  	legendHTML += '<nobr><IMG SRC="' + smIcon[g] + '"> - ' + category[g]
		if (g != 5) {legendHTML += ';</nobr> '} else {legendHTML += '</nobr> '};
	  }
	  document.getElementById("legend").innerHTML = legendHTML
	  
	
      request.send(null);
		
    }
	
	
	
    //]]>
