//   http://online.itepub.net/0-596-10161-9/0596101619/googlemapshks-chp-6-sect-9.html
//    explains how to calculate the centre and zoom level to use for a set of points


function createMarker(point, type, title, text) {
  var useicon = defaulticon;
  switch(type)
  {
  case 'city':
    useicon = cityicon;
    break;
  case 'default':
    useicon = defaulticon;
    break;
  default:
    useicon = defaulticon;
  }
  
  var marker = new GMarker(point, useicon);
  GEvent.addListener(marker, "click", function() {
    marker.openInfoWindowHtml("<b>" + title + "</b><br />" + text);
  });
  return marker;
}


// See here for links to icons: http://gmapicons.googlepages.com/home  and  http://econym.googlepages.com/geicons.htm  and  http://groups.google.com/group/Google-Maps-API/web/examples-tutorials-custom-icons-for-markers?pli=1
function makeDefaultIcon() {
  var icon = new GIcon();
  // defaulticon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
  // defaulticon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
  icon.image = "/img/icons/gm/mm_20_red.png";
  icon.shadow = "/img/icons/gm/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);
  return icon;
}

function makeCityIcon() {
  var icon = new GIcon();
  // cityicon.image = "http://maps.google.com/mapfiles/kml/pal4/icon26.png";
  // cityicon.shadow = "http://maps.google.com/mapfiles/kml/pal4/icon26s.png";
  icon.image = "/img/icons/gm/icon26.png";
  icon.shadow = "/img/icons/gm/icon26s.png";
  icon.iconSize = new GSize(32, 32);
  icon.shadowSize = new GSize(56, 32);
  icon.iconAnchor = new GPoint(16, 32);
  icon.infoWindowAnchor = new GPoint(16, 0);
  return icon;
}

function addPinPointer() {
  if (GBrowserIsCompatible()) {
    var centre = map.getCenter();
    var marker = new GMarker(centre, {draggable: true});

    GEvent.addListener(marker, "dragstart", function() {
      map.closeInfoWindow();
    });

    GEvent.addListener(marker, "dragend", function() {
      var point = marker.getPoint();
      marker.openInfoWindowHtml("<p>Lat: " + point.lat() + "<br />Lon: " + point.lng() + "</p>");
    });

    map.addOverlay(marker);
  }
}

function makeMap(lat, lon, zoom) {  
  
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
    map.addControl(new GOverviewMapControl());
    map.enableContinuousZoom();
    map.enableScrollWheelZoom();
    map.setCenter(new GLatLng(lat, lon), zoom);
    map.setMapType(G_HYBRID_MAP); 
    return map;
}


