﻿var map = null;
var polys = [];
var labels = [];

GPolygon.prototype.Contains = function(point) 
{
    var j=0;
    var oddNodes = false;
    var x = point.lng();
    var y = point.lat();
    for (var i=0; i < this.getVertexCount(); i++) 
    {
        j++;
        if (j == this.getVertexCount()) 
        {
            j = 0;
        }
        if (((this.getVertex(i).lat() < y) && (this.getVertex(j).lat() >= y))
          || ((this.getVertex(j).lat() < y) && (this.getVertex(i).lat() >= y))) {
            if ( this.getVertex(i).lng() + (y - this.getVertex(i).lat())
            /  (this.getVertex(j).lat()-this.getVertex(i).lat())
            *  (this.getVertex(j).lng() - this.getVertex(i).lng())<x ) {
              oddNodes = !oddNodes
            }
        }
    }
    return oddNodes;
}



function createMap()
{
    if (!GBrowserIsCompatible()) 
    {
        alert('Uw browser is niet compatibel met de Google Maps API');
        return;
    }

    map = new GMap2(document.getElementById("map_canvas"));
    map.setCenter(new GLatLng(52.3, 4.9), 8);
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.enableContinuousZoom();
    map.enableScrollWheelZoom();  
    
    GEvent.addListener(map, "click", function(overlay,point) {
        if (!overlay) {
            for (var i=0; i<polys.length; i++) {
                if (polys[i].Contains(point)) {
                    map.openInfoWindowHtml(point,labels[i]);
                    //i = 999; // Jump out of loop
                }
            }
        }
    });
    
    GEvent.addListener(map, "zoomend", function(oldLevel, newLevel) {
        if(newLevel < 8)
        {
            map.setCenter(new GLatLng(52.3, 4.9), 8);
        }
    });
    
}

function createPolygon(id, content, color)
{
    var pts = new Array();
    for(var i=0;i<polyPoints.length;i++)
    {
        if(polyPoints[i][0] == id)
        {
            pts.push(new GLatLng(parseFloat(polyPoints[i][1]), parseFloat(polyPoints[i][2])));
        }
    }

    var polyMarker = new GPolygon(pts,"#000000",1,1,color,0.5,{clickable:false});
    polys.push(polyMarker);
    labels.push(content);   
    map.addOverlay(polyMarker);
}


// Key handler for intercepting the [enter] key for submitting the directions
function submitenter()
{
    var keycode;
    if (window.event) keycode = window.event.keyCode;
    else if (e) keycode = e.which;
    else return true;

    if (keycode == 13)
    {
        centerMap(null);
        return false;
    }
    else
    {
       return true;
    }
} 


// Center the map around given address.
function centerMap() {
  var address = document.getElementById('txtAddress').value;
  if(address.length == 0)
  {
    alert('Vul een plaatsnaam in.');
    return;
  }
  
  var currentZoomLevel = map.getZoom();

  if (address.length > 0) {
    var geocoder = new GClientGeocoder();

    geocoder.getLatLng(address,
      function(point) {
        if (!point) {
          alert('Adres "' + address + '" niet gevonden.');
        } else {
          //map.setCenter(point, currentZoomLevel);
          map.setCenter(point, 11);
        }
      });
  }
}