/*
<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_tCjP07GKqF4wSd1yQ2hQGhB4Ym0NhnRiOQry7AE8xZlQS-hS4sj7O5er64eOLQRXRVrmTwxjE3g" 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: -16.965555, lng: 145.736023, title: 'Tropical Havens', link: null, img: 'i-logo-map.jpg', desc: '17 Theresa Close<br />Bayview Gardens, Cairns<br />Queensland 4869'},
	{locId:  2, group: 'other', lat: -16.483986, lng: 145.46582, title: 'Port Douglas', link: null, desc: 'Port Douglas'},
	{locId:  3, group: 'other', lat: -16.79042, lng: 145.691383, title: 'Trinity Beach', link: null, desc: 'Trinity Beach'},
	{locId:  4, group: 'other', lat: -16.589879, lng: 145.513507, title: 'Oak Beach', link: null, desc: 'Oak Beach'},
	{locId:  5, group: 'other', lat: -16.747083, lng: 145.670998, title: 'Palm Cove', link: null, desc: 'Palm Cove'},
	{locId:  6, group: 'other', lat: -16.099955, lng: 145.459565, title: 'Cape Tribulation', link: null, desc: 'Cape Tribulation'},
	{locId:  6, group: 'other', lat: -16.498908, lng: 145.465518, title: 'Port Douglas', link: null, desc: 'Four Mile Beach'},
	{locId:  6, group: 'other', lat: -16.371201, lng: 145.367249, title: 'Whyanbeel Valley', link: null, desc: 'Whyanbeel Valley'},
	{locId:  6, group: 'other', lat: -16.429516, lng: 145.404117, title: 'Newell Beach', link: null, desc: 'Newell Beach'}
];

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(-16.934964537183383, 145.7585334777832), 9);
  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);
}