
var map;
var extremites = new Array();
var markerInfo = new Array();

function initMap(){

	if(document.getElementById('map'))
	{
		map = new GMap2(document.getElementById("map"));	
	  	map.disableDoubleClickZoom();
	  	
	  	function TextualZoomInControl() {}
	  	function TextualZoomOutControl() {}
		TextualZoomInControl.prototype = new GControl();
		TextualZoomOutControl.prototype = new GControl();
		
		TextualZoomInControl.prototype.initialize = function(map) 
		{  
			var container = document.createElement("div");  
			
			var zoomInDiv = document.createElement("div");  
			this.setButtonStyle_(zoomInDiv);  
			container.appendChild(zoomInDiv);  
			zoomInDiv.appendChild(document.createTextNode("Zoom +"));  
			GEvent.addDomListener(zoomInDiv, "click", function() {    map.zoomIn();  });  
			
			map.getContainer().appendChild(container);  
			return container;
		}
		
		TextualZoomOutControl.prototype.initialize = function(map) 
		{  
			var container = document.createElement("div");  
			
			var zoomOutDiv = document.createElement("div");  
			this.setButtonStyle_(zoomOutDiv);  
			container.appendChild(zoomOutDiv);  
			zoomOutDiv.appendChild(document.createTextNode("Zoom -"));  
			GEvent.addDomListener(zoomOutDiv, "click", function() {    map.zoomOut();  });  
		
			map.getContainer().appendChild(container);  
			return container;
		}
		
			
		
		TextualZoomInControl.prototype.getDefaultPosition = function() 
		{  
			return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(5, 5));
		}
		
		TextualZoomOutControl.prototype.getDefaultPosition = function() 
		{  
			return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(5, 25));
		}
		
		TextualZoomInControl.prototype.setButtonStyle_ = function(button) 
		{  
			button.className = 'mapGIIPlus';
			/*
			button.style.textDecoration = "underline";  
			button.style.color = "#0000cc";  
			button.style.backgroundColor = "white";  
			button.style.font = "10px Arial";  		
			button.style.border = "1px solid black";  
			button.style.padding = "2px";  
			button.style.marginBottom = "3px";  
			button.style.textAlign = "center";  
			button.style.width = "50px";  
			button.style.cursor = "pointer";
			*/
		}
		
		TextualZoomOutControl.prototype.setButtonStyle_ = function(button) 
		{  
			button.className = 'mapGIIMoins';
			/*
			button.style.textDecoration = "underline";  
			button.style.color = "#0000cc";  
			button.style.backgroundColor = "white";  
			button.style.font = "10px Arial";  		
			button.style.border = "1px solid black";  
			button.style.padding = "2px";  
			button.style.marginBottom = "3px";  
			button.style.textAlign = "center";  
			button.style.width = "50px";  
			button.style.cursor = "pointer";
			*/
		}
	  	map.addControl(new TextualZoomInControl());
	  	map.addControl(new TextualZoomOutControl());
	  	
	  	var icon = new GIcon(G_DEFAULT_ICON);
	  	
	  	var baseIcon = new GIcon();
  		baseIcon.iconSize=new GSize(25,31);
  		baseIcon.iconAnchor=new GPoint(13,34);
  		baseIcon.infoWindowAnchor=new GPoint(13,3);
  		var picto_url = document.getElementById('picto_google_map').value;
  		var icon = new GIcon(baseIcon, picto_url, null,null);
  		
		var maxMapScale = 15;
		var mapTypes = map.getMapTypes();
		for (var i=0; i<mapTypes.length; i++) {
			mapTypes[i].getMaximumResolution = function() {return maxMapScale;}
		}
	
		var found = true;
		var index = 0;
		while(found && index < 50)
		{
			index ++;
			
			var agence = document.getElementById('agence_'+index);
			if(agence)
			{
				var tabAgence = agence.value.split('##');
				var marker =new GMarker(new GLatLng(tabAgence[0], tabAgence[1]), {icon: icon, draggable:false});	
				markerInfo[tabAgence[0]+'##'+tabAgence[1]] = index;			
			
				GEvent.addListener(marker, "click", function(point) {

					var id = markerInfo[point.lat()+'##'+point.lng()];								
					var info = document.getElementById('info_agence_'+id);
					
					if(info)
					{	
						var html = info.innerHTML;
						map.closeInfoWindow();
						
						var tempMarker = new GMarker(new GLatLng(point.lat(),point.lng()));
						
						tempMarker.openExtInfoWindow(map,"extInfoWindow_funkyBox_beak",html,{beakOffset: 3});
						
						//marker.openInfoWindowHtml(html); 
					}
			  			  		
			  	});
  				
				addCoordonnees(tabAgence[0],tabAgence[1]);
				map.addOverlay(marker);	
			}
			else
			{
				found = false;	
			}
		}
	
		//BBO : les coordonnees GII ne sont pas fiables
		if(document.getElementById('latitude').value>0 && document.getElementById('longitude').value>0 && document.getElementById('zoom').value>0)
		{
			map.setCenter(new GLatLng(document.getElementById('latitude').value, document.getElementById('longitude').value), 12);
			
		}else if(document.getElementById('latitudeArrondissement') && document.getElementById('longitudeArrondissement') && document.getElementById('zoomArrondissement'))
		{
			//map.setCenter(new GLatLng(document.getElementById('latitudeArrondissement').value, document.getElementById('longitudeArrondissement').value), document.getElementById('zoomArrondissement').value);
			
			map.setCenter(new GLatLng(document.getElementById('latitudeArrondissement').value, document.getElementById('longitudeArrondissement').value), 14);
			
		}
		else
		{
		
			center_google_map(extremites["minLat"],extremites["minLong"],extremites["maxLat"],extremites["maxLong"]);
		
		}
		
		for (var id in other_agence) {
						
			var marker =new GMarker(new GLatLng(other_agence[id]["latitude"], other_agence[id]["longitude"]), {icon: icon, draggable:false});	
			eval("GEvent.addListener(marker, \"click\", function() {show_agency('"+id+"')});");
			map.addOverlay(marker);	
			
		}
	}
}


