/*
<script type="text/javascript" charset="utf-8" src="scripts/map.js"></script>
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAnjsnT5IyXvTXQDNyZtFo9xSc28Pld55Ezt5FtH30zrMTlStcWBQg8DuUBm0BNxYcy0ie0M-xAI8ykA" type="text/javascript"></script>
<link rel="stylesheet" href="css/map.css" type="text/css" charset="utf-8" />
        
<div id="map" style="width: 380px; height: 350px;">&nbsp;</div>
        
<script type="text/javascript" charset="utf-8" defer>
initGoogleMap();
setTimeout('highlightMapLocation(1)', 1);
</script>
*/

var map;
var geocoder;

var markerMngr;

function initGoogleMap() {
  if (!GBrowserIsCompatible())
    return false;
  
  // setup map  
  map = new GMap2(document.getElementById("map"));
  map.addControl(new GMapTypeControl()); // map/satelite/hybrid buttons
  map.addControl(new GLargeMapControl()); // pan buttons/zoom slider
  map.addControl(new GScaleControl());
  map.setCenter(new GLatLng(-28, 134), 4);
  window.onunload = GUnload;
  
  // add markers
  markerMngr = new GMarkerManager(map);
  for (var i = 0; i < mapLocations.length; i++) {
    var locRec = mapLocations[i];
    markerMngr.addMarker(createMarker(locRec), locRec.minZoom, locRec.maxZoom);
  }
  
  return true;
}

function grabLatLongFromClick(overlay, point)
{
  alert('clicked on: ' + point);
}

function fetchMarkers()
{
  var markers = new Array();
  
  for (var i= 0; i < mapLocations.length; i++) {
    locRec = mapLocations[i];
    
    if (locRec.lat == 0)
      continue;
    
    markers.push(createMarker(locRec));
  }
  
  return markers;
}

function createMarker(locRec)
{
  var marker = new GMarker(new GLatLng(locRec.lat, locRec.lng));
  
  if (locRec.action == 'zoom-in') {
    GEvent.addListener(marker, "click", function() { map.setCenter(new GLatLng(locRec.lat, locRec.lng), 8) });
  } else if (locRec.action == 'show-desc') {
    var descHtml = '<div class="map-info-desc"><strong>' + locRec.title + '</strong><p>' + locRec.desc + '</p><br clear="all" /></div>';
    GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(descHtml); });
  }
  
  locRec.marker = marker;
  return marker;
}

function locationFromAddress(address)
{
  if (!geocoder)
    geocoder = new GClientGeocoder();
  
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point)
        alert(address + 'not found');
      else
        alert('lat/lng: ' + point);
    }
  );
}

function findLocationWithId(locId)
{
  for (var i= 0; i < mapLocations.length; i++) {
    if (mapLocations[i].locId == locId)
      return mapLocations[i];
  }
  
  return 0;
}

function highlightMapLocation(locId)
{
  locRec = findLocationWithId(locId);
  if (locRec == 0)
    alert('Sorry, could not find that location on the map.');
  
  locRec.marker.openInfoWindowHtml(locRec.marker.tts_descriptionHtml);
}
