
$(function() {
	var section = top.location.pathname;		
	if(section == '/contact.php') 
	{
		initGoogleMapsBureaux();
	}	
});

// Fonction qui affiche une carte Google Maps des bureaux
function initGoogleMapsBureaux() {
	if (GBrowserIsCompatible()) {
		$('#map').css({'height' : '300px', 'width' : '400px'});
		var map = document.getElementById("map");
		
		var bureauxToShow = location.hash;
		bureauxToShow = bureauxToShow.replace(/#/g,'');
		
		var m = new GMap2(map);
		m.setCenter(new GLatLng(46.31658418182218, -73.4710693359375), 8);
		m.savePosition();
		m.addControl(new GLargeMapControl());
		var c = new GMapTypeControl();
		m.addControl(c);
		
		var nbreBureaux = $('#bureaux > div').length;

		for(i=0 ; i<nbreBureaux; i++) {
			// On extrait la latitude/longitude contenue dans un element specifie par la classe .p_latitude_longitude
			var latitudeLongitude = $('#bureaux > div:nth-child(' + (i+1) + ') .p_latitude_longitude').text();
			latitudeLongitude = latitudeLongitude.split(',');
			
			// On extrait le id de l'element div contenant les informations sur le bureau
			var idBureau = $('#bureaux > div:nth-child(' + (i+1) + ')').attr('id');

			// On extrait le title de l'element div contenant les informations sur le bureau
			var nomBureau = $('#bureaux > div:nth-child(' + (i+1) + ')').attr('title');

			// Si l'element extrait semble correspondre a une latitude/longitude
			if(latitudeLongitude.length == 2) {
				var tinyIcon = new GIcon();
				tinyIcon.image = "http://www.google.com/mapfiles/marker.png";
				tinyIcon.iconSize = new GSize(20, 34);
				tinyIcon.iconAnchor = new GPoint(13, 38);
				tinyIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
				tinyIcon.shadowSize = new GSize(37, 34);
				tinyIcon.infoWindowAnchor = new GPoint(13, 0);
				markerOptions = { icon:tinyIcon, title: nomBureau};

				var infos = $('#bureaux > div:nth-child(' + (i+1) + ')').html();
				var type = markerOptions;
				var point = new GLatLng(parseFloat(latitudeLongitude[0]),parseFloat(latitudeLongitude[1]));
					
				var marker = createMarker(point, infos, type, m);
            	m.addOverlay(marker);
				
				if(idBureau == bureauxToShow) {
					GEvent.trigger(marker, "click");
				}
			}
		}

	}
	else {
		$('#map').html('<p style="color: #474747;">La carte permettant de localiser nos bureaux ne peut s\'afficher, car votre fureteur n\'est pas compatible avec Google Maps.</p>');
	}
}


// Fonction qui crÃ©e les Â«markersÂ» pour la carte Google Maps des communautÃ©s cries
function createMarker(point, infos, type, m) {
	var marker = new GMarker(point, type);
	var html = '<div class="infobulle" style="width: 300px; height: 90px; overflow: auto;">' + infos + '</div>';
	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindow(html,{maxWidth : 450});
	});
	
	GEvent.addListener(marker, 'infowindowclose', function() {
		m.returnToSavedPosition() ;
	});
	
	return marker;
}


