var DEFAULT_ZOOM_LEVEL = 15;
	
var listeRendezVous = [];
var listeResidences = [];
var map;
var mgr;
var rdvIcon;
var otherIcon;
var imgPath;
var currentRendezVous;

function initialize()
{
	initializeData();
	initializeMap();
}


function initializeMarkers()
{
	for (var i = 0; i < listeRendezVous.length; i++)
	{
		var rendezVous = listeRendezVous[i];
		var latlong    = rendezVous.latlong;
		if (latlong != null && latlong != '')
		{
			var point  = getLatLong (latlong);
			var marker = createMarker (null, point, "rendezVous", i);	
			rendezVous.marker = marker;
		}
	}
	
	for (var i = 0; i < listeResidences.length; i++)
	{
		var residence = listeResidences[i];
		var latlong   = residence.latlong;
		if (latlong != null && latlong != '')
		{
			var point  = getLatLong (latlong);
			var marker = createMarker (null, point, "residence", i);
			residence.marker = marker;
		}
	}
	
	/*
	if(listeRendezVous.length > 0)
	{
		markerClicked(listeRendezVous[0].marker);
		currentRendezVous = listeRendezVous[0].id;
	}
	*/
}


function newMarker (type, index)
{
	var list  = getListFromType (type);
	var obj   = list[index];
	var point = map.getCenter();
	
	var marker = createMarker (null, point, type, index);
	
	obj.marker = marker;
	
	initializeLists();
	
	showInfoWindow (type, index);
}


function listeClicked (type, index)
{
	var marker = getMarkerForTypeAtIndex (type, index);
	var latlong = marker.getLatLng();

	map.panTo (latlong); 
	
	showInfoWindow (type, index);
}


function initializeMap ()
{
	if (!google.maps.BrowserIsCompatible()) return;
	
	var myMap    = document.getElementById ("map");
	var geoCoder = new google.maps.ClientGeocoder ();
	map          = new google.maps.Map2 (myMap);
	rdvIcon      = new google.maps.Icon (google.maps.DEFAULT_ICON, imgPath + "sucetteEsf-false.png");
	otherIcon    = new google.maps.Icon (google.maps.DEFAULT_ICON, imgPath + "sucette-false.png");
	
	map.addMapType (G_PHYSICAL_MAP);
	map.setMapType (G_PHYSICAL_MAP);
	
	map.addControl (new google.maps.LargeMapControl());
	map.addControl (new google.maps.MenuMapTypeControl());
	
	map.disableDoubleClickZoom();
	map.enableContinuousZoom();
	map.enableScrollWheelZoom();
   
   if (mapCenter == '')
   {
   	  geoCoder.getLatLng (station + ", France", centerMap);
   }
   else
   {
   	  centerMap (getLatLong (mapCenter));
   }
}


function centerMap (point)
{
	if (point == null) return;

   	map.setCenter (point, mapZoomLevel);
    mgr = new google.maps.MarkerManager (map);
    
    initializeMarkers();
}


function createMarker (overlay, point, type, index)
{
	var icon       = (type == "rendezVous") ? rdvIcon : otherIcon;
	var marker     = new google.maps.Marker (point, {icon:icon});
	var click_func = function() { markerClicked (marker); };
	
	marker.valraiso       = {};
	marker.valraiso.type  = type;
	marker.valraiso.index = index;
	
	google.maps.Event.addListener (marker, "click", click_func);
	
	mgr.addMarker (marker, 1);
	mgr.refresh();
		   
	return marker;
}


function markerClicked (marker)
{
	var type  = marker.valraiso.type;
	var index = marker.valraiso.index;
	
	showInfoWindow (type, index);
}


function showInfoWindow (type, index)
{
	var list   = getListFromType (type);
	var obj    = list[index];
	var marker = obj.marker;
	var text   = obj.text;
	
	if (type == "rendezVous")
	{
		currentRendezVous = obj.id;
	}
	
	marker.openInfoWindowHtml (text);	
}


function getMarkerForTypeAtIndex (type, index)
{
	var list = getListFromType (type);
	var obj  = list[index];
	
	return obj.marker;
}


function getListFromType (type)
{
	var list;
	
	if (type == "rendezVous") list = listeRendezVous;
	else if (type == "residence") list = listeResidences;
	
	return list;
}


function getGPSCoord (marker)
{
   var point = marker.getPoint();
   
   return point.y + "," + point.x;
}


function getLatLong (str)
{
	var arr = str.split(',');
	return new google.maps.LatLng (arr[0], arr[1]);
}


function saveGeoData ()
{
	var str       = '';
	var geoData   = document.getElementById ("geoData");
	var geoZoom   = document.getElementById ("geoZoom");
	var geoCenter = document.getElementById ("geoCenter");
	var point     = map.getCenter();
	
	str = fillData (listeRendezVous, 'rendezVous', str);
	str = fillData (listeResidences, 'residence', str);
	
	geoData.value   = str;
	geoZoom.value   = map.getZoom();
	geoCenter.value = point.y + "," + point.x;
	
	return true;
}


function fillData (list, type, str)
{	
	for (var i = 0; i < list.length; i++)
	{
		var item   = list[i];
		var id     = item.id;
		var marker = item.marker;
		
		if (marker != null)
		{
			if (str.length > 0)
			{
				str += "|";
			}
			str += (type + '@' + id + '@' + getGPSCoord (marker));
		}
	}
	
	return str;
}

