var map = null;
var geocoder = null;

function gmaps() {
	if (GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById("gmaps"));
		map.setUIToDefault();
		//map.addMapType(G_SATELLITE_3D_MAP);
        map.setCenter(new GLatLng(46.860056, 2.0), 5);
		geocoder = new GClientGeocoder();
		for (var i=0; i<datas.length; i++) {
			var marker = addMarker(datas[i]);
		}
	}
}

function gmaps_locate_fr(search) {
	if (geocoder) {
		geocoder.getLatLng(search,
			function(point) {
				if (point) {
					map.closeInfoWindow();
					map.setCenter(point,8);
				}
			}
		);
	}
}

function addMarker(data) {
	if (geocoder) {
		geocoder.getLatLng(
			data.address +','+ data.city +','+ data.country,
			function(point) {
				if (!point){ addMarker(data); }
				//if (point) {
					var marker = createMarker(point,data);
					addClickevent(marker);
					map.addOverlay(marker);
				//}
			}
		);
	}
}

function createMarker(point, data) {
	var markerOpts = {};
	//if (data.address2) { data.address += '<br>' + data.address2; }

	var myIcon		= new GIcon(G_DEFAULT_ICON);
	myIcon.image	= "http://www.qualipluie.com/images/pointer_blue_small.png";
	myIcon.shadow	= "http://www.qualipluie.com/images/blank.png";
	myIcon.iconSize	= new GSize(32,37);
	markerOpts.icon	= myIcon;

	markerOpts.title = data.title + ' : Cliquez pour voir l\'adresse';
	var marker = new GMarker(point, markerOpts);
	marker.content = ['<font face="arial"><b>', data.title, '</b><br>', data.contact, '<br><br>', data.address, '<br>', data.city, '<br>Tél : ', data.phone, '<br><br>', data.email, '<br>', data.url].join('');
	return marker;           	  
}

function addClickevent(marker) {
	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindowHtml(marker.content);
	});
	return marker;
}
