var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.iconSize = new GSize(32,32);
baseIcon.shadowSize = new GSize(0,0);
baseIcon.iconAnchor = new GPoint(0,0);
baseIcon.infoWindowAnchor = new GPoint(0, 0);

Event.observe(window,'load',function()
{
	
	tab_map = new Array();
	
	if($("carte_pos_actuelle")) tab_map.push("carte_pos_actuelle"); // creermap("carte_pos_actuelle", "dernier_point");
	
	if($("carte_parcours_previsionnel")) tab_map.push("carte_parcours_previsionnel"); //creermap("carte_parcours_previsionnel", "tout");
	
	creermap();
	
	if($('bt_agrandir_colonne'))
	{
		Event.observe('bt_agrandir_colonne','click',function(event)
		{
			if($('bt_agrandir_colonne').hasClassName('reduire'))
			{
				new Effect.Parallel([
					new Effect.Morph('carte_colonne',{style:'width:280px;height:180px;'}),
					new Effect.Morph('cont_carte',{style:'height:117px;'}),
					new Effect.Morph('carte_pos_actuelle',{style:'width:279px;height:117px;'})
					],{ 
					duration: 0.5,
					beforeStart : function()
					{
						tab_map[0].removeControl(control1);
						tab_map[0].removeControl(control2);
					},
					afterFinish:function()
					{
						$('bt_agrandir_colonne').removeClassName('reduire');
						$('bt_agrandir_colonne').update(LG_agrandir);
						tab_map[0].checkResize();
						tab_map[0].removeOverlay(geoXml[0]);
						point_selection = new GLatLng(geoXml[0].ma.markers[0].latlng.lat, geoXml[0].ma.markers[0].latlng.lng);
						var IconType = new GIcon(baseIcon);
						IconType.image = geoXml[0].ma.markers[0].image;
						marker = {
							icon:IconType,
							clickable : false
						};
						position = new GMarker(point_selection, marker);
						tab_map[0].setCenter(point_selection, 15);
						tab_map[0].addOverlay(position);
					}
				});
			}
			else
			{
				new Effect.Parallel([
					new Effect.Morph('carte_colonne',{style:'width:904px;height:500px;'}),
					new Effect.Morph('cont_carte',{style:'height:435px;'}),
					new Effect.Morph('carte_pos_actuelle',{style:'width:902px;height:435px;'})
					],{ 
				  	duration: 0.5,
				  	afterFinish:function()
					{
						$('bt_agrandir_colonne').addClassName('reduire');
						$('bt_agrandir_colonne').update(LG_reduire);
						cbGeo = geoXml[0].getDefaultCenter();
						tab_map[0].checkResize();
						zoom = tab_map[0].getBoundsZoomLevel(boundGeo[0]);
						
						tab_map[0].setCenter(new GLatLng(cbGeo.y,cbGeo.x), zoom);
						tab_map[0].clearOverlays();
						tab_map[0].addOverlay(geoXml[0]);
						
						control1 = new GLargeMapControl3D();
						control2 = new GMapTypeControl();
						
						tab_map[0].addControl(control1);
						tab_map[0].addControl(control2);
				  	}
				});
			}
			event.stop();
		});
	}	
});

var creermap = function()
{
	var rand = Math.floor(Math.random()*100000000);
	
	geocoder = new Array();
	geoXml = new Array();
	cbGeo = new Array();
	boundGeo = new Array();
	pos = new Array();
	zoom = new Array();
	
	tab_map.each(function(el,i)
	{
		tab_map[i] = new GMap2($(el));
		tab_map[i].setMapType(G_HYBRID_MAP);
		geocoder[i] = new GClientGeocoder();
	
		if(el == "carte_parcours_previsionnel")
		{
			tab_map[i].addControl(new GSmallMapControl());
			tab_map[i].addControl(new GMapTypeControl());
		}
		
		/* IMPORT KML */
		
		//http://download.agence-clark.com/aleister/trace.kml

		geoXml[i] = new GGeoXml("http://la-boudeuse.org/wp-content/uploads/trace.kml?v="+rand, function()
		{
			//db(geoXml[0]);
			cbGeo[i] = geoXml[i].getDefaultCenter();
			boundGeo[i] = geoXml[i].getDefaultBounds();

			//pos[i] = geoXml[i].mc.length - 1;
			if($('position_navire')){
				$('position_navire').update(geoXml[0].ma.markers[0].name);
			}
			
			zoom[i] = tab_map[i].getBoundsZoomLevel(boundGeo[i]);
			
			if(el == "carte_pos_actuelle")
			{
				point_selection = new GLatLng(geoXml[0].ma.markers[0].latlng.lat, geoXml[0].ma.markers[0].latlng.lng);
				var IconType = new GIcon(baseIcon);
				IconType.image = geoXml[0].ma.markers[0].image;
				marker = {
					icon:IconType,
					clickable : false
				};
				var position = new GMarker(point_selection, marker);
				tab_map[0].setCenter(point_selection, 13);
				tab_map[i].addOverlay(position);
			}
			else
			{
				tab_map[i].setCenter(new GLatLng(cbGeo[i].y, cbGeo[i].x), zoom[i]);
				tab_map[i].addOverlay(geoXml[i]);
			}
		});
		/* FIN IMPORT KML */
	});
}