
var map;
var geocoder;

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(-16.747083, 145.670998), 15);
  window.onunload = GUnload;
  
  // add markers & location links
  var markers = fetchMarkers();
  for (var i = 0; i < markers.length; i++) {
    map.addOverlay(markers[i]);
  }
  
  // highlight a location if instructed via the url.
  if (FORM_DATA['locid'] != undefined) {
    setTimeout("highlightMapLocation(FORM_DATA['locid'])", 1);
  } else if ($('idproduct').value != '') {
    var idproduct = $('idproduct').value;
    locId = 0;
    
    for (var i= 0; i < mapLocations.length; i++) {
      locRec = mapLocations[i];
      
      if (locRec.lat == 0)
        continue;
      
      if (locRec.idproduct == idproduct) {
        //we have found a matching location
        // ToggleVisibleSubNav(locRec.location);
        // ToggleVisibleSubSubNav(locRec.location_heading);
        
        locId = locRec.locId;
        break;
      }
    }
    
    if (locId != 0)
      highlightMapLocation(locId);
  }
  
  // if we're in dev mode, show the address finder
  if (document.location.toString().indexOf('dev-mode') != -1) {
    $('dev-mode-address-finder').style.display = 'block';
  }
  
  return true;
}

function grabLatLongFromClick(overlay, point)
{
  $('address_lookup_result').innerHTML = point;
}

function fetchMarkers()
{
  var markers = new Array();
  
  for (var i= 0; i < mapLocations.length; i++) {
    locRec = mapLocations[i];
    
    try{
    if (locRec.lat == 0)
      continue;
    }
    catch(e){
      continue;
    }
    markers.push(createMarker(locRec));
  }
  
  return markers;
}
function createMarker(locRec)
{
  // generate description html
  var imgTagHtml = (locRec.img == '') ? '' : '<img src="' + locRec.img + '" />' ;
  var targetBlankHtml = (locRec.link.substr(0, 11) != 'javascript:') ? 'target="_blank"' : ''; // don't send javascript links to a new window
  var descHtml = '<div class="map-info-desc">' + imgTagHtml + '<h3>' + locRec.title + '</h3><h4>' + locRec.address + '</h4><p>' + locRec.desc + '</p><a href="' + locRec.link + '" ' + targetBlankHtml + '>More Info &raquo;</a><br clear="all" /></div>';
  
  // create pushpin icon
  var icon = new GIcon();
  icon.image = "images/map-icon/foreground.png";
  icon.shadow = "images/map-icon/shadow.png";
  icon.printImage = 'images/map-icon/foreground.gif';
  icon.mozPrintImage = 'images/map-icon/foreground.gif';
  icon.printShadow = 'images/map-icon/shadow.gif';
  icon.transparent = 'images/map-icon/click-mask.png';
  icon.iconSize = new GSize(36, 36);
  icon.shadowSize = new GSize(36, 36);
  icon.iconAnchor = new GPoint(13, 36);
  icon.infoWindowAnchor = new GPoint(13, 1);
  icon.imageMap = [
    12, 36,
    0, 16,
    0, 11,
    2, 7,
    7, 2,
    10, 0,
    15, 0,
    20, 3,
    24, 7,
    25, 12,
    25, 16,
    23, 20
  ];
  
  // create marker
  var marker = new GMarker(new GLatLng(locRec.lat, locRec.lng), icon);
  marker.tts_descriptionHtml = descHtml;
  GEvent.addListener(marker, "click", function() { marker.openInfoWindowHtml(marker.tts_descriptionHtml); });
  locRec.marker = marker;
  
  // create link to it
  // var locLi = document.createElement('li');
  // locLi.innerHTML = '<a href="map.php#map-top" onclick="highlightMapLocation(' + locRec.locId + ');return true;">' + locRec.title + '</a>';
  // 
  // $(locRec.group + '-locations').appendChild(locLi);

  return marker;
}

var locFromAddressMarker = false;

function locationFromAddress(address)
{
  if (!geocoder)
    geocoder = new GClientGeocoder();
  
  geocoder.getLatLng(
    address,
    function(point) {
      if (!point) {
        $('address_lookup_result').innerHTML = ('can\'t find: ' + address);
      } else {
        $('address_lookup_result').innerHTML = ('lat/lng: ' + point);
        
        map.setCenter(point, 17);
        
        if (locFromAddressMarker != false)
          map.removeOverlay(locFromAddressMarker);
        
        locFromAddressMarker = new GMarker(point);
        map.addOverlay(locFromAddressMarker);
      }
    }
  );
}

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);
}

function ToggleVisibleSubNav(id){
 
  for (var i= 0; i < map_location.length; i++) {
    try{
    if(map_location[i].name == id){
      $('l_'+(map_location[i].name)).style.display = '';
    }
    else{
      $('l_'+(map_location[i].name)).style.display = 'none';
    }
    }
    catch(e){}
  }
}

function ToggleVisibleSubSubNav(id){
  
  for (var i= 0; i < map_location_headings.length; i++) {
   try{
    if(map_location_headings[i].name == id)
      $('lh_'+(map_location_headings[i].name)).style.display = '';
    else
      $('lh_'+(map_location_headings[i].name)).style.display = 'none';
    }
    catch(e){}
  }
  
}
