/*
API Google Maps pour les lieux public de la ville
*/

function objClone(what,rec) {
    for (var i in what) {
       if ((rec) && (typeof what[i]=="object"))
           this[i] = new objClone(what[i],true);
       else
           this[i] = what[i];
    }
}

// Variables globales
var googleMapsZoom = 14;
var googleMapsTypeId = google.maps.MapTypeId.ROADMAP;
var googleMapsTypeControl = google.maps.MapTypeControlStyle.HORIZONTAL_BAR;
var googleMapsNavigationControl = google.maps.NavigationControlStyle.DEFAULT;
var googleMapsIcon = 'local/images/google-maps-icon.png';
var map;

function initializeGeneralGoogleMaps () {

	var divs = document.getElementsByTagName("div");
	
	for (i = 0; i < divs.length; i++) {
		
		switch (divs[i].className) {
		
		case 'spGoogleMaps':
			var divGoogleMapsCode = divs[i].id;
			var myLat = document.getElementById(divGoogleMapsCode+'_latitude');
			var myLon = document.getElementById(divGoogleMapsCode+'_longitude');
			var myNom = document.getElementById(divGoogleMapsCode+'_nom');
			var myLatlng = new google.maps.LatLng(myLat.innerHTML,myLon.innerHTML);

			// Options d'affichage d'une carte Google
			var myOptions = {
				zoom: googleMapsZoom,
				disableDefaultUI: true,
				center: myLatlng,
				mapTypeControl: true,
				mapTypeControlOptions: {style: googleMapsTypeControl},
				navigationControl: true,
				navigationControlOptions: {style: googleMapsNavigationControl},
				mapTypeId: googleMapsTypeId
			};
			
			// Carte Google
			map = new google.maps.Map(document.getElementById(divGoogleMapsCode), myOptions);
			map.scrollwheel = false; 

			// Marker
		    var marker = new google.maps.Marker({
		        position: myLatlng, 
		        map: map,
		        title: myNom.innerHTML
		    });
		    break;
		    
		case 'entiteGoogleMaps':
			var divGoogleMapsCode = divs[i].id;
			var myLat = document.getElementById(divGoogleMapsCode+'_latitude');
			var myLon = document.getElementById(divGoogleMapsCode+'_longitude');
			var myNom = document.getElementById(divGoogleMapsCode+'_nom');
			var myLatlng = new google.maps.LatLng(myLat.innerHTML,myLon.innerHTML);

			// Options d'affichage d'une carte Google
			var myOptions = {
				zoom: googleMapsZoom,
				disableDefaultUI: true,
				center: myLatlng,
				mapTypeControl: true,
				mapTypeControlOptions: {style: googleMapsTypeControl},
				navigationControl: true,
				navigationControlOptions: {style: googleMapsNavigationControl},
				mapTypeId: googleMapsTypeId
			};
			
			// Carte Google
			map = new google.maps.Map(document.getElementById(divGoogleMapsCode), myOptions);
			map.scrollwheel = false; 

			// Marker
		    var marker = new google.maps.Marker({
		        position: myLatlng, 
		        map: map,
		        title: myNom.innerHTML
		    });
		    break;
		    
		case 'entitesGoogleMaps':
			var divGoogleMapsCode = divs[i].id;
			
			var parags = new Array ();
			var tmp = divs[i].getElementsByTagName("p");
			var latMin = 90, latMax = 0;
			var lonMin = 180, lonMax = -180;
			for (j = 0; j < tmp.length; j++) {
				var pGoogleMapsCode = tmp[j].id;
				parags.push(new Array(
						document.getElementById(pGoogleMapsCode+'_latitude').innerHTML
						, document.getElementById(pGoogleMapsCode+'_longitude').innerHTML
						, document.getElementById(pGoogleMapsCode+'_nom').innerHTML
						, parseInt(document.getElementById(pGoogleMapsCode+'_position').innerHTML)
				));
				var currLat = parseFloat(document.getElementById(pGoogleMapsCode+'_latitude').innerHTML);
				var currLon = parseFloat(document.getElementById(pGoogleMapsCode+'_longitude').innerHTML);
				latMin = Math.min(latMin,currLat);
				lonMin = Math.min(lonMin,currLon);
				latMax = Math.max(latMax,currLat);
				lonMax = Math.max(lonMax,currLon);
			}

			// Options d'affichage d'une carte Google
			var myOptions = {
				disableDefaultUI: true,
				mapTypeControl: true,
				mapTypeControlOptions: {style: googleMapsTypeControl},
				navigationControl: true,
				navigationControlOptions: {style: googleMapsNavigationControl},
				mapTypeId: googleMapsTypeId
			};
			
			// Carte Google
			map = new google.maps.Map(document.getElementById(divGoogleMapsCode), myOptions);
			//map.setTilt(45);
			map.scrollwheel = false;
			
			botLeft = new google.maps.LatLng(latMin,lonMin);
			topRight = new google.maps.LatLng(latMax,lonMax);
			bounds = new google.maps.LatLngBounds(botLeft,topRight);
			map.setCenter(new google.maps.LatLng(((latMax+latMin)/2.0),((lonMax+lonMin)/2.0)));
			map.fitBounds(bounds);

			var markers = [];
			for (j = 0; j < parags.length; j++) {
				var myLat = parags[j][0];
				var myLon = parags[j][1];
				var myNom = parags[j][2];
				var myLatlng = new google.maps.LatLng(myLat,myLon);
	
				// Marker
			    var marker = new google.maps.Marker({
			        position: myLatlng, 
			        map: map,
			        title: myNom,
					icon: "http://www.ville-pont-audemer.fr/images/google-maps-icons/numbers/marker"+parags[j][3]+".png"
			    });
				markers.push(marker);
			}
			var markerCluster = new MarkerClusterer(map, markers);
			break;

		case 'itineraireGoogleMaps':
			var divGoogleMapsCode = divs[i].id;
			
			var parags = new Array ();
			var tmp = divs[i].getElementsByTagName("p");
			var latMin = 90, latMax = 0;
			var lonMin = 180, lonMax = -180;
			for (j = 0; j < tmp.length; j++) {
				var pGoogleMapsCode = tmp[j].id;
				parags.push(new Array(
						document.getElementById(pGoogleMapsCode+'_latitude').innerHTML
						, document.getElementById(pGoogleMapsCode+'_longitude').innerHTML
						, document.getElementById(pGoogleMapsCode+'_nom').innerHTML
						, parseInt(document.getElementById(pGoogleMapsCode+'_position').innerHTML)
				));
				var currLat = parseFloat(document.getElementById(pGoogleMapsCode+'_latitude').innerHTML);
				var currLon = parseFloat(document.getElementById(pGoogleMapsCode+'_longitude').innerHTML);
				latMin = Math.min(latMin,currLat);
				lonMin = Math.min(lonMin,currLon);
				latMax = Math.max(latMax,currLat);
				lonMax = Math.max(lonMax,currLon);
			}

			// Options d'affichage d'une carte Google
			var myOptions = {
				disableDefaultUI: true,
				mapTypeControl: true,
				mapTypeControlOptions: {style: googleMapsTypeControl},
				navigationControl: true,
				navigationControlOptions: {style: googleMapsNavigationControl},
				mapTypeId: googleMapsTypeId
			};
			
		        // Carte Google
			map = new google.maps.Map(document.getElementById(divGoogleMapsCode), myOptions);
			map.scrollwheel = false;

		   	botLeft = new google.maps.LatLng(latMin,lonMin);
			topRight = new google.maps.LatLng(latMax,lonMax);
			bounds = new google.maps.LatLngBounds(botLeft,topRight);
			map.setCenter(new google.maps.LatLng(((latMax+latMin)/2.0),((lonMax+lonMin)/2.0)));
			map.fitBounds(bounds);

			var waypoints = [];
			for (j = 0; j < parags.length; j++) {
				var myLat = parags[j][0];
				var myLon = parags[j][1];
				var myNom = parags[j][2];
				var myLatlng = new google.maps.LatLng(myLat,myLon);

				waypoints[j] = {location:myLatlng,stopover:false};
				var marker = new google.maps.Marker({
			        position: myLatlng, 
			        map: map,
			        title: myNom,
					icon: "http://www.ville-pont-audemer.fr/images/google-maps-icons/numbers/marker"+parags[j][3]+".png"
			    });
			}
			
			// Limitation de la version 3 de Google Maps
			if (waypoints.length < 9) {
				var directionsRenderer = new google.maps.DirectionsRenderer();
				directionsRenderer.setMap(map);
				var directionsService = new google.maps.DirectionsService();
	
				var request = {
					origin: new google.maps.LatLng(parags[0][0],parags[0][1]),
					destination: new google.maps.LatLng(parags[parags.length-1][0],parags[parags.length-1][1]),
					travelMode: google.maps.DirectionsTravelMode.DRIVING,
					waypoints: waypoints
				};
	
				directionsService.route(request, function(response, status) {
					if (status == google.maps.DirectionsStatus.OK) {
						directionsRenderer.setDirections(response);
					}
				});
			}
			
			break;

		case 'entiteGoogleMapsKml':
			var divGoogleMapsCode = divs[i].id;
			var kml = document.getElementById(divGoogleMapsCode+'_kml');
			var myLatlng = new google.maps.LatLng(49.343467,0.519104);

			// Options d'affichage d'une carte Google
			var myOptions = {
				zoom: 12,
				center: myLatlng,
				mapTypeControl: true,
				mapTypeControlOptions: {style: googleMapsTypeControl},
				navigationControl: true,
				navigationControlOptions: {style: googleMapsNavigationControl},
				mapTypeId: googleMapsTypeId
			};
			
			// Carte Google
			map = new google.maps.Map(document.getElementById(divGoogleMapsCode), myOptions);
			map.scrollwheel = false; 

			// Options de KmlLayer 
			var myOptionsKml = {
				preserveViewport: true
			};

			// KmlLayer
			var ctaLayer = new google.maps.KmlLayer(kml.innerHTML, myOptionsKml);
			ctaLayer.setMap(map);
		    break;

		}
	}
}

