//<![CDATA[
//
// variables globales
//var iHotel = new Array('http://maps.google.com/mapfiles/kml/pal2/icon28.png', 'http://maps.google.com/mapfiles/kml/pal2/icon28s.png');
//var iRestaurant = new Array('http://maps.google.com/mapfiles/kml/pal2/icon40.png', 'http://maps.google.com/mapfiles/kml/pal2/icon40s.png');
//var iGolf = new Array('http://maps.google.com/mapfiles/kml/pal2/icon13.png', 'http://maps.google.com/mapfiles/kml/pal2/icon13s.png');
//var iActivite = new Array('http://maps.google.com/mapfiles/kml/pal2/icon57.png', 'http://maps.google.com/mapfiles/kml/pal2/icon57s.png');
//var iNature = new Array('http://maps.google.com/mapfiles/kml/pal2/icon12.png','http://maps.google.com/mapfiles/kml/pal2/icon12s.png');
//var iEglise = new Array('http://maps.google.com/mapfiles/kml/pal2/icon11.png', 'http://maps.google.com/mapfiles/kml/pal2/icon11s.png');
//var iChapiteau = new Array('http://maps.google.com/mapfiles/kml/pal3/icon29.png', 'http://maps.google.com/mapfiles/kml/pal3/icon29s.png');
//var iCinema = new Array('http://maps.google.com/mapfiles/kml/pal2/icon30.png', 'http://maps.google.com/mapfiles/kml/pal2/icon30s.png');
var iPunaise = new Array('http://' + location.host + '/appclients/lama/map/pin.png', 'http://' + location.host + '/appclients/lama/map/pin_shadow.png');
var iPunaiseND = new Array('http://' + location.host + '/appclients/lama/map/pin_nd2.png', 'http://' + location.host + '/appclients/lama/map/pin_shadow.png');
var iSkieur = new Array('http://' + location.host + '/appclients/lama/map/ski.png', 'http://' + location.host + '/appclients/lama/map/ski_shadow.png');

var map;			// Stock les informations de base de la carte
var gmarkers = [];
var htmls = [];
var i = 0;
var postIt = new Array();

// Coordonnées maxi et mini du dragg de la carte
var allowedBounds = new GLatLngBounds(new GLatLng(45.66, 5.9), new GLatLng(46.15, 6.65)); 

// Création de l'icône personnalisée de base
var baseIcon = new GIcon();
baseIcon.iconSize = new GSize(14.0, 22.0);
baseIcon.shadowSize = new GSize(26.0, 22.0);
baseIcon.iconAnchor = new GPoint(7.0, 22.0);
baseIcon.infoWindowAnchor = new GPoint(7.0, 0);

//Icône de skieur
var skiIcon = new GIcon();
skiIcon.iconSize = new GSize(40.0, 28.0);
skiIcon.shadowSize = new GSize(55.0, 28.0);
skiIcon.iconAnchor = new GPoint(20.0, 28.0);
skiIcon.infoWindowAnchor = new GPoint(20.0, 28.0);
		
function initialize() {

	// Check to see if this browser can run the Google API
	if (GBrowserIsCompatible()) {
		// Display the map, with some controls and set the initial location 
		map = new GMap2(document.getElementById('map_canvas'));

		// ====== Restricting the range of Zoom Levels =====
		// Get the list of map types      
		var mt = map.getMapTypes();
		// Overwrite the getMinimumResolution() and getMaximumResolution() methods
		for (var i=0; i<mt.length; i++) {
			mt[i].getMinimumResolution = function() {return 11;}
			mt[i].getMaximumResolution = function() {return 17;}
		}

		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl());
		map.setCenter(new GLatLng(45.841791, 6.272485), 10);
		map.enableScrollWheelZoom();

		// Add a move listener to restrict the bounds range
		GEvent.addListener(map, 'move', function() {
			checkBounds();
		});
	}
}

// If the map position is out of range, move it back
var checkBoundsCounter = 0;
function checkBounds() {
	// Perform the check and return if OK

	checkBoundsCounter++;
	if (checkBoundsCounter % 2 > 0) {
		//Si on enlève cette condition la fonction est exécutée trop souvent et ça fait geler l'écran
		//Si on met une valeur de plus de 2 pour le modulo on obtient un effet de tremblement quand on arrive aux limites
		//Exécuter la fonction 1 fois sur 2 corrige les deux problèmes
	 	return;
	 }
	 checkBoundsCounter = 0;
	 
	if (allowedBounds.contains(map.getBounds().getSouthWest()) && allowedBounds.contains(map.getBounds().getNorthEast())) {
		return;
	}
	
	// It`s not OK, so find the nearest allowed point and move there
	var X1 = map.getBounds().getSouthWest().lng();
	var Y1 = map.getBounds().getSouthWest().lat();
	var X2 = map.getBounds().getNorthEast().lng();
	var Y2 = map.getBounds().getNorthEast().lat();
	var X = map.getCenter().lng();
	var Y = map.getCenter().lat();
	
	var AminX = allowedBounds.getSouthWest().lng();
	var AminY = allowedBounds.getSouthWest().lat();
	var AmaxX = allowedBounds.getNorthEast().lng();
	var AmaxY = allowedBounds.getNorthEast().lat();
	
	if (X1 < AminX) {X += (AminX - X1);} 
	if (X2 > AmaxX) {X -= (X2 - AmaxX);}
	if (Y1 < AminY) {Y += (AminY - Y1);}
	if (Y2 > AmaxY) {Y -= (Y2 - AmaxY);}
	
	map.setCenter(new GLatLng(Y, X));
}

// A function to create the marker and set up the event window
// Dont try to unroll this function. It has to be here for the function closure

