
//Google Maps: Garry Harstad
var geocoder = new GClientGeocoder(); //for converting an address string


//<![CDATA[
//lat=North up/down
//GLatLng(37.4419, -122.1419)lat, lon   zoom:13
var map="";
function loadmapdata(lat,lon,containerid,zoom)
{
  var mymarkerhtml="";
  if (GBrowserIsCompatible())
  {
    map = new GMap2(document.getElementById(containerid)); map.addControl(new GLargeMapControl()); map.addControl(new GMapTypeControl()); map.setCenter(new GLatLng(lat, lon),zoom);;
    map.setMapType(G_NORMAL_MAP); //OPTIONS:G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP 
    //GEvent.addListener(map, "dblclick", function() { centeronaddress("map_info"); });
    //GEvent.addListener(map,"click",showAddress);
    //showAddress(); 
    //centeronaddress('map_info');
    //addMarker(map,lat,lon,mymarkerhtmlGoesHERE)
    return map;
  }
}


// Initializes a google map for a single cemetery
function initcemeterymap(lat,lon,containerid,zoom,markerhtml)
{
  //alert(markerhtml)
  var mymapobj=loadmapdata(lat,lon,containerid,zoom);
  //if(markerhtml != undefined) { markerhtml=null; }
  addMarker(mymapobj,lat,lon,markerhtml);
}

// Initializes a google map for all cemeteries passed in by a string formatted "cemeteryid~lat~lon|cemeteryid~lat~lon...."
//looks for an HTML div to grab innerHTML from and place as the map marker content. The Div is assumed to be named (ID'd) as cemmarker[cemeteryid] E.g. "cemmarker24" For cemeteryID 24
function initcemeteriesmap(latlonstring,containerid,zoom)
{
  //alert(typeof latlonstring)
  if(latlonstring.length <= 0) { return; }
  var latlonarray=new Array();
  var thiscemarray=new Array();
  var cemid,lat,lon=0
  var markercontentid="";
  var mymapobj=null;
  var markerhtml="";
  
  latlonarray=latlonstring.split("|");
  //alert("latlonarray="+latlonarray)
  for(var i=0; i<latlonarray.length; i++)
  {
    thiscemarray=latlonarray[i].split("~");
    cemid=thiscemarray[0];
    lat=parseFloat(thiscemarray[1]);
    lon=-parseFloat(thiscemarray[2]);//values stored are actually GPSWest so that converts to (-GPSWEST=Longitude)
    markercontentid="cemmarker"+cemid;
    markerhtml=document.getElementById(markercontentid).innerHTML;
    if(i == 0)
    {
      mymapobj=loadmapdata(41.59645,-71.08233,containerid,zoom);
      //alert("lat="+lat+"\nlon="+lon+"\ncontainerid="+containerid)
    }
    addMarker(mymapobj,lat,lon,markerhtml);
  }
}


function centeronaddress(containerid)
{
  //if(typeof containerid=="undefined") { var containerid="map_info"; }
  map.openInfoWindow(map.getCenter(),document.getElementById(containerid));
}

function showAddress()
{
  var address=document.mapaddressform.mapaddress.value;
  //alert(address);
  geocoder.getLatLng(address,
  function(point)
  {
    if (!point) { alert(address + " not found"); }
    else
    {
      map.setCenter(point, 16);
      var marker = new GMarker(point);
      addOverlay(marker);
      marker.openInfoWindowHtml(address);
    }
  } );
  setTimeout("centeronaddress('map_info')",600);
}


/*
marker.openInfoWindowHtml(address);
*/

function createMarker(point,html)
{
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(html); });
  return marker;
}

function addMarker(mapobj,lat,lon,html)
{
  point = new GLatLng(lat,lon);
  
  if(html != "undefined" && html != "")
  {
    marker=createMarker(point, html)
  }
  else
  {
    marker=createMarker(point,'<div style="width:200px; height:100px; border:1px solid orange; background-color:lightyellow;">HELLO</div>');
  }
  
  //marker=createMarker(point,'<div style="width:200px; height:100px; border:1px solid orange; background-color:lightyellow;">as fsa fasf' + html + '</div>')
  mapobj.addOverlay(marker);
}
/*

function showcemeterymapinfo()
{
    var mapinfodiv=document.getElementById("map_info");
    if(mapinfodiv!=null)
    {
      map.openInfoWindow(map.getCenter(),mapinfodiv);
    }
}

*/

//]]>
