/* ************************************************************************************* */
/* * 
/* * Script file: Google Maps, Holiday Media
/* * Authors: Wim Latour & Jan Verharen
/* * dev@holiday.nl
/* * 
/* ************************************************************************************* */

var map = null;
var geocoder = null;
var zoom = 9;

function GMap_load(latitude,longitude) {

  var address = document.frmGMapLocation.frmGMapAddress.value;
  var content = document.frmGMapLocation.frmGMapContent.value;
  if (!content) content = address;

  if (document.frmGMapRoute && document.frmGMapRoute.frmGMapDestinationAddress) {
    if (! document.frmGMapRoute.frmGMapDestinationAddress.value)
      document.frmGMapRoute.frmGMapDestinationAddress.value = address;
  }

  if (GBrowserIsCompatible()) {
    map = new GMap2(document.getElementById("map"));

    map.setCenter(new GLatLng(latitude, longitude), zoom);

    map.setMapType(G_NORMAL_MAP); /* both layers */
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GOverviewMapControl());

    geocoder = new GClientGeocoder();

    if (latitude&&longitude) {

      var _marker = new GMarker(new GLatLng(latitude, longitude));
      map.addOverlay(_marker);

      var _WINDOW_HTML = GMap_genHTML(content);

      GEvent.addListener(_marker, "click", function() {
        _marker.openInfoWindowHtml(_WINDOW_HTML);
      });
      //_marker.openInfoWindowHtml(WINDOW_HTML);

    } else {

//      GMap_showAddress(address, content);

    }

  }
}

function GMap_showAddress(address, content) {
  if (geocoder) {
    geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " niet gevonden");
        } else {
          map.setCenter(point, zoom);
          var _marker = new GMarker(point);
          map.addOverlay(_marker);

          var _WINDOW_HTML = GMap_genHTML(content);

          GEvent.addListener(_marker, "click", function() {
            _marker.openInfoWindowHtml(_WINDOW_HTML);
          });
          _marker.openInfoWindowHtml(_WINDOW_HTML);
        }
      }
    );
  }
}

function calcLocation(element,lng,saddr,address,city,country) {
  if(!lng) { lng = 'nl'; }
  if(!country) { country = 'Nederland'; }
  
  if(!address || !city ) {
    alert("plaats en adres zijn verplicht!");
    return false;
  }
  
  var uri  = 'http://maps.google.nl/maps?';
      uri += 'daddr='+saddr;
      uri += '&saddr='+address+', '+city+', '+country;
      uri += '&f=li&hl='+lng+'&ie=UTF8&z=&om=1';
  
  popitup(uri,950,700);
  
}

function GMap_genHTML(content) {
  var _WINDOW_HTML;
  _WINDOW_HTML = '<div class="GMapLocationPopup">';
  _WINDOW_HTML += content;
  _WINDOW_HTML += '</div>';
  return _WINDOW_HTML;
}