function show_agency(id)
{
	//On ferme toutes les infowindow ouvertes
	map.closeInfoWindow();
	
	//Recuperation ajax
	var xhr = createXHR();
    	xhr.onreadystatechange=function()
    	{
      		if(xhr.readyState==4)
      		{
      			var html = xhr.responseText;
			var tempMarker = new GMarker(new GLatLng(other_agence[id]["latitude"],other_agence[id]["longitude"]));	
			tempMarker.openExtInfoWindow(map,"extInfoWindow_funkyBox_beak",html,{beakOffset: 3});	
        	}
        }
      
    	xhr.open("GET","ajax/get_detail_agence.ajax.php?id="+id,true);
    	xhr.send(null);
		
}



function addCoordonnees(latitude, longitude)
{	
	if(extremites.length == 0)
	{
		extremites["minLat"] = latitude;
		extremites["maxLat"] = latitude;
		extremites["minLong"] = longitude;
		extremites["maxLong"] = longitude;
	}
	else
	{
		if(latitude < extremites["minLat"])
		{
			extremites["minLat"] = 	latitude;
		}
		if(latitude > extremites["maxLat"])
		{
			extremites["maxLat"] = 	latitude;
		}
		if(longitude < extremites["minLong"])
		{
			extremites["minLong"] = 	longitude;
		}
		if(longitude > extremites["maxLong"])
		{
			extremites["maxLong"] = 	longitude;
		}
	}
}

function center_google_map(minLat, minLong, maxLat, maxLong) {
	
	
	var bounds = new GLatLngBounds; 
	bounds.extend(new GLatLng(minLat, minLong)); 
	bounds.extend(new GLatLng(maxLat, maxLong)); 
	
	var zoom = map.getBoundsZoomLevel(bounds);
	if(zoom > 12)
	{
		zoom = 12;
	}
	
	map.setCenter(bounds.getCenter(), zoom); 
}








