if (GBrowserIsCompatible()) {
	// A function to create the marker and set up the event window
	function createMarker(point, name, html1, html2, html3, label1, label2, label3) {
		var marker = new GMarker(point, iconGreen);
		// The info window version with the "to here" form open
		to_htmls[i] = html1 +'<br>Start address:<form action="javascript:getDirections()">' +
			'<input type="text" SIZE=40 MAXLENGTH=40 name="saddr" id="saddr" value="" /><br>' +
			'<INPUT value="Get Directions" TYPE="SUBMIT">' +
			'<input type="hidden" id="daddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
			'"/>';

		// The info window version with the "to here" form open
		from_htmls[i] = html1 + '<br>Directions: <a href="javascript:tohere(' + i + ')">To here</a> - <b>From here</b>' +
			'<br>End address:<form action="javascript:getDirections()">' +
			'<input type="text" SIZE=40 MAXLENGTH=40 name="daddr" id="daddr" value="" /><br>' +
			'<INPUT value="Get Directions" TYPE="SUBMIT">' +
			'<input type="hidden" id="saddr" value="'+name+"@"+ point.lat() + ',' + point.lng() +
			'"/>';

		// The inactive version of the direction info
		html1 = html1 + '<br><a href="javascript:tohere('+i+')">Directions</a>';

		GEvent.addListener(marker, "click", function() { marker.openInfoWindowTabsHtml([new GInfoWindowTab(label1,html1), new GInfoWindowTab(label2,html2), new GInfoWindowTab(label3,html3)]); } );
		// save the info we need to use later for the side_bar
		gmarkers[i] = marker;
		htmls[i] = html1;
		htmls1[i] = html1;
		htmls2[i] = html2;
		htmls3[i] = html3;
		labels1[i] = label1;
		labels2[i] = label2;
		labels3[i] = label3;

		// add a line to the side_bar html
		side_bar_html += '<a href="javascript:myclick(' + i + ')" onmouseover="mymouseover('+i+')" onmouseout="mymouseout()">' + name + '</a><br>';
		i++;
		marker.tooltip = '<div class="mapTooltip">'+name+'</div>';
		//  ======  The new marker "mouseover" and "mouseout" listeners  ======
		GEvent.addListener(marker,"mouseover", function() {
			showTooltip(marker);
		});
		GEvent.addListener(marker,"mouseout", function() {
			tooltip.style.visibility="hidden"
		});
		return marker;
	}
	// ====== This function displays the tooltip ======
	// it can be called from an icon mousover or a side_bar mouseover
	function showTooltip(marker) {
		tooltip.innerHTML = marker.tooltip;
		var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
		var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
		var anchor=marker.getIcon().iconAnchor;
		var width=marker.getIcon().iconSize.width;
		var height=tooltip.clientHeight;
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height));
		pos.apply(tooltip);
		tooltip.style.visibility="visible";
	}
	// ===== This function is invoked when the mouse goes over an entry in the side_bar =====
	// It launches the tooltip on the icon
	function mymouseover(i) {
		showTooltip(gmarkers[i])
	}
	// ===== This function is invoked when the mouse leaves an entry in the side_bar =====
	// It hides the tooltip
	function mymouseout() {
		tooltip.style.visibility="hidden";
	}
	// function to request directions =====
	function getDirections() {
		var saddr = document.getElementById("saddr").value
		var daddr = document.getElementById("daddr").value
		document.getElementById("side_bar").innerHTML = "";
		gdir.load("from: "+saddr+" to: "+daddr);
	}
	// This function picks up the click and open the correct info window
	function myclick(i) {
		gmarkers[i].openInfoWindowTabsHtml([new GInfoWindowTab(labels1[i],htmls1[i]), new GInfoWindowTab(labels2[i],htmls2[i]), new GInfoWindowTab(labels3[i],htmls3[i])]);
	}
	// functions to open the directions forms
	function tohere(i) {
		gmarkers[i].openInfoWindowHtml(to_htmls[i]);
	}
	function fromhere(i) {
		gmarkers[i].openInfoWindowHtml(from_htmls[i]);
	}
	function showMarkers(type) {
		var markerFile = "/mapData/markers.php";
		side_bar_html = "<em>"+prodDesc[type]+"</em><br />";
		if( type == undefined ) {
			selectorClause="";
		}
		else {
			selectorClause = '?selector='+type;
		}
		markerFile += selectorClause;
		map.clearOverlays();
		GDownloadUrl(markerFile, function(data) {
			var xmlDoc = GXml.parse(data);
			var markers = xmlDoc.documentElement.getElementsByTagName("marker");
			for (var i = 0; i < markers.length; i++) {
				// format data for marker
				var lat = parseFloat(markers[i].getAttribute("lat"));
				var lng = parseFloat(markers[i].getAttribute("lng"));
				var html = markers[i].getAttribute("city");
				var name = markers[i].getAttribute("farmName");
				var firstName = markers[i].getAttribute("firstName");
				var lastName = markers[i].getAttribute("lastName");
				var address = markers[i].getAttribute("address");
				var city = markers[i].getAttribute("city");
				var email = markers[i].getAttribute("email");
				var phone = markers[i].getAttribute("phone");
				var link = markers[i].getAttribute("link");
				var certText = 'Organic Certification: ';
				var cert = markers[i].getAttribute("cert");
				if ( cert == 'C') {certText += 'Call for Status'}
				else if ( cert == 'M') {certText += 'Meets All Criteria'}
				else if ( cert == 'Y') {certText += 'Certified'}
				else {certText += 'Not Specified'};
				// build product tab
				var prodTab = '';
				if ( markers[i].getAttribute("treeFruit") == 'Y') {prodTab += " Tree Fruit<br/>"};
				if ( markers[i].getAttribute("smallFruit") == 'Y') {prodTab += " Small Fruit<br/>"};
				if ( markers[i].getAttribute("veg") == 'Y') {prodTab += " Vegetables<br/>"};
				if ( markers[i].getAttribute("grainsHay") == 'Y') {prodTab += " Grains/Hay<br/>"};
				if ( markers[i].getAttribute("dairyEgg") == 'Y') {prodTab += " Dairy/Eggs<br/>"};
				if ( markers[i].getAttribute("meat") == 'Y') {prodTab += " Meat<br/>"};
				if ( markers[i].getAttribute("flowersHerbs") == 'Y') {prodTab += " Flowers/Herbs<br/>"};
				if ( markers[i].getAttribute("landCare") == 'Y') {prodTab += " Land Care<br/>"};
				if ( markers[i].getAttribute("pyoFarmStand") == 'Y') {prodTab += " PYO/Farmstand<br/>"};
				if ( markers[i].getAttribute("csa") == 'Y') {prodTab += " CSA<br/>"};
				var rawDesc = markers[i].getAttribute('description');
				var xlat = rawDesc.replace(/&lt;/gi,'<' );
				xlat = xlat.replace(/&gt;/gi,'>' );
				prodTab = "<div class='gTag'>" + prodTab + xlat + "</div>";
				// build name tab
				var nameTab = "<div class='gTag'><b>" + name + "</b> <br/>" + address + "<br/>" + city + "<br/>" + certText + "</div>";
				// build contactTab
				var niceLink = link.replace(/http:\/\//gi,'' );
//				var contactTab = "<div class='gTag'>" + firstName + "&nbsp;" + lastName + "<br /><a href=" + link + ">"+ niceLink +"</a><br/><b><a href=\"mailto:" + email + "\?subject=[NOFANHFoodmap]>"+ email +"</a><br/>" + phone + "</div>";
				var contactTab = "<div class='gTag'>" + firstName + "&nbsp;" + lastName + '<br /><a href=' + link + '>' + niceLink + '</a><br /><a href=mailto:' + email + '?subject=[NOFANHFoodmap]>'+ email +'</a><br/>' + phone + "</div>";
				// make marker
				var point = new GLatLng(lat,lng);
				var marker = createMarker(point, name, nameTab, prodTab, contactTab, 'Farm', 'Products', 'Contact');
				map.addOverlay(marker);
			}
			// put the assembled side_bar_html contents into the side_bar div
			document.getElementById("side_bar").innerHTML = side_bar_html;
		});
	};
function getParmValue(parameter) {
  var loc = location.search.substring(1, location.search.length);
  var param_value = false;

  var params = loc.split("&");
  for (i=0; i<params.length;i++) {
      param_name = params[i].substring(0,params[i].indexOf('='));
      if (param_name == parameter) {
          param_value = params[i].substring(params[i].indexOf('=')+1)
      }
  }
  if (param_value) {
      return param_value;
  }
  else {
      return false; //Here determine return if no parameter is found
  }
}
	var iconGreen = new GIcon();
	iconGreen.image = '/images/mm_20_green.png';
	iconGreen.shadow = '/images/mm_20_shadow.png';
	iconGreen.iconSize = new GSize(12, 20);
	iconGreen.shadowSize = new GSize(22, 20);
	iconGreen.iconAnchor = new GPoint(6, 20);
	iconGreen.infoWindowAnchor = new GPoint(5, 1);
	var side_bar_html = "";
	var gmarkers = [];
	var htmls = [];
	var htmls1 = [];
	var htmls2 = [];
	var htmls3 = [];
	var label1 = [];
	var label2 = [];
	var label3 = [];
	var labels1 = [];
	var labels2 = [];
	var labels3 = [];
	var mapCenter = "";
	var lat = 0;
	var lng = 0;
	var scale = 0;
	var i = 0;
	// arrays to hold variants of the info window html with get direction forms open
	var to_htmls = [];
	var from_htmls = [];
// create the map
	mapCenter = getParmValue("center");
	switch (mapCenter)
	{
		case "m":
			lat = 42.90011265525328;
			lng = -72.05108642578125;
			scale=9;
		break
		case "mv":
			lat = 43.028745251348795;
			lng = -71.32049560546875;
			scale=9;
		break
		case "sc":
			lat = 42.867912483915305;
			lng = -71.0595703125;
			scale=9;
		break
		case "ds":
			lat = 43.50672896600784;
			lng = -71.9384765625;
			scale=9;
		break
		case "lr":
			lat = 43.560491112629286;
			lng = -71.2628173828125;
			scale=9;
		break
		case "wm":
			lat = 44.268804788566165;
			lng = -71.48529052734375;
			scale=9;
		break
		case "gnw":
			lat = 44.80717308106489;
			lng = -71.279296875;
			scale=9;
		break
		default:
			lat = 43.849;
			lng = -71.644;
			scale = 8;
		break
	}
//	document.write(mapCenter);
	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(lat, lng), scale);
