var geocoder;
var map;
  
function initialize() {
	geocoder = new google.maps.Geocoder();

	var latlng = new google.maps.LatLng(43.6603818, 7.1952049000000215);//43.920701, 7.177142
	var myOptions = {
	  zoom: 10,
	  center: latlng,
	  mapTypeId: google.maps.MapTypeId.ROADMAP
	}

	map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
	/*var nyLayer = new google.maps.KmlLayer('Map/alpesMaritimes.kml');
	nyLayer.setMap(map);*/
	var shadow = new google.maps.MarkerImage('map/Map/images/cdgpuce.png',
  // The shadow image is larger in the horizontal dimension
  // while the position and offset are the same as for the main image.
			new google.maps.Size(34, 43),
			new google.maps.Point(0,0),
			new google.maps.Point(15, 43));
	var LatLng = new google.maps.LatLng(43.6603818, 7.1952049000000215);
	var marker = new google.maps.Marker({
				map: map,
				position: LatLng,
				icon: shadow,
				animation: google.maps.Animation.DROP,
				title:"CDG06"
	});
	var infowindow = new google.maps.InfoWindow(
			{ content: "<div align='center'>CDG06<br/><br/><a href='http://www.cdg06.fr' target='_blank'  ><img border='none' src='map/Map/images/web.png' alt='' /></a>",
				size: new google.maps.Size(50,50)
			});
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(map,marker);
			});

}

function codeAddress(Monadresse,Libelle,Type,mail,web) {
	var address = Monadresse;
	var contenu =""
	var testWeb ="";
	address = address.replace('(','');
	address = address.replace(')','');
	address = address.split(",") ;
	addressx = address[0];
	addressy =	address[1];
	var img = "";
	if(Type == "commune"){img = "PinMap.png";} else {img = "PinMapRed.png";}
	//var site = Site;
	contenu = 	"<div align='center'>"+Libelle+"<br/><br/>";
	
	testWeb = web.substring(0,2);
	

	if(testWeb != "04") {contenu += "<a href='http://"+web+"' target='_blank'  ><img border='none' src='map/Map/images/web.png' alt='' /></a>";}
	else {contenu += "<table><tr><td><img border='none' src='map/Map/images/call.png' alt='' /></td><td>"+web+"</td></tr></table>";}
	
	
	
	if(mail != "null") {contenu += "&nbsp;&nbsp;<a href='mailto:"+mail+"'><img border='none' src='map/Map/images/emailButton.png' alt='' /></a></div>"; }							
	//if(mail != "null") {
	
	var shadow = new google.maps.MarkerImage('map/Map/images/'+img,
  // The shadow image is larger in the horizontal dimension
  // while the position and offset are the same as for the main image.
			new google.maps.Size(37, 32),
			new google.maps.Point(0,0),
			new google.maps.Point(0, 32));
			var myLatLng = new google.maps.LatLng(addressx,addressy);
			var marker = new google.maps.Marker({
				map: map, 
				icon: shadow,
				position: myLatLng,
				animation: google.maps.Animation.DROP,
				title:Libelle
			});
			
			var infowindow = new google.maps.InfoWindow(
			{ content: contenu,
				size: new google.maps.Size(50,50)
			});
			google.maps.event.addListener(marker, 'click', function() {
				infowindow.open(map,marker);
			});
}


function ajaxMap() {
    var xhr=null;
    
    if (window.XMLHttpRequest) { 
        xhr = new XMLHttpRequest();
    }
    else if (window.ActiveXObject) 
    {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
    }
    //on définit l'appel de la fonction au retour serveur
	
    xhr.onreadystatechange = function() { alert_ajax(xhr); };
    
	xhr.open("GET", "map/Map/Affilier.xml.php", true);
	//xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); 
    xhr.send();
	
}

function alert_ajax(xhr){
	var docXML= xhr.responseXML;
	var itemsLibelle = docXML.getElementsByTagName("Libelle");
	var itemsAdresse = docXML.getElementsByTagName("Adresse");
	var itemsWeb = docXML.getElementsByTagName("web");
	var itemsMail = docXML.getElementsByTagName("mail");
	var itemsType = docXML.getElementsByTagName("Type");
	
	var i =0;
	for (j=0;j< itemsLibelle.length;j++) {
				codeAddress(itemsAdresse.item(j).firstChild.data,itemsLibelle.item(j).firstChild.data,itemsType.item(j).firstChild.data,itemsMail.item(j).firstChild.data,itemsWeb.item(j).firstChild.data);
	}
}
