var isAlreadyGMap = false;

function makeGMap() 
{
	/**
	 * googlemap
	 *
	 */
	if ( !bSteno && $('gmap') )
	{
		//Gmap
		if (GBrowserIsCompatible()) {

			var oBounds = new GLatLngBounds();
			
			/**
			 * create a new marker with custom info bulle
			 *
			 * @param array
			 * @return object GMarker
			 */
			var oGCustomOverlay = null;
			function createMarker(aObj)
			{
				//create marker
				var oMarker = new GMarker(
					new GLatLng(
						aObj[0], 
						aObj[1]	
					), { icon: oIcon }
				);

				//html
				var sHtml = '';
				sHtml += '<div class="marker_top"></div>';
				sHtml += '<a href="javascript:void(0);" class="marker_close"></a>';
				if ( aObj[7] != '' && aObj[8] != '' )
				{ // ajout au carnet de voyage
					var bLocalized = (( aObj[0] != '' && aObj[1] != '') ? "1" : "0");
					var sCartLink = "'" + aObj[6] + "','" + aObj[2] + "','"+ aObj[7] + "','" + aObj[8] + "','" + bLocalized +"', 3";
					/* XITI Event on addToCart */
					var sXTClick = '';
					try
					{
						if ($type(xt_click) == 'function')
						{
							sXTClick = "xt_click(this, 'C', '"+ ($chk(iXITILevel) ? iXITILevel : 30) +"', '"+aObj[6]+"','A')";
						}
					}
					catch(err)
					{
						sXTClick = '';
					}
					/* END XITI Event */
					sHtml += '<a class="add_fav_gmap_link" onclick="add_selection('+sCartLink+');'+sXTClick+';return false;" href="javascript:void(0);"><img  src="img/global/picto_tools_addfavorite_green.gif" alt="Ajouter au carnet de voyage" /></a>';
				}
				sHtml += '<a href="' + aObj[6] + '" class="marker_content">';
				if (aObj[4] != '')
					sHtml += '<img src="' + aObj[4] + '" alt="' + aObj[2] + '" title="' + aObj[2] + '" />';
				sHtml += '<span>';
				sHtml += '<span class="title">' + aObj[2] + '</span>';
				if ( aObj[3] != '' ) sHtml += '<span class="commune">' + aObj[3] + '</span>';
				sHtml += '<span class="desc">' + aObj[5] + '</span>';
				sHtml += '</span>';
				sHtml += '</a>';
				sHtml += '<div class="marker_bottom"></div>';

				//
				GEvent.addListener(oMarker, 'click', function(coordinates) {
					//remove element
					if ( oGCustomOverlay != null )
						oMap.removeOverlay(oGCustomOverlay);

					//change center point of view
					oMap.panTo(new GLatLng(this.getPoint().lat(), this.getPoint().lng()));

					oGCustomOverlay = new GCustomOverlay(this, sHtml, -100, -120);

					//add custom div on map
					oMap.addOverlay(oGCustomOverlay);

				});

				return oMarker;
			} /* end of createMarker function */

			var oMap = new GMap2(document.getElementById('gmap'));

			oMap.setMapType(G_PHYSICAL_MAP);
			//add zoom and move control
			oMap.addControl(new GSmallMapControl());

			//new icon
			var oIcon = new GIcon(G_DEFAULT_ICON);
			
			if ( document.getElementById('gmapData') )
			{
				if ( document.getElementById('gmapData').icon && document.getElementById('gmapData').icon.value != '' )
					oIcon.image = document.getElementById('gmapData').icon.value;
				else
					oIcon.image = "img/global/marker_" + document.getElementById('gmapData').season_color.value + ".gif";
			}
			
			oIcon.iconSize = new GSize(17, 20);
			oIcon.shadowSize = new GSize(0,0);
			
			var sHtml = '';
			var aMarkerGmap = $$('.objGoogleMap');
			aMarkerGmap.each(function(el, i){
			
				aObj = el.value.split('¤');

				oMarker = createMarker(aObj);
				//add marker in map
				oMap.addOverlay(oMarker);
				oBounds.extend(oMarker.getLatLng());

			});
			var iZoom = 11;
			if ( aMarkerGmap.length > 1 )
				iZoom = oMap.getBoundsZoomLevel(oBounds);

			try
			{
				if ( $chk(bIsSubTerritory) &&  bIsSubTerritory)
					iZoom -= 2;
			}
			catch(e)
			{
				
			}
			oMap.setCenter(oBounds.getCenter(), iZoom);
			
			//addEvent on gmap to close infobulle
			$('gmap').addEvent('click', function(){
				if ( oGCustomOverlay != null )
					oMap.removeOverlay(oGCustomOverlay);
				oGCustomOverlay = null;
			});
			
			isAlreadyGMap = true;
		}
	}
}
