/**
  * Modified javascript for geomap module
  * Based on
  *  - jQuery googleMap Copyright Dylan Verheul <dylan@dyve.net>
  *  - Google map API tutorial Copyright Mike Williams <econym.googlepages.com>
  *  - Of microformats and geocoding Copyright Blues <tech.bluesmoon.info/2006/01/of-microformats-and-geocoding.html>
  * This script parse geo microformats on the page and display a google map on a block (block id = map)
  *
  * Lesauf <www.lesauf-design.net>
  */

if (GBrowserIsCompatible()) {
  // array of markers
  var gmarkers = [];
  var i = 0;

  /**
    * Add a marker with a callout containing the specified html
    */
  function addMarkerToMap(point, name, html) {
    var marker = new GMarker(point);

    name = "<h3>" + name + "</h3>";
    // configuring info windows to open on click
    if (html)
    {
      GEvent.addListener(marker, 'click', function() {
        marker.openInfoWindowHtml(name + html);
      });
    }

    return marker;
  }

  /**
    * Called on unload action of body.
    * It parse geo microformats on page and render a map
    */
  function loadMicroformats() {
    // retrieving the div element which will contain the map
    var mapdiv = document.getElementById('map');

    // Retrieving all geo microformats on page
    var adr = document.getElementsByClassName("geo");
    if (adr.length) { //Display of block
      mapdiv.style.display = 'block';
    }

    // parsing geo microformats
    for (var i=0; i<adr.length; i++) {
      // Grab HTML to put into callout
      var html = adr[i].innerHTML;
      var name = adr[i].title;

      var lat, lon;

      var address = adr[i].getElementsByTagName('span');

      for (var j=0; j<address.length; j++) {
        if (address[j].className == 'latitude') {
          lat = 1*address[j].title;
        } else if (address[j].className == 'longitude') {
          lon = 1*address[j].title;
        }
      } // /for

      // Replacing microformats with link to marker on map
      adr[i].innerHTML = '<span class="see_map_link"><a href="javascript:myclick(' + i + ');">See on map -&gt;</a></span>';
      adr[i].style.display = 'block';
      // getting point
      var point = new GLatLng(lat, lon);

      // building markers array
      marker = addMarkerToMap(point, name, html);
      gmarkers[i] = marker;

      // limits of map
      if (!bounds) { 
        var bounds = new GLatLngBounds(marker.getPoint());
      } 
      else {
        // extend bounds with marker.getPoint();
        bounds.extend(marker.getPoint());
      }
    } // /for

    // Build map and center on lat/lon if we have markers
    if (gmarkers.length) {
      var map = new GMap2(mapdiv);
      map.addControl(new GSmallZoomControl());
      //map.addControl(new GSmallMapControl());
      //map.addControl(new GMapTypeControl());

      var southWest=bounds.getSouthWest();
      var northEast=bounds.getNorthEast();
      map.setCenter(bounds.getCenter(), map.getBoundsZoomLevel(bounds));

      for (var i=0; i<gmarkers.length; i++) {
        map.addOverlay(gmarkers[i]);
      }
      map.enableScrollWheelZoom();
    } // /if gmarkers
  }

  /**
    * This function picks up the click and opens the corresponding info window
    */
  function myclick(i) {
    GEvent.trigger(gmarkers[i], "click");
  }
} else { // Browser is not compatible
  alert("Sorry, the Google Maps API is not compatible with this browser");
}

