/*
<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=ABQIAAAAzM_tCjP07GKqF4wSd1yQ2hTRirFLkU_SmMJdL86RWcwjBYPFoBSj7fd26_pmhwJFCFeA5BBA9tcN0Q" 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 mapLocations = [
  {locId:  1, group: 'other', lat: -17.022545, lng: 145.457732, title: 'de Brueys Boutique Wines', link: null, img: null, desc: '189 Fichera Rd<br />Mareeba<br />Queensland 4880 Australia'}
];

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 GSmallMapControl()); // pan buttons/zoom slider
  //map.addControl(new GScaleControl());
  map.setCenter(new GLatLng(-17.022545, 145.457732), 13);
  window.onunload = GUnload;
  
  // add markers & location links
  var markers = fetchMarkers();
  for (var i = 0; i < markers.length; i++) {
    map.addOverlay(markers[i]);
  }
  
  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)
{
  // generate description html
  var imgTagHtml = (locRec.img != null) ? '<img src="images/' + locRec.img + '" />' : '';
  if (locRec.link != null)
    var targetBlankHtml = (locRec.link.substr(0, 11) != 'javascript:') ? 'target="_blank"' : ''; // don't send javascript links to a new window
  var moreInfoHtml = (locRec.link != null) ? '<a href="' + locRec.link + '" ' + targetBlankHtml + '>More Info &raquo;</a>' : '';
  
  var descHtml = '<div class="map-info-desc">' + imgTagHtml + '<h4>' + locRec.title + '</h4><p>' + locRec.desc + '</p>' + moreInfoHtml + '<br clear="all" /></div>';
  
  // create marker
  var marker = new GMarker(new GLatLng(locRec.lat, locRec.lng));
  marker.tts_descriptionHtml = descHtml;
  GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(marker.tts_descriptionHtml); });
  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);
}
