/*
	map.mapArray(dataArray,step?,construct?,callAfter?)

step is 10 by default
construct, called as a method of the map, just uses lat & lng properties of the data
callAfter, also called as a method of the map, is called after the last marker's mapped.

This will dynamically map an array of data. The data should be an 
array of objects looking something like this:

{ lat: 42, lng: 42, other: data, here: etc }

*/

function setupMapArray() {

function setupMapArray(map) {
var config = {};
	map.mapArray = function(dataArray,step,mkMarker,afterwards) {
		// store the config
		config.dataArray  = dataArray;
		config.step       = step       || 10;
		config.mkMarker   = mkMarker   || function(d){ return new GMarker(new GLatLng(d.lat,d.lng)) };
		config.afterwards = afterwards || function(){};
		config.startIndex = 0;
		config.map        = this;

		mapArray();
	};

	function mapArray() {
		var maxIndex = Math.min(config.startIndex + config.step,config.dataArray.length);

    for(var i = config.startIndex; i < maxIndex; i++)
      config.map.addOverlay(config.mkMarker.call(config.map,config.dataArray[i]));

	  if(maxIndex < config.dataArray.length) {
	    config.startIndex = maxIndex;
	    setTimeout(function(){ mapArray() },20);
	  } else {
			config.afterwards.call(config.map,config.dataArray);
			config = {};
	  }
	}
}
window.setupMapArray = setupMapArray;
}
setupMapArray()