// Each instance of the function preserves the contends of a different instance
// of the "marker" and "html" variables which will be needed later when the event triggers.    
function createMarker(point, image, title, html) {

	// Create our "tiny" marker Icon
	if (image != undefined) {
		var Icon = new GIcon(baseIcon);
		Icon.image = image[0];
		Icon.shadow = image[1];

		// Set up our GMarkerOptions object
		markerOptions = { icon:Icon };
		var marker = new GMarker(point, markerOptions);
	}
	else {
		var marker = new GMarker(point);
	}
	var postItIndex = i;
	GEvent.addListener(marker, 'click', function() {
		showPostIt(postItIndex, 0)
	});
	
	gmarkers[i] = marker
	htmls[i] = html;

	i++;
 	var label = new ELabel(point, title, "shortLabel", new GSize(15, 9), 85, false);
 	GEvent.addListener(marker, "mouseover", function () {label.show()});
 	GEvent.addListener(marker, "mouseout", function () {label.hide()});
 	map.addOverlay(label);
 	label.hide();	
	
	return marker;
}

function showPostIt(postItIndex, postItSubIndex)
{
	if (postIt[postItIndex][postItSubIndex][2].indexOf('<!--TITRE2-->') >= 0)
	{
		//Récupérer les infos pour bâtir la fiche
		var xmlhttp = GetXmlHttpObject();
		if (xmlhttp != null)
		{
			xmlhttp.onreadystatechange = function()
			{
				if (xmlhttp.readyState == 4)
				{
					if (xmlhttp.responseXML.documentElement.getElementsByTagName('Titre2')[0].firstChild != null)
						postIt[postItIndex][postItSubIndex][2] = postIt[postItIndex][postItSubIndex][2].replace('<!--TITRE2-->', xmlhttp.responseXML.documentElement.getElementsByTagName('Titre2')[0].firstChild.nodeValue);
						
					if (xmlhttp.responseXML.documentElement.getElementsByTagName('Description')[0].firstChild != null)						
						postIt[postItIndex][postItSubIndex][2] = postIt[postItIndex][postItSubIndex][2].replace('<!--DESCRIPTION-->', xmlhttp.responseXML.documentElement.getElementsByTagName('Description')[0].firstChild.nodeValue);
					
					if (xmlhttp.responseXML.documentElement.getElementsByTagName('Image')[0].firstChild != null)
						if (xmlhttp.responseXML.documentElement.getElementsByTagName('TitreComplet')[0].firstChild != null)
							postIt[postItIndex][postItSubIndex][2] = postIt[postItIndex][postItSubIndex][2].replace('<!--PHOTO-->', '<img src="' + xmlhttp.responseXML.documentElement.getElementsByTagName('Image')[0].firstChild.nodeValue + '" alt="' + xmlhttp.responseXML.documentElement.getElementsByTagName('TitreComplet')[0].firstChild.nodeValue + '" />');
						else
							postIt[postItIndex][postItSubIndex][2] = postIt[postItIndex][postItSubIndex][2].replace('<!--PHOTO-->', '<img src="' + xmlhttp.responseXML.documentElement.getElementsByTagName('Image')[0].firstChild.nodeValue + '" />');

					if (xmlhttp.responseXML.documentElement.getElementsByTagName('Adresse')[0].firstChild != null)						
						postIt[postItIndex][postItSubIndex][2] = postIt[postItIndex][postItSubIndex][2].replace('<!--ADRESSE-->', xmlhttp.responseXML.documentElement.getElementsByTagName('Adresse')[0].firstChild.nodeValue);
					
					showMarker(postItIndex, postItSubIndex);
				}
			};
			
			xmlhttp.open('GET', 'Ajax/InfoResultatMap.asp?Resultat=' + postIt[postItIndex][postItSubIndex][3], true);
			xmlhttp.send(null);
		}
	}
	else
	{
		//Les infos ont déjà été récupérées
		showMarker(postItIndex, postItSubIndex);
	}
}

function showMarker(postItIndex, postItSubIndex)
{
	htmls[postItIndex] = '<div id="postIt' + postItIndex + '" class="postIt">';
	htmls[postItIndex] += postIt[postItIndex][postItSubIndex][2];
	htmls[postItIndex] += '</div>';
	gmarkers[postItIndex].openInfoWindowHtml(htmls[postItIndex]);
}
					
function addSkiStation()
{
	//Affichage d'icônes sur les stations de ski
	
	var aujourdhui = new Date();
	
	//Affichage des icônes de septembre à avril seulement
	if (FormatSMD(aujourdhui, 'MMDD') >= '0901' || FormatSMD(aujourdhui, 'MMDD') <= '0413') {
		var Icon = new GIcon(skiIcon);
		Icon.image = iSkieur[0];
		Icon.shadow = iSkieur[1];
		markerOptions = { icon:Icon };
		//La Cluzaz
		map.addOverlay(new GMarker(new GLatLng(45.906733, 6.426659), markerOptions));
		//Saint-Jean-de-Sixt
		map.addOverlay(new GMarker(new GLatLng(45.925105, 6.411681), markerOptions));
		//Le Grand Bornand
		map.addOverlay(new GMarker(new GLatLng(45.943033, 6.429405), markerOptions));
		//Manigod
		map.addOverlay(new GMarker(new GLatLng(45.862628, 6.369324), markerOptions));
		//Seythenex
		map.addOverlay(new GMarker(new GLatLng(45.726555, 6.299286), markerOptions));
		//Semnoz
		map.addOverlay(new GMarker(new GLatLng(45.795297, 6.101532), markerOptions));
	}
}

function setMapPosition(latitude, longitude, zoomFactor)
{
	map.setCenter(new GLatLng(latitude, longitude), zoomFactor);
}

//]]> 