jQuery(document).ready(setupVestigingPopups);

/* Mini map next to search form */
var MiniMap = new Object();

MiniMap.init = function () {
   var mapContainer, map, marker, markerData = {}, geoCoder;
   var _location;

   mapContainer = document.getElementById("map_canvas");

   geoCoder = new GClientGeocoder();
   geoCoder.setBaseCountryCode("nl");

   map = new GMap2(mapContainer);
   map.addControl( new GSmallZoomControl3D() );
   map.setCenter(new GLatLng(52.35, 5.4), 7); //nederland by default

   //get location and place icon for Searched location
   if (MiniMap.searchLocation != '') {
      geoCoder.getLatLng(MiniMap.searchLocation,
         function (point) {
            if (!point) { if (console && console.log) { console.log('Locatie niet gevonden: "'+ MiniMap.searchLocation +'"') } }
            else {
               markerData.title = 'Uw gezochte locatie';
               marker = new GMarker(point, markerData);

               map.addOverlay(marker);
            }
         }
      );
   }

   // Request latlong for each result location. Delay every 250ms; Google blocks successive request that come too
   // quickly or in too great a volume.
   var i = 0;
   var requestTimer = setInterval(function () {
      if (i == MiniMap.arrFoundLocations.length) {
         clearInterval(requestTimer);
         return;
      }
      /////
      _location = MiniMap.arrFoundLocations[i];

      //console.log('Locating '+ _location.title +' ('+ _location.locationstring + ')...');

	   if(_location.coords != null && _location.coords != '') {
         var arrCoords = _location.coords.split(',');
         if(arrCoords.length == 2) {
            var point        = new GLatLng(arrCoords[0], arrCoords[1]);
            var marker       = createMarker(point, _location.title, _location.html);

            map.addOverlay(marker);
         }
	   } else {
		   geoCoder.getLatLng(_location.locationstring,
			   function (point) {
               if (!point) { if (console && console.log) { console.log('Locatie niet gevonden: "'+ _location.locationstring +'"') } }
               else {
                  if (console && console.log) console.log('  '+ _location.title +' ('+ _location.city +', '+ _location.locationstring + ') geocoded at { '+ point + ' }');
                  markerData.title = _location.title;
                  markerData.html = _location.html;
                  marker = createMarker(point, markerData.title, markerData.html);

                  map.addOverlay(marker);
				   }
			   }
		   );
	   }

      /////
      i++;
   }, 250);
}
MiniMap.searchLocation = '';
/* arrFoundLocations
   Contains maps to represent some data about a location
*/
MiniMap.arrFoundLocations = new Array();

/* Vestiging-specific main & helper functions */
var arrMarkers = new Array();
var arrEmptyCoords = new Array();

function initGoogleMap() {
	var map = new GMap2(document.getElementById("map_canvas"));

	map.setCenter(new GLatLng(52.20, 5.4), 7); //nederland
	map.addControl( new GLargeMapControl3D() );
	map.addControl( new GMapTypeControl() );

	for( var _i=0; _i < arrMarkers.length; _i++ ) {
		var arrCoords = arrMarkers[_i].coordinates.split(',');
		if(arrCoords.length == 2) {
			var point        = new GLatLng(arrCoords[0], arrCoords[1]);
			var marker       = createMarker(point, arrMarkers[_i].title, arrMarkers[_i].description);

			map.addOverlay(marker);
		}
	}

	// load coordinates
	var geocoder = new GClientGeocoder();
   geocoder.setBaseCountryCode("nl");

   // Request latlong for each unkown location. Delay every 500ms; Google blocks successive request that come too
   // quickly or in too great a volume.
   var i = 0;
   var requestTimer = setInterval(function () {
      if (i == arrEmptyCoords.length) {
         clearInterval(requestTimer);
         return;
      }
      /////
		var id = arrEmptyCoords[i].id;

      geocoder.getLatLng(arrEmptyCoords[i].search,
         function (point) {
            if (!point) { }
            else {
               var url = '/templates/snippets/vestiging_ajax.asp';
               var pars = 'coords='+point+'&id='+id;

               jQuery.ajax({ type: "POST", url: url, data: pars });
            }
         }
      );

      /////
      i++;
   }, 500);
}

function addMarker(coordinates, title, description) {
	var _oMarker = new Object();
	_oMarker.coordinates = coordinates;
	_oMarker.title = title;
	_oMarker.description = description;

	arrMarkers.push(_oMarker);
}

function setCoordinates(id, address, zipcode, town) {
	var _search = town;

	var _oEmptyCoords = new Object();
	_oEmptyCoords.search = _search;
	_oEmptyCoords.id     = id;

	arrEmptyCoords[arrEmptyCoords.length] = _oEmptyCoords;
}

function createMarker(point, title, html) {
   var markerIcon = new GIcon(G_DEFAULT_ICON);
   markerIcon.image = '/gfx/icon_gmaps_marker.png';
   markerIcon.shadow = '';
   markerIcon.iconSize = new GSize(20, 20);
   markerIcon.iconAnchor = new GPoint(10, 10);

	var marker = new GMarker(point, {
      title: (title || '')
      ,icon: markerIcon
   });
	GEvent.addListener(marker, "click", function() {
	   marker.openInfoWindowHtml(html);

      // openInfoWindowHtml has an option onOpenFn: to call a function after it opens, but it doesn't do anything, it seems.
      // hence a crude delay. -WJ.
      setTimeout(setupVestigingPopups, 150);
	});
	return marker;
}

function setupVestigingPopups() {
   var popLinks = jQuery('.pop_location');

   popLinks.click(function () {
      var winSize = measure('window');
      window.open(this.href,'_blank','width=990,height=650,location=yes,menubar=yes,toolbar=yes,status=yes,scrollbars=yes,resizable=yes');
      return false;
   })
}
