// JavaScript Document
function initMap() {
	if (true){//!document.ie6 || document.chargeCarte){
		if ($("#midmap").length==1 && GBrowserIsCompatible()) {
			map = new GMap2(document.getElementById("midmap"));
			map.setCenter(new GLatLng(45.301142, 5.548624), 9);
			map.setUIToDefault();
		
//			map.addControl(new GSmallMapControl());
//			map.addControl(new GMapTypeControl());
//			map.disableScrollWheelZoom();
			map.setMapType(G_PHYSICAL_MAP);
			
			geoKml = new GGeoXml("http://www.reaap38.fr/38-Isere.kml");
			map.addOverlay(geoKml);
			
			document.baseIcon = new GIcon(G_DEFAULT_ICON);
			document.baseIconOver = new GIcon(document.baseIcon);

			document.markers=new Array();
			document.markersOver=new Array();
			
			var cp=0;
			$(".action .adresse").each(function(){
				var adresse=$(this).html();
				findLocation(adresse,cp);
				cp++;
			});/**/
		}
	} else {
		$("#midmap").html("<a href='#'><img src='images/pb-carte-ie.jpg' /></a>");
		$("#midmap a").click(function(){
			$("#midmap").html('<img src="../images/ajax-mid.gif" alt="Carte géographique" />');
			document.chargeCarte=true;
			initMap();
		});
	}
}

function initLitMap() {
	if ($("#litmap").length==1 && GBrowserIsCompatible()) {
		var adresse=$("#litmap .adresse").html();
		map = new GMap2(document.getElementById("litmap"));
		map.setCenter(new GLatLng(45.301142, 5.548624), 8);
		map.setUIToDefault();
//			map.addControl(new GSmallMapControl());
//			map.addControl(new GMapTypeControl());
//			map.disableScrollWheelZoom();
		map.setMapType(G_NORMAL_MAP);
		
		geoKml = new GGeoXml("http://www.reaap38.fr/38-Isere.kml");
		map.addOverlay(geoKml);
		
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		document.baseIcon = new GIcon(G_DEFAULT_ICON);

		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		document.baseIconOver = new GIcon(document.baseIcon);
		
		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		document.couponIcon = new GIcon(document.baseIcon);

		// Create a base icon for all of our markers that specifies the
		// shadow, icon dimensions, etc.
		document.couponIconOver = new GIcon(document.couponIcon);

		// Creates a marker whose info window displays the letter corresponding
		// to the given index.

		document.markers=new Array();
		document.markersOver=new Array();

		var cp=0;
		$(".action .adresse").each(function(){
			var adresse=$(this).html();
			findLocation(adresse,cp);
			cp++;
		});
		
		
	}
}

function createStructureMarker(point, index, iconStyle) {
	// Create a lettered icon for this point using our icon class
	
	markerOptions = { icon:iconStyle };
	
	var marker = new GMarker(point, markerOptions);
	
	marker.index=index;
	
	GEvent.addListener(marker, "click", function() {
		var elt=$(".action .adresse").eq(marker.index).parent().parent();
		var texte=$(".desc",elt).html();

		var adresse=$(".action .adresse").eq(marker.index).html();
		var posAt=adresse.indexOf("@");
		if (posAt>=0){
			adresse=adresse.substr(0,posAt);
		}
		texte+=adresse;
		var etiquetteOptions={maxWidth: 217};
		marker.openInfoWindowHtml('<div class="etiquetteMap">'+texte+'</div>',etiquetteOptions);
	});/**/
	
	return marker;
}

function findLocation(addressTxt,index) {
	if (addressTxt.indexOf("@")<0){
		addressTxt+=" FRANCE";
	}
	$.get("address2latlng.php",
		{index: index, address: addressTxt},
		function(data){
			var reg=new RegExp(",", "g");
			var tab=data.split(reg);
			
			var point=new GLatLng(tab[2],tab[1]);
			var index=tab[0]*1;
			
			var marker = createStructureMarker(point,index,document.baseIcon);
			map.addOverlay(marker);
			document.markers[index]=marker;
			
			var markerOver = createStructureMarker(point,index,document.baseIconOver);
			map.addOverlay(markerOver);
			document.markersOver[index]=markerOver;
			
			markerOver.hide();
			
		}
	);
}

function survol(index,bool){
	if (bool){
		document.markers[index].hide();
		document.markersOver[index].show();
	} else {
		document.markers[index].show();
		document.markersOver[index].hide();
	}
}