// Tableaux globaux 
// 0:code - 1:latitude - 2:longitude - 3:nom - 4:site internet - 5:icône
var localMaps = new Array (
	new Array ("mairie",49.3573757,0.5164647,"Mairie","http://www.ville-pont-audemer.fr/contact/mairie.php","administration.png")
	,new Array ("eclat",49.3558243,0.5180311,"L'Eclat","http://eclat.ville-pont-audemer.fr/presentation.php","theater.png")
	,new Array ("crc",49.3567607,0.5154991,"Conservatoire à Rayonnement Communal","http://www.ville-pont-audemer.fr/culture/conservatoire.php","music.png")
);
var nbMaps = localMaps.length;
var contents = new Array (nbMaps);
var infos = new Array (nbMaps);
var latlng = new Array (nbMaps);

function initializeEclat () {
		
	// Recherche de l'élément Google Maps
	var divGoogleMapsCode = "google_maps_";
	var arrayDiv = document.getElementsByTagName("div");
	var chaine = "";
	var id = -1;
	var code = "";
	var i;

	i = 0;
	while (code == ""  && i < arrayDiv.length) {
		var chaine = arrayDiv[i].id;
		var res = chaine.indexOf(divGoogleMapsCode);
		if  (res == 0) {
			code = chaine.substring(divGoogleMapsCode.length, chaine.length);
		}
		i++;
	}
	i = 0;
	while (id < 0 && i < nbMaps) {
		if (code == localMaps[i][0]) {
			id = i;
		}
		i++;
	}

	for (i=0; i<nbMaps; i++) {
		latlng[i] = new google.maps.LatLng(localMaps[i][1],localMaps[i][2]);
	}

	// Options d'affichage d'une carte Google
	var myOptions = {
		zoom: 16,
		disableDefaultUI: true,
		center: latlng[id],
		mapTypeControl: true,
		mapTypeControlOptions: {style: googleMapsTypeControl},
		navigationControl: true,
		navigationControlOptions: {style: googleMapsNavigationControl},
		mapTypeId: googleMapsTypeId
	};

	map = new google.maps.Map(document.getElementById(divGoogleMapsCode+code), myOptions);
	map.scrollwheel = false; 
	
	for (i=0; i<nbMaps; i++) {
		contents[i] = '<div id="googleMapsContent">'+
			'<h1 id="firstHeading" class="firstHeading">'+localMaps[i][3]+'</h1>'+
			'<div id="googleMapsBody"><a href="'+localMaps[i][4]+
			'" title="Coordonnées (remplace la fenêtre existante)">'+
			'Coordonnées postales, téléphoniques, courriels, ...'+
			'</a>'+
			'</div>';
			
		var marker = new google.maps.Marker({
			position: latlng[i], 
			map: map, 
			title: localMaps[i][3],
			icon: "http://www.ville-pont-audemer.fr/images/google-maps-icons/"+localMaps[i][5]
		});
		
		attachMessage(marker,i);

	}		
}

function attachMessage (marker, number) {
	var infowindow = new google.maps.InfoWindow(
		{ content: contents[number]	}
	);

	google.maps.event.addListener(marker, 'click', function() {
		infowindow.open(map,marker);
	});
	
}

function createXhrObject () {
    if (window.XMLHttpRequest)
        return new XMLHttpRequest();
 
    if (window.ActiveXObject) {
        var names = [
            "Msxml2.XMLHTTP.6.0",
            "Msxml2.XMLHTTP.3.0",
            "Msxml2.XMLHTTP",
            "Microsoft.XMLHTTP"
        ];
        for(var i in names) {
            try{ return new ActiveXObject(names[i]); }
            catch(e){}
        }
    }
    return null; // non supporté
}

xhr = createXhrObject();

