// JavaScript Document
var centerLatt=0;
var centerLong=0;
var carte="";
var carte2="";
var first=1;
var gmarkers=[];
var bounds = "";

//---Fonction d'affichage Onglet
function GGload() {
	if (GBrowserIsCompatible()) {
	
		carte = new GMap2(document.getElementById("map"));
		var minLatt=99999;
		var maxLatt=-99999;
		var minLong=99999;
		var maxLong=-99999;
		zoom=parseInt(zoomMap);
		bounds = new GLatLngBounds();
		
		for (var i = 1; i <= listeLatt.length; i++) {
			if(listeLatt[i-1]!="" && listeLong[i-1]!=""){	
				if(listeLatt[i-1]<minLatt){ minLatt=listeLatt[i-1]; }
				if(listeLatt[i-1]>maxLatt){ maxLatt=listeLatt[i-1]; }
				if(listeLong[i-1]<minLong){ minLong=listeLong[i-1]; }
				if(listeLong[i-1]>maxLong){ maxLong=listeLong[i-1]; }
			}
		}
		
		function createMarker(point, index) {
			
			var icon = new GIcon();
			icon.image = "";
			icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			icon.iconSize = new GSize(15, 21);
			icon.shadowSize = new GSize(22, 20);
			icon.iconAnchor = new GPoint(3, 21);
			icon.infoWindowAnchor = new GPoint(5, 1);
			var marker = new GMarker(point);
			marker.id=index;
			if(index>0){
				if(listeInfobulle.length > 0){
					GEvent.addListener(marker, "click", function() {
						marker.openInfoWindowHtml(listeInfobulle[index-1]);
					});
				}
			}
			gmarkers[index]=marker;
			return marker;
		}
		
		centerLatt=parseFloat(minLatt)+parseFloat((maxLatt-minLatt)/2);
		centerLong=parseFloat(minLong)+parseFloat((maxLong-minLong)/2);
		carte.addControl(new GSmallMapControl());
		carte.setCenter(new GLatLng('46.965259400349275', '2.52685546875'), zoom, G_PHYSICAL_MAP);
		
		for (var i = 1; i <= listeLatt.length; i++) {
			var point = new GLatLng(listeLatt[i-1],listeLong[i-1]);
			carte.addOverlay(createMarker(point, i));
			bounds.extend(point);
		}
		
		carte.setZoom(carte.getBoundsZoomLevel(bounds));
		carte.setCenter(bounds.getCenter());
	}
}

function ChangeLocation(i) {
	carte.setCenter(new GLatLng(listeLatt[i], listeLong[i]), 10);
	carte.openInfoWindowHtml(new GLatLng(listeLatt[i], listeLong[i]), listeInfobulle[i]);
}

function GGloadPts() {
	if (GBrowserIsCompatible()) {
	
		carte = new GMap2(document.getElementById("map"));
		var minLatt=99999;
		var maxLatt=-99999;
		var minLong=99999;
		var maxLong=-99999;
		zoom=parseInt(7);
		bounds = new GLatLngBounds();
		
		for (var i = 1; i <= listeLatt.length; i++) {
			if(listeLatt[i-1]!="" && listeLong[i-1]!=""){	
				if(listeLatt[i-1]<minLatt){ minLatt=listeLatt[i-1]; }
				if(listeLatt[i-1]>maxLatt){ maxLatt=listeLatt[i-1]; }
				if(listeLong[i-1]<minLong){ minLong=listeLong[i-1]; }
				if(listeLong[i-1]>maxLong){ maxLong=listeLong[i-1]; }
			}
		}
		
		function createMarker(point, index) {
			
			var icon = new GIcon();
			icon.image = "";
			icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
			icon.iconSize = new GSize(15, 21);
			icon.shadowSize = new GSize(22, 20);
			icon.iconAnchor = new GPoint(3, 21);
			icon.infoWindowAnchor = new GPoint(5, 1);
			var marker = new GMarker(point);
			marker.id=index;
			gmarkers[index]=marker;
			return marker;
		}
		
		centerLatt=parseFloat(minLatt)+parseFloat((maxLatt-minLatt)/2);
		centerLong=parseFloat(minLong)+parseFloat((maxLong-minLong)/2);
		carte.addControl(new GSmallMapControl());
		carte.setCenter(new GLatLng('46.965259400349275', '2.52685546875'), zoom, G_PHYSICAL_MAP);
		
		for (var i = 1; i <= listeLatt.length; i++) {
			var point = new GLatLng(listeLatt[i-1],listeLong[i-1]);
			carte.addOverlay(createMarker(point, i));
			bounds.extend(point);
		}
		
		carte.setCenter(bounds.getCenter());
	}
}
//---

var map = null;
var markers = [];
var markerClusterer = null;
function initialize() {
	if(GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('map'));
		map.setCenter(new GLatLng(39.91, 116.38), 2, G_HYBRID_MAP);
		map.addControl(new GSmallMapControl());
		bounds = new GLatLngBounds();
		
		var icon = new GIcon(G_DEFAULT_ICON);
		icon.image = "/graphiques/site/picto/marker.png";
		icon.iconSize = new GSize(30, 40);
		
		var markers = [];
		
		for (var i = 0; i < data.count; ++i) {
			var latlng = new GLatLng(data.spots[i].latitude, data.spots[i].longitude);
			var marker = new GMarker(latlng, {icon: icon});
			var fn = markerClickFn(data.spots[i].spot_titre, data.spots[i].spot_lieu, data.spots[i].spot_lien, latlng);
       		GEvent.addListener(marker, "click", fn);
			markers.push(marker);
			bounds.extend(latlng);
		}
		var markerCluster = new MarkerClusterer(map, markers);
		
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
	}
}
function initializeStructure() {
	if(GBrowserIsCompatible()) {
		map = new GMap2(document.getElementById('map'));
		map.setCenter(new GLatLng(latinit, loninit), zoomMap, G_HYBRID_MAP);
		map.addControl(new GSmallMapControl());
		bounds = new GLatLngBounds();
		
		var icon = new GIcon(G_DEFAULT_ICON);
		icon.image = "/graphiques/site/picto/marker.png";
		icon.iconSize = new GSize(30, 40);
		
		var markers = [];
		
		for (var i = 0; i < data.count; ++i) {
			var latlng = new GLatLng(data.structures[i].latitude, data.structures[i].longitude);
			var marker = new GMarker(latlng, {icon: icon});
			var fn = markerClickFn(data.structures[i].structure_titre, data.structures[i].structure_lieu, data.structures[i].structure_lien, latlng);
       		GEvent.addListener(marker, "click", fn);
			markers.push(marker);
			bounds.extend(latlng);
		}
		var markerCluster = new MarkerClusterer(map, markers);
		
		map.setZoom(map.getBoundsZoomLevel(bounds));
		map.setCenter(bounds.getCenter());
	}
}
function markerClickFn(spot_titre, spot_lieu, spot_lien, latlng) {
	return function() {
	var titre = spot_titre;
	var lieu = spot_lieu;
	var lien = spot_lien;
	var infoHtml = '<div style="width:300px;"><b>' + titre
	 + '</b><br />'
	 + '<b>Lieu : </b>' + lieu + '<br />'
	 + '<a href="' + lien + '" class="visi"><b>En savoir plus</b></a></div>';
	map.openInfoWindowHtml(latlng, infoHtml);
	};
}