﻿var center;
var map = null;
var geocoder = null;
var glocation;
var zoom;
var gbusiness;

function GoogleMe(ArrayStorage,gzoom,gtype){

  if(document.getElementById("map")){

	  if (GBrowserIsCompatible()) {

		  // Initialize the map.
		  map = new GMap2(document.getElementById("map"));
      geocoder = new GClientGeocoder();
      map.addControl(new GSmallMapControl()); 
	    map.enableScrollWheelZoom();

	 // ArrayStorage empty, set default Eurocommerce location
	 if(ArrayStorage.length == 0){

		eurocomnow();

	 } else {

      for(var i=0; i<ArrayStorage.length; i++){

		  if(ArrayStorage[i]['zipcode']){ 
			glocation = ArrayStorage[i]['zipcode'] + ',nl';       
		  } else {
			glocation = 'nl,' + ArrayStorage[i]['city'] + ',' + ArrayStorage[i]['address'];
		  }

      // gbusiness = ArrayStorage[i]['name'];

      center = geocoder.getLatLng(glocation,function(point) {
        if (!point) {
          // alert(glocation + " niet gevonden!");
        } else {
            map.setCenter(point, gzoom);
            var marker = new GMarker(point);         
          /* GEvent.addListener(marker, "click", function() {
            marker.openInfoWindowHtml(gbusiness); // If enabled, last value opened, others closed
          });
          */
          map.addOverlay(marker);
        }

      });

	  }

      if(gtype == 1){
        center = geocoder.getLatLng(ArrayStorage[0]['province'],function(point) {
          if (point) { map.setCenter(point, gzoom); }
        });
      }

      if(gtype == 2){
        center = geocoder.getLatLng(ArrayStorage[0]['city'],function(point) {
          if (point) { map.setCenter(point, gzoom); }
        });
      }

      }
    }
  }
}

function ProjectMe()
{
	for(var i=0; i<top.Projecten.length; i++){
      if(top.Projecten[i]['link'] == document.getElementById('xlink').value){
        var gtext = top.Projecten[i]['name'];

		if((top.Projecten[i]['address']) && (top.Projecten[i]['city'])){ 
			glocation = top.Projecten[i]['city'] + ',' + top.Projecten[i]['address'] + ',nl'; } 
			else 
			{ glocation = top.Projecten[i]['city'] + ',nl'; }
              
        if(document.getElementById("map")){

	        if (GBrowserIsCompatible()) {

		        // Initialize the map.
		        map = new GMap2(document.getElementById("map"));
	            geocoder = new GClientGeocoder();
		        map.addControl(new GSmallMapControl()); 
			    map.enableScrollWheelZoom();

            if(document.getElementById('geocodex').value.length > 10){ 
              glocation = document.getElementById('geocodex').value;               
            }

            center = geocoder.getLatLng(glocation,function(point) {
         
              if (!point) {
                alert('Not found');
              } else {

                map.setCenter(point, 15);
                var marker = new GMarker(point);         
                GEvent.addListener(marker, "click", function() {
                  marker.openInfoWindowHtml(gtext); 
                });
                map.addOverlay(marker);
              }     
            });
          }
        }
      }
    }
}

function GoogleAll()
{
              
        if(document.getElementById("map")){

	        if (GBrowserIsCompatible()) {

		        // Initialize the map.
		        map = new GMap2(document.getElementById("map"));
            geocoder = new GClientGeocoder();
            map.addControl(new GSmallMapControl()); 
	          map.enableScrollWheelZoom();

            for(var i=0; i<top.Projecten.length; i++){
           
              if(top.Projecten[i]['city']){ 
                glocation = top.Projecten[i]['city'] + ',nl';
                center = geocoder.getLatLng(glocation,function(point) {
             
                  if (!point) {
                    // alert('Not found');
                  } else {

                    map.setCenter(point, 6);
                    var marker = new GMarker(point);         
                    map.addOverlay(marker);
                 }     
              });
            }
          }

      }
    }
}

function eurocomnow(){

  if(document.getElementById("map")){

	  if (GBrowserIsCompatible()) {

			// Initialize the map.
			map = new GMap2(document.getElementById("map"));
			geocoder = new GClientGeocoder();
			map.addControl(new GSmallMapControl());
			map.enableScrollWheelZoom();

			address = "Bergweidedijk 40, deventer, nl";
			center = geocoder.getLatLng(address,function(point) {

	        if (!point) {
		      alert(address + " niet gevonden!");
			} else {
	          map.setCenter(point, 14);
		      var marker = new GMarker(point);
			  map.addOverlay(marker);
	          // marker.openInfoWindowHtml(address); // If enabled, last value opened, others closed
		    }
	      });

    }
  }
}


$(document.body).unload(function() {
	if (GBrowserIsCompatible()) {
		GUnload();
	}
});

// center = new GLatLng(52.23663085859762, 6.192963123321533);
// map.setCenter(center, 14);
// map.addOverlay(new GMarker(center));
