//var box = outputBox();
function initGoogleMap() {
	var map = new GMap2(document.getElementById("map_canvas"));

	map.setCenter(new GLatLng(51.618017, 6.240234), 6); //benelux
	map.setUIToDefault();
	
	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].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.
   // strictly this is bad programming. If the geocoder returns the value _after_ a new loop is started, the vars 
   // id and strSearch will be changed, and the coordinates will be saved for the wrong id.
   var _j = 0;
   var requestTimer = setInterval(function () {
      if (_j == arrEmptyCoords.length) {
         clearInterval(requestTimer);
         return;
      }
      /////
		var id = arrEmptyCoords[_j].id;
      var strSearch = arrEmptyCoords[_j].search;

      //grab coords
      geocoder.getLatLng(
         strSearch,
         function(point) { //callback function 
	  
            if (!point) {
               // what to do?
			      //console.log('Not found: ' + strSearch);
            }
            else {
               var url = '/templates/lejeune_common/members_ajax.asp';
               //var pars = {coords:point, id:id};
               var pars = 'coords='+point+'&id='+id;
               //console.log('Search succes for:' + strSearch + ' Now making ajax call with parameters: ' + pars);
               $.ajax({
                 type: "POST",
                 url: url,
                 data: pars
               });
            }
         }
      );
      /////
      ++_j;
   }, 500);
}

var arrMarkers = new Array();
var arrEmptyCoords = new Array();

function addMarker(coordinates, description) {
	var _oMarker = new Object();
	_oMarker.coordinates = coordinates;
	_oMarker.description = description;

	arrMarkers.push(_oMarker);
}

function setCoordinates(id, address, postcode, town, country) {
   var _search;
	//var _search = address + ', ' + postcode + ', ' + town;
   if (country != 1) {
      _search = postcode + ' ' + address + ' ' + town;
   }
   else if ((/postbus/i).test(address)) { //bij postbussen, geef alleen de stad op
      _search = town;
   }
   else { //zoek anders op postcode, of adres + plaats als postcode leeg is.
   	_search = isEmpty(postcode) ? address + ' ' + town : postcode;
   }

	var _oEmptyCoords = new Object();
	_oEmptyCoords.search = _search;
	_oEmptyCoords.id     = id;	

	arrEmptyCoords[arrEmptyCoords.length] = _oEmptyCoords;
}

function createMarker(point,html) { 
	var marker = new GMarker(point); 
	GEvent.addListener(marker, "click", function() { 
	  marker.openInfoWindowHtml(html); 
	}); 
	return marker; 
  } 

function setNameSize() {
   $('namecontainer').style.width = (getStyleValue($('logo'), 'width', NOUNIT) - $('logo').offsetWidth()) + 'px';
}
