/* ************************************************************************************* */
/* * 
/* * 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);
    
    var customUI              = map.getDefaultUI();
    customUI.maptypes.hybrid  = false;
    
    map.setUI(customUI);
    map.setMapType(G_PHYSICAL_MAP); //G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP, G_DEFAULT_MAP_TYPES
    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;
}

function selected(object) {
  var unSelect = true;
  if(unSelect) {
    $("divMapCategories").select("a").each(function(el) {
      el.removeClassName("isSelected");
      unSelect = false;
    });
  }
  if(!unSelect) {
    object.addClassName("isSelected");
  }
}

function getPage() {
  var categorie = arguments[0] || 0;
  var ID        = arguments[1] || 0;
  var container = $("ajaxContent");
  var url       = "/handler/omgeving.lp";
  
  container.update('<div id="divLoading"><center><img src="/images/loading.gif" alt="" title=""></center></div>');
  
  var myAjax = new Ajax.Request(url, {
  parameters: {categorie:categorie,ID:ID},
    method: 'post',
    onComplete: function(originalRequest) {
      try {
        container.innerHTML = originalRequest.responseText;
        if(ID) setTimeout("document.location.replace('#ajax_content');", 500);
      } catch (e) {
      }
    }
  });
}