﻿// JScript File
/*
function ga_initialize__() { 
	var latlng = new google.maps.LatLng(50.846281,4.354727);
	var myOptions = {
      zoom: 9,
      //center: latlng,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
	window.setTimeout(setupMarkers, 1000); 
}*/

function loadScript() {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src = "http://maps.google.com/maps/api/js?sensor=false&region=BE&callback=ga_initialize";
    document.body.appendChild(script);
  }
  
 /* var txt="";
 
function setupMarkers(){
    
    geocoder = new google.maps.Geocoder();
    var cnt = 0;
	for(ikey in ctr){
      try {
          if (cnt % 10 == 0) {
              //window.setTimeout(setupMarker(ctr[ikey]), 1005);
          }
          else {
              //setupMarker(ctr[ikey]);
          }

         // setupMarker(ctr[ikey]);
      }
      catch(err)
      {
       txt="There was an error on this page.\n\n";
       txt+="Error description: " + err.description + "\n\n";
       txt+="Click OK to continue.\n\n";
       alert(txt);
       }

       cnt += 1;
        
        
	}
}*/

/*function setupMarker(item)
{
    geocoder.geocode({ 'address': item[2] + ',' + item[3] },
                     function (results, status) {
                         if (status == google.maps.GeocoderStatus.OK) {
                             createMarker(results[0].geometry.location, item[0], item[1] + '<br>' + item[5] + '<br>' + item[2] + '<br>' + item[4] + ' ' + item[3], item[1]);
                         }
                         else {
                             if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                                 setTimeout(function () { createMarker(results[0].geometry.location, item[0], item[1] + '<br>' + item[5] + '<br>' + item[2] + '<br>' + item[4] + ' ' + item[3], item[1]); }, (timeout * 3));
                             }
                             else {
                                 alert("Geocode was not successful for the following reason: " + status);
                             }
                         }
                     });
}*/
 
/*function createMarker(point, id, text, ploeg) {
	 
       var marker = new google.maps.Marker({
            map: map, 
            position: point,
            title:ploeg 
        });
       
     var infowindow = new google.maps.InfoWindow({
        content: text
    });
  
    google.maps.event.addListener(marker, 'click', function() {
      infowindow.open(map, marker);
    });
    markersArray.push(marker);        
	
	bounds = new google.maps.LatLngBounds();
	for (i in markersArray) {       
	 
	      bounds.extend(markersArray[i].getPosition());    
	 }
	 map.fitBounds(bounds);
	return marker;
}*/





var geocoder;
var map;
var markersArray = []; 

var places = [];
var address_position = 0;

var timeout = 600;

function ga_initialize() {
    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(50.846281, 4.354727);
    var myOptions = {
        zoom: 8,
        center: latlng,
        mapTypeId: 'roadmap'
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    addMarker(address_position);
}


function addMarker(position) {
    if (ctr != null) {
        if (ctr[position] != null) {
            geocoder.geocode({ 'address': ctr[position][2] + ',' + ctr[position][3] }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    places[position] = results[0].geometry.location;

                    var marker = new google.maps.Marker({
                        position: places[position],
                        map: map,
                        title: ctr[position][1]
                    });
                    markersArray.push(marker);

                    google.maps.event.addListener(marker, 'click', function () {
                        var infowindow = new google.maps.InfoWindow();
                        infowindow.setContent(ctr[position][1] + '<br>' + ctr[position][5] + '<br>' + ctr[position][2] + '<br>' + ctr[position][4] + ' ' + ctr[position][3]);
                        infowindow.open(map, marker);
                    });

                    /*bounds = new google.maps.LatLngBounds();
                    for (i in markersArray) {
                    bounds.extend(markersArray[i].getPosition());
                    }
                    map.fitBounds(bounds);*/
                }
                else {
                    if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                        setTimeout(function () { addMarker(position); }, (timeout * 3));
                    }
                }
                address_position++;
                if (address_position < ctr.length) {
                    setTimeout(function () { addMarker(address_position); }, (timeout));
                }
                if (address_position == ctr.length) {
                    bounds = new google.maps.LatLngBounds();
                    for (i in markersArray) {
                        bounds.extend(markersArray[i].getPosition());
                    }
                    map.fitBounds(bounds);
                }
            });
        }
    }
}


/*function addMarker(position) {
    geocoder.geocode({ 'address': address[position] }, function (results, status) {



        if (status == google.maps.GeocoderStatus.OK) {
            places[position] = results[0].geometry.location;

            var marker = new google.maps.Marker({
                position: places[position],
                map: map
            });

            google.maps.event.addListener(marker, 'click', function () {
                if (!infowindow) {
                    infowindow = new google.maps.InfoWindow();
                }
                infowindow.setContent(popup_content[position]);
                infowindow.open(map, marker);
            });
        }
        else {
            if (status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT) {
                setTimeout(function () { addMarker(position); }, (timeout * 3));
            }
        }
        address_position++;
        if (address_position < address.length) {
            setTimeout(function () { addMarker(address_position); }, (timeout));
        }
    });
}*/





 

