/*
	map.mapArray(dataArray,step=10,construct=new GMarker,callAfter=function(){})

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 has been 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 }

0.1  - initial

TODO:
    add afterStep arg, which would be called after each step
        so that a progress bar could be managed, for example
    make it so that there is not one shared config variable

*/

function setupMapArray() {
var config = {};

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 = {};
    }
}

GMap2.prototype.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(parseFloat(d.lat), parseFloat(d.lng))) };
    config.afterwards = afterwards || function(){};
    config.startIndex = 0;
    config.map        = this;

    mapArray();
};

}
setupMapArray()