// ====== set up marker mouseover tooltip div ======
	var tooltip = document.createElement("div");
	map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
	tooltip.style.visibility="hidden";
// === create a GDirections Object ===
	var gdir=new GDirections(map, document.getElementById("side_bar"));
// === Array for decoding the failure codes ===
	var reasons=[];
	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 or incomplete Address:  No unique 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.";
	reasons[G_GEO_BAD_REQUEST]        = "A directions request could not be successfully parsed.";
	reasons[G_GEO_MISSING_QUERY]      = "No query was specified in the input.";
	reasons[G_GEO_UNKNOWN_DIRECTIONS] = "The GDirections object could not compute directions between the points.";
	var prodDesc=[];
	prodDesc[undefined] = "All Farms";
	prodDesc['treeFruit'] = "Tree Fruit";
	prodDesc['smallFruit'] = "Small Fruit";
	prodDesc['veg'] = "Vegetables";
	prodDesc['grainsHay'] = "Grains/Hay";
	prodDesc['dairyEgg'] = "Dairy/Eggs";
	prodDesc['meat'] = "Meat";
	prodDesc['flowersHerbs'] = "Flowers/Herbs";
	prodDesc['landCare'] = "Land Care";
	prodDesc['pyoFarmStand'] = "PYO/FarmStand";
	prodDesc['csa'] = "CSA";
// === catch Directions errors ===
	GEvent.addListener(gdir, "error", function() {
		var code = gdir.getStatus().code;
		var reason="Code "+code;
		if (reasons[code]) {
		reason = reasons[code]
	}
	alert("Failed to obtain directions, "+reason);
	});
// Create all the markers
	showMarkers();
}
else {
	alert("Sorry, Your browser cannot properly display this page.");
}
	// This Javascript is based on code provided by the
	// Blackpool Community Church Javascript Team
	// http://www.commchurch.freeserve.co.uk/
	// http://www.econym.demon.co.uk/googlemaps/