    var geocoder;
    var map;
	var level = 8;	
	var mgr;
	var citymarker = [];
	var citymarkerdata = [];
	var zipmarker = [];
	var zipmarkerdata = [];
	var agymarker = [];
	

	
    // On page load, call this function
    function load(latitude, longitude) {
    
    	// Create new map object
    	map = new GMap2(document.getElementById("map"));

    	//the zoom control from the upper left of the map
    	map.addControl(new GSmallMapControl());
		
    	//the map type control (Hybrid, Map, Satellite)
    	map.addControl(new GMapTypeControl());
		
		//display map center
    	map.setCenter(new GLatLng(latitude, longitude), level);      

		mgr = new GMarkerManager(map);	

    }

    // This function adds the point to the map
    function addMarker(latitude, longitude, info, city, zipcode) {
    	var point = new GLatLng(latitude, longitude);

    	// Create a marker
    	var marker = new GMarker(point, {icon : getIcon(2)});
		//GMarker(point, {icon : getIcon(2)});
    	// Add address information to marker
    	GEvent.addListener(marker, "click", function() {
   	    marker.openInfoWindowHtml(info);  });
    	
    	//Add marker to array of agency markers
		agymarker.push(marker);
		
		//Build zipcode information array (number of agencies)
		found = 0;		
		zipcode	+= ', France';
		for (var i=0;i<zipmarkerdata.length;i++)
			if (zipmarkerdata[i][0] == zipcode) {
				zipmarkerdata[i][1]++;
				found = 1;
				break;
			}
		if (!found)
		{
			zipmarkerdata.push(new Array(zipcode, 1))
		}

		//Build city information array (number of agencies)
		found = 0;			
		city += ', France';
		for (var i=0;i<citymarkerdata.length;i++)
			if (citymarkerdata[i][0] == city) {
				citymarkerdata[i][1]++;
				found = 1;
				break;
			}
		if (!found)
		{
			citymarkerdata.push(new Array(city, 1))
		}		
    }

// sets marker icon data depending on icon type and returns an icon object
	function getIcon(level)	{	
      var baseIcon = new GIcon();
      baseIcon.iconSize=new GSize(24,38);
      baseIcon.iconAnchor=new GPoint(13,28);
      //baseIcon.iconSize=new GSize(32,32);
      //baseIcon.shadowSize=new GSize(56,32);
      //baseIcon.iconAnchor=new GPoint(16,32);
      baseIcon.infoWindowAnchor=new GPoint(16,0);
      
	  //set different icon style for marker type: 1 - city marker, 2 - zip marker, 3 - agency marker	  
	  switch (level) {
    		case 1:
    			baseIcon.image = "http://www.gti-immobilier.fr/common/xsl/b2c/gti/picts/gti-picto-googl.gif";
    			break;
    		case 2:
    			baseIcon.image = "http://www.gti-immobilier.fr/common/xsl/b2c/gti/picts/gti-picto-googl.gif";	
    			break;
    		case 3:
    			baseIcon.image = "http://www.gti-immobilier.fr/common/xsl/b2c/gti/picts/gti-picto-googl.gif";	
    			break;
    	}		      
	  return baseIcon;	
	}


// builds and displays on the map non agency markers
	function setupMarkers() {
		var q, k, i, strDisplay; 
		var geocoder = new GClientGeocoder();	
			
		for (q=0;q<zipmarkerdata.length;q++) {			
    		var strDisplay = zipmarkerdata[q][1];
    		geocoder.getLocations(zipmarkerdata[q][0],
    		 function(response) { 
    		 //debugger;
            if (response.Placemark) {
            		var place = response.Placemark[0];
            		var point = new GLatLng(place.Point.coordinates[1],	place.Point.coordinates[0]);
            		var marker = new GMarker(point, {icon : getIcon(2)});            		
    				// Add information to the current zip marker from zip information array 
    				GEvent.addListener(marker, "click", function() { 
    					var index_marker_zip = -1; 
    				//Search index of current marker in information array	
    					for (i=0; i < zipmarker.length; i++)
    					{
    						if (zipmarker[i] == marker)
    						{
    							index_marker_zip = i; 
    							break; 
    						}
    					} 
    					if (index_marker_zip > -1)
    					{
						//Write description to info popup of marker
							strDisplay = "<table border=0>" +
										 "<tr>" +
										 "<th> Code postal : </th>" +
										 "<th>" + zipmarkerdata[index_marker_zip][0] + "</th>" +
										 "</tr><tr>" +
										 "<td align=right>" + zipmarkerdata[index_marker_zip][1] + "</td>"
										  
							if (zipmarkerdata[index_marker_zip][1] > 1)
    							strDisplay += '<td>agences</td>'  
    						else
    							strDisplay += '<td>agence</td>';  
							strDisplay += '</tr></table>'
    						marker.openInfoWindowHtml(strDisplay);      							
    					} 					
   					});            		
					for (k=0; k<zipmarkerdata.length; k++) 
					 if (zipmarkerdata[k][0] == response.name)
						break;									
					zipmarker.splice(k,0,marker);
//zip markers visible between levels of zoom 7 and 10					
					mgr.addMarker(marker, 7, 10);
    			}    	    
    		 }		    		 
    		);
		}

		for (q=0;q<citymarkerdata.length;q++) {		
			var strDisplay = citymarkerdata[q][1];
    		geocoder.getLocations(citymarkerdata[q][0],
    		 function(response) { 
            if (response.Placemark) {
            		var place = response.Placemark[0];
            		var point = new GLatLng(place.Point.coordinates[1],	place.Point.coordinates[0]);
            		var marker = new GMarker(point, {icon : getIcon(1)});            		
    				// Add information to the current city marker from city information array 
    				GEvent.addListener(marker, "click", function() { 
    					var index_marker_city = -1; 
    				//Search index of current marker in information array	
    					for (i=0; i < citymarker.length; i++)
    					{
    						if (citymarker[i] == marker)
    						{
    							index_marker_city = i; 
    							break; 
    						}
    					} 
    					if (index_marker_city > -1)
    					{
						//Write description to info popup of marker
							strDisplay = "<table border=0>" +
										 "<tr>" +
										 "<th> Ville : </th>" +
										 "<th>" + citymarkerdata[index_marker_city][0] + "</th>" +
										 "</tr><tr>" +
										 "<td align=right>" + citymarkerdata[index_marker_city][1] + "</td>"
										  
							if (citymarkerdata[index_marker_city][1] > 1)
    							strDisplay += '<td>agences</td>'  
    						else
    							strDisplay += '<td>agence</td>';  
							strDisplay += '</tr></table>'
    						marker.openInfoWindowHtml(strDisplay);      							
    					} 					
   					});            		
					for (k=0; k<citymarkerdata.length; k++) 
					 if (citymarkerdata[k][0] == response.name)
						break;									
					citymarker.splice(k,0,marker);
//city markers visible between levels of zoom 1 and 6					
					mgr.addMarker(marker, 1, 6);
    			}    	    
    		 }		    		 
    		);
    		
		}
	}    
	
//adds agency markers on the map
	function refreshmgr()
	{
//agency markers visible from levels of zoom 11
		mgr.addMarkers(agymarker, 6);
		mgr.refresh();	
	
	}
//calculate the center of the map according to the minlatitude, maxlatitude, minlongitude, maxlongitude	
	function calculateCenterMap(minlatitude, maxlatitude, minlongitude, maxlongitude)
	{
	    var centerlatitude = (maxlatitude + minlatitude)/2;
	    var centerlongitude = (maxlongitude + minlongitude)/2;
	    
	    load(centerlatitude, centerlongitude);
	}



	
