
if (GBrowserIsCompatible()) {

  var n=0;
  var map;
  var gdir;
  var gmarkers = [];
  var gmarkersShow = [];
  var derbyInfo = "<img src='DerbyHouseSm84.jpg' align='left' style='padding-bottom: 15px;'><br>Derby Lane Cottage<br>6 Derby Lane<br>207.288.9483<br>"
  
  var icon = new GIcon();
  icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
  icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
  icon.iconSize = new GSize(12, 20);
  icon.shadowSize = new GSize(22, 20);
  icon.iconAnchor = new GPoint(6, 20);
  icon.infoWindowAnchor = new GPoint(5, 1);      

  iconblue = new GIcon(icon,"http://labs.google.com/ridefinder/images/mm_20_blue.png"); 
  icongreen = new GIcon(icon,"http://labs.google.com/ridefinder/images/mm_20_green.png"); 
  iconyellow = new GIcon(icon,"http://labs.google.com/ridefinder/images/mm_20_yellow.png"); 


  function createMarker(point,name,address,phone,url,logo,icon) {
    var marker = new GMarker(point, {icon:icon});

  	var html = '';
    if (logo != '') {
      html += '<img src="userimg/' + logo + '" height="70" align="left">';
    }
    if (name != '') {
      if (url != '') {
        html += '<a href="' + url + '" target="_blank" style="color: black; font-weight: bold;">';
      }
    	html += name;
      if (url != '') {
      	html += '</a>';
      }
    	html += '<br>';
    }
    if (address != '') {
      html += address + '<br>';
    }
    if (phone != '') {
      html += phone;
    }
    
    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(html);
    });
    
    GEvent.addListener(marker, "mouseover", function() {
      marker.openInfoWindowHtml(html);
    });
    
    return marker;
  }

  // read the markers from the XML
  GDownloadUrl("markers.xml", function (doc) {
    var xmlDoc = GXml.parse(doc);
    var markers = xmlDoc.documentElement.getElementsByTagName("marker");

    for (var i = 0; i < markers.length; i++) {
      
      // obtain the attribues of each marker
      var cat = markers[i].getAttribute("cat");
      var name = markers[i].getAttribute("name");
      var address = markers[i].getAttribute("address");
      var phone = markers[i].getAttribute("phone");
      var url = markers[i].getAttribute("url");
      var logo = markers[i].getAttribute("logo");
      var lat = parseFloat(markers[i].getAttribute("lat"));
      var lng = parseFloat(markers[i].getAttribute("lng"));
      var point = new GLatLng(lat,lng);
      
      if (typeof(gmarkers[cat])=='undefined') {
      	gmarkers[cat] = [];
      	gmarkersShow[cat] = 0;
      }
      if (cat==0) {
        var marker = createMarker(point,name,address,phone,url,logo,iconblue);
      } else {
        var marker = createMarker(point,name,address,phone,url,logo,icon);
      }
      gmarkers[cat].push(marker);
    }

    // Display the map, with some controls and set the initial location 

    map = new GMap2(document.getElementById("map"));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.enableGoogleBar();
    //GEvent.addListener(map, "moveend", showMarkers);
    //GEvent.addListener(map, "zoomend", showMarkers);
    gdir = new GDirections(map, document.getElementById("map_directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    GEvent.addListener(gdir, "error", handleErrors);
    map.setCenter(new GLatLng(44.387207, -68.203681), 15);

    var mm = new GMarkerManager(map, {borderPadding:1});
    GEvent.addListener(mm, "changed", showMarkers);
    for (var i=0;i<gmarkers.length;i++) {
      mm.addMarkers(gmarkers[i],0,17);
    }
    mm.refresh();
    var delay = mm.getMarkerCount();
    gmarkersShow[0] = 1;
    showMarkers();
    gmarkers[0][0].openInfoWindow(derbyInfo);
  });
} else {
  alert("Sorry, the Google Maps API is not compatible with this browser"); // zzz show static map
}

function setDirections(fromAddress, toAddress, locale) {
  gdir.load("from: " + fromAddress + " to: " + toAddress, { "locale": locale });
}

function onGDirectionsLoad(){ 
  // Use this function to access information about the latest load()
  // results.
  // e.g.
  // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
  // and yada yada yada...
}

function handleErrors() {
  if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS) {
    alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
  } else if (gdir.getStatus().code == G_GEO_SERVER_ERROR) {
    alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
  } else if (gdir.getStatus().code == G_GEO_MISSING_QUERY) {
    alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
  } else if (gdir.getStatus().code == G_GEO_BAD_KEY) {
   alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
  } else if (gdir.getStatus().code == G_GEO_BAD_REQUEST) {
    alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
  } else {
  	alert("An unknown error occurred.");
  }
}

function toggleMarkers(type, status) {
	gmarkersShow[type] = (status?1:0);
  if ( ( (type==3) || (type==5) ) && status) {
  	map.panTo(gmarkers[0][0].getLatLng());
  	map.setZoom(12);
  	gmarkers[0][0].openInfoWindow(derbyInfo);
  }
  showMarkers();
}

function showMarkers() {
	//alert ('showMarkers' + arguments.length);
  for (var i=0;i<gmarkers.length;i++) {
  	for (var j=0;j<gmarkers[i].length;j++) {
  		gmarkers[i][j].closeInfoWindow();
    	if (gmarkersShow[i]==1) {
    		gmarkers[i][j].show();
    	} else {
    		gmarkers[i][j].hide();
    	}
    }
  }
}
