var oMapManager;
var oMarkerCollection;
var sDomainSheet;
var requestPrestaLiee;

var addLocationEvent = function( oEventsManager )
{
	var aSelect = $$('.blocAlentours select');

	if (aSelect.length > 0)
	{
		aSelect.each(function(elem){
			elem.addEvent('change', function(){
				if (this.value != '')
					window.location = this.value;
			});
		});
	}

	oEventsManager.fireEvent('nextStep');
}

var addDomaineEvent = function( oEventsManager )
{
	var aSelect = $$('.blocEvenements select');

	if (aSelect.length > 0)
	{
		aSelect.each(function(elem){
			elem.addEvent('change', function(){
				if (this.value != '')
					window.location = this.value;
			});
		});
	}

	oEventsManager.fireEvent('nextStep');
}

var addHebEvent = function( oEventsManager )
{
	var aSelectHeb = $$('.blocAccomodation select#accommodation_select');
	var aSelectRes = $$('.blocAccomodation select#accommodation_select_resort');
	
	if (aSelectHeb.length > 0)
	{
		aSelectHeb.each(function(elem){
			elem.addEvent('change', function(){
				if (this.value != '')
				{
					var tmp = this.value.replace("/~~","/~"+aSelectRes[0].value+"~");
					window.location = tmp;
				}
			});
		});
	}

	oEventsManager.fireEvent('nextStep');
}


var addHebResEvent = function( oEventsManager )
{
	var aSelectRes = $$('.blocAccomodation select#accommodation_select_resort');
	var aSelectHeb = $$('.blocAccomodation select#accommodation_select');
	
	if (aSelectRes.length > 0)
	{
		aSelectRes.each(function(elem){
			elem.addEvent('change', function(){
				if (this.value != '')
					aSelectHeb.setStyle( 'display', 'block' );
				else
					aSelectHeb.setStyle( 'display', 'none' );	
			});
		});
		
	}

	oEventsManager.fireEvent('nextStep');
}

var addMapEvent = function( oEventsManager )
{
	var sIdContainer = 'gmap';

	if ($(sIdContainer))
	{
		// init GPS center of map
		var oGPSCenter = new Hash ({
			iLatitude: 45.677401,
			iLongitude: 6.394043
		});

		var iZoom = 9;

		oMapManager = new MapManager( sIdContainer, {
			oGPSCenter: oGPSCenter,
			iZoomValue: iZoom,
			iMapTypeID: google.maps.MapTypeId.TERRAIN
		});
		oMapManager.initMap();

		oMapManager.addEvent( 'mapLoaded', function mapLoaded()
		{
			this.removeEvent( 'mapLoaded', mapLoaded );
			oEventsManager.fireEvent('nextStep');
		});
	}
	else
		oEventsManager.fireEvent('nextStep');
}

var addSliderItemBtnEvent = function( oEventsManager )
{
	// get container for list
	var oContainerItemList = $$('.carto table .objects_list .objects_viewport');

	// set height for one item
	var iHeightItem = 57;

	// get top button for slide
	var oClickTop = $$('.carto table td#clickable_top');
	if( oClickTop )
	{
		// call moveTo on click
		oClickTop.addEvent( 'click', function(){
			moveTo.run( [oContainerItemList, iHeightItem] );
		});

		// roll over
		oClickTop.addEvent( 'mouseenter', function(){
			this.getElements('img').set('src', './img/global/landing/arrow_up_roll.gif');
		});

		oClickTop.addEvent( 'mouseleave', function(){
			this.getElements('img').set('src', './img/global/landing/arrow_up.gif');
		});
	}

	// get bottom button for slide
	var oClickBottom = $$('.carto table td#clickable_bottom');
	if( oClickBottom )
	{
		// call moveTo on click
		oClickBottom.addEvent( 'click', function(){
			moveTo.run( [oContainerItemList, -iHeightItem] );
		});

		// roll over
		oClickBottom.addEvent( 'mouseenter', function(){
			this.getElements('img').set('src', './img/global/landing/arrow_down_roll.gif');
		});

		oClickBottom.addEvent( 'mouseleave', function(){
			this.getElements('img').set('src', './img/global/landing/arrow_down.gif');
		});
	}

	// moveTo function for slide item container
	var moveTo = function ( oElement, iValue ){
		var iTopElement 	= oElement.getStyle('top')[0].toInt();
		var iHeightParent 	= oElement.getParent()[0].getHeight();
		var iHeightElement 	= oElement.getHeight();
		var iMaxValue		= iHeightParent - iHeightElement;

		if( iTopElement + iValue >= 0 )
			oElement.setStyle( 'top', 0 );
		else if( iTopElement + iValue < iMaxValue )
			oElement.setStyle( 'top', iMaxValue );
		else
			oElement.setStyle( 'top', iTopElement + iValue );
	}

	oEventsManager.fireEvent('nextStep');
}

var addItemCartoListEvent = function( oEventsManager )
{
	// get items
	var aItem = $$('.carto table .objects_list .objects_viewport .object');
	var aItemClassImg = $$('.carto table .objects_list .objects_viewport .object div.image');
	// add mouse over event on each item
	aItem.each( function( elem, index ){
		elem.addEvent( 'mouseenter', function(e){

			if( e )
				e.stopPropagation();

			// reinit all item with old class value or empty value
			reInitClassItem.run();

			// remove img class if exist
			if( this.hasClass('img') )
				this.removeClass('img');

			// add open class
			this.addClass('open');
		});
	});

	// reinit class on item
	var reInitClassItem = function()
	{
		// remove open class on each item
		aItem.each( function( elem, index ){
			elem.removeClass('open');
		});

		// add img class on item which was img before had opened
		aItemClassImg.each( function( elem, index ){
			var parent = elem.getParent('div');
			if( !parent.hasClass('img') )
				parent.addClass('img');
		});
	}

	oEventsManager.fireEvent('nextStep');
}

var addCartoKMZ = function( oEventsManager )
{
	if ($('infos_hidden'))
	{
		if( $('infos_hidden').plan_kmz )
		{
			var sKMZ = $('infos_hidden').plan_kmz.value;

			new google.maps.KmlLayer(
				sKMZ,
				{
					map: oMapManager.getMap()
				}
			);
		}
	}

	oEventsManager.fireEvent('nextStep');
}

var addCartoMarker = function( oEventsManager )
{
	var form = $('infos_hidden');

	if (form)
	{
		if( form.geoloc || $('address_commune') )
		{
			var iGPSLat 	= null;
			var iGPSLon 	= null;
			var sAddress 	= null;
			var aCoords		= new Array();

			if( form.geoloc.value != '' )
			{
				sGeoloc = form.geoloc.value;
				aCoords = sGeoloc.split(';');

				if( aCoords[0] && aCoords[1] )
				{
					iGPSLat = aCoords[0].replace(',', '.').toFloat();
					iGPSLon = aCoords[1].replace(',', '.').toFloat();
				}
			} else if( $('address_commune') ) {
				sAddress 	= $('address_commune').get('html');
				sAddress 		= sAddress.replace('<br>', ' ');
				sAddress		= sAddress.stripTags();
			}

			// create new marker
			var oMarker = new MapMarker(
				{
					sAddress: sAddress,
					oGPSCoord : new Hash ({
						iLatitude: iGPSLat,
						iLongitude: iGPSLon
					}),
					oIcon :	{ sIconPath : 'img/global/map/flag_red.png' }
				}
			);

			// append marker
			oMarker.addEvent( 'MarkerAppended', function()
			{
				if( !$('infos_hidden').plan_kmz )
				{
					oMapManager.extendBounds( oMarker.getGMarker() );
					oMapManager.setCenterWidthBounds();
					if ($('infos_hidden').fiche_cyclo && $('infos_hidden').fiche_cyclo.value == 1)
					{
					  oMapManager.getMap().setZoom(oMapManager.getMap().getZoom().toInt() - 2);
					}
					else
					{
					  oMapManager.getMap().setZoom(oMapManager.getMap().getZoom().toInt() + 1);
					}
				}

				oEventsManager.fireEvent('nextStep');
			});

			oMarker.addEvent('MarkerAppended', function() {
				this.getGMarker().setClickable(false);
			});

			oMarker.initMarker( oMapManager.getMap() );
		}
		else
			oEventsManager.fireEvent('nextStep');
	}
	else
		oEventsManager.fireEvent('nextStep');
}

var addEventCartoDisplayPrestaLiee = function( oEventsManager )
{
	var sType = '';
	// get lang and id_sitra
	if ($('infos_hidden'))
	{
		var sLang = $('infos_hidden').lang.value;
		var sIdSitra = $('infos_hidden').id_sitra.value;
		
		if ($('infos_hidden').type)
			sType = $('infos_hidden').type.value;
	}
	
	var aLinks = $$('.selection ul li.lien a');
	aLinks.each( function( elem, index ){
		elem.addEvent( 'click', function(e){
			// apply new css style on current button
			if( elem.getParent('li').hasClass('lien') )
			{
				// add class lien on button
				reinitLinkStyle();

				elem.getParent('li').removeClass('lien');

				// launch request
				requestPrestaLiee.run( [this.get('rel'), sIdSitra, sLang ] );
			} else {

				elem.getParent('li').addClass('lien');

				// clear markers and right content
				if( oMarkerCollection instanceof MapMarkerCollection )
				{
					oMarkerCollection.clearMarkers();
				}
				hiddenRightContentContainer.run();
			}
		});
	});

	requestPrestaLiee = function( sType, sIdSitra, sLang ){

		var oLoader 		= $('carto_loader');
		var id_version 		= $('infos_hidden').id_version.value;
		var id 				= $('infos_hidden').id.value;
		var solr_params		= '';

		// get geoloc for cyclo
		if( $('infos_hidden').fiche_cyclo && $('infos_hidden').fiche_cyclo.value == 1 )
		{
			solr_params += '&solr_params=1';

			// get GPSCoord for current map
			if( oMapManager instanceof MapManager )
			{
				var iLatNE 	= oMapManager.getMap().getBounds().getNorthEast().lat();
				var iLongNE = oMapManager.getMap().getBounds().getNorthEast().lng();

				var iLatSW 	= oMapManager.getMap().getBounds().getSouthWest().lat();
				var iLongSW = oMapManager.getMap().getBounds().getSouthWest().lng();

				if( iLatNE != '' && iLongNE != '' && iLatSW != '' && iLongSW != '' )
					solr_params += '&GPSLatNE=' + iLatNE + '&GPSLatSW=' + iLatSW + '&GPSLongNE=' + iLongNE + '&GPSLongSW=' + iLongSW;

			}

			// get classification spec
			if( $('infos_hidden').crit_int_cyclo_heb && $('infos_hidden').crit_int_cyclo_heb.value != '' )
				solr_params += '&crit_interne=' + $('infos_hidden').crit_int_cyclo_heb.value;
		}

		// solr url request
		var myRequest = new Request({
			method: 'post',
			url: '../content/scripts/getLinkObjects.php',
			data: 'id='+sIdSitra+'&lang='+sLang+'&type='+sType+'&id_fiche='+id+'&id_version='+id_version+solr_params,
			onRequest: function(){

				// display loader
				oLoader.setStyle( 'display', 'block' );

				// clear markers
				if( oMarkerCollection instanceof MapMarkerCollection )
					oMarkerCollection.clearMarkers();

				// reinit bound collection
				if( oMapManager instanceof MapManager )
					oMapManager.reinitializeBounds();
			},
			onComplete: function( oReturn ){
				if( oReturn != null )
				{
					var oReturn 	= JSON.decode(oReturn);
					var oData 		= JSON.decode(oReturn.data);
					var sHTMLTpl 	= oReturn.template;
					//var iNbElement	= 0;

					var sHTMLItem = '';
					if (oData.length > 0)
					{
						var iNbElem = 0;
						oData.each( function( elem )
						{
							// display right content
							var sContent = getItemRightContent.run([elem, iNbElem, sHTMLTpl]);
							if( sContent != null )
							{
								sHTMLItem += sContent;
								iNbElem++;
							}
						});
					}
					else
					{
						if ($('empty_result_msg'))
						{
							sHTMLItem += '<p class="empty">' + $('empty_result_msg').get('value') + '</p>';
						}
					}

					// append html in container
					var oContainer = $('objects_viewport');
					if( oContainer )
					{
						oContainer.set( 'html', sHTMLItem );
						oEventsManager.fireEvent('addItemCartoListEvent');
						oEventsManager.fireEvent('displayMarkerOnMap');
					}

					// top container
					oContainer.setStyle('top', 0);

					// display right content container
					displayRightContentContainer.run();

					// display loader
					oLoader.setStyles({ 'display':'none' });
				}
			}
		}).send();
	}

	var getItemRightContent = function( elem, index, sHTMLTpl )
	{
		var sReturn = null;

		// get data
		var sLink		= elem.lien;
		var sTitle		= elem.titre;
		var sTitleComplete	= elem.titre_complete;
		var sDesc		= elem.description;
		var sLat		= elem.latitude;
		var sLon		= elem.longitude;
		var sTown		= elem.commune;
		var sPict		= elem.image;

		if( sTitle != '' && sLat != '' && sLon != '' )
		{
			sReturn = sHTMLTpl.replace( /\{link\}/g, sLink);
			sReturn = sReturn.replace( /\{title\}/g, sTitle );
			sReturn = sReturn.replace( /\{title_complete\}/g, sTitleComplete );
			sReturn = sReturn.replace( /\{desc\}/g, sDesc );
			sReturn = sReturn.replace( /\{latitude\}/g, sLat );
			sReturn = sReturn.replace( /\{longitude\}/g, sLon );
			sReturn = sReturn.replace( /\{address\}/g, sTown );
			sReturn = sReturn.replace( /\{picture\}/g, sPict );
			sReturn = sReturn.replace( /\{number\}/g, index + 1);

			if( index == 0 )
				sReturn = sReturn.replace( /\{style_open\}/g, 'open');
			else
				sReturn = sReturn.replace( /\{style_open\}/g, 'img');
		}

		return sReturn;
	}

	var reinitLinkStyle = function()
	{
		aLinks.each( function( elem, index ){
			if( !elem.getParent('li').hasClass('lien') )
				elem.getParent('li').addClass('lien');
		});
	}

	var displayRightContentContainer = function()
	{
		$$('.carto table .objects_list').setStyle('display', 'block');
		$$('.carto table td.clickable .clickable_arrow').setStyle('display', 'block');
		$$('.carto table td.clickable').setStyle('width', '139px');
		$$('.carto table td .gmap').setStyle('width', '583px');
	}

	var hiddenRightContentContainer = function()
	{
		$$('.carto table .objects_list').setStyle('display', 'none');
		$$('.carto table td.clickable .clickable_arrow').setStyle('display', 'none');
		$$('.carto table td.clickable').setStyle('width', '');
		$$('.carto table td .gmap').setStyle('width', '100%');
	}
	
	if (sType != '')
	{
		// launch request
		requestPrestaLiee.run( [sType, sIdSitra, sLang ] );
	}

	oEventsManager.fireEvent('nextStep');
}

var displayMarkerOnMap = function( elem, index ){

	if( $$('.object') && $$('.object').length > 0 )
	{
		/*######## BUILD PICTO DEFAULT URI ########*/
		var oURI = new URI();
		oURI.set('directory', oURI.get('directory') + '../content/scripts/displayPicto.php');
		/*######## END BUILD PICTO DEFAULT URI ########*/

		/*######## BUILD MARKER COLLECTION ########*/
		//Initialize MarkerCollection object
		oMarkerCollection = new MapMarkerCollection({
			behaviour:{
				multipoints:{
					container: "<table cellspacing='0' cellpadding='0' border='0'>\n\
									<tr>\n\
										<td><img src='img/global/map/picto_blue_left.gif' class='fl' alt='' /></td>\n\
										<td>\n\
											<div class='conteneur' id='conteneur'>\n\
												##CONTENT##\n\
											</div>\n\
										</td>\n\
										<td><img src='img/global/map/picto_blue_right.gif' class='fl' alt='' /></td>\n\
									</tr>\n\
								</table>",
					element: "<a href='javascript:void(0);' title='##CONTENT##' rel='MultiMarker'>##CONTENT##</a>",
					icon: "img/global/map/picto_multi.gif",
					event: function( oMultiPointMarker )
					{
						oMultiPointMarker.addEvent( 'MarkerAppended', function()
						{
							oMultiPointMarker.oMarkerEvents.customover = google.maps.event.addListener(
								oMultiPointMarker.getGMarker(),
								'mouseover',
								function()
								{
									oMultiPointMarker.CollectionData.multiPointOverlay.setPositionFromMarker(this.getGMarker());
									oMultiPointMarker.CollectionData.multiPointOverlay.setMap(this.getGMarker().getMap());
								}.bind(oMultiPointMarker)
							);

							oMultiPointMarker.oMarkerEvents.overlayout = google.maps.event.addListener(
								oMultiPointMarker.CollectionData.multiPointOverlay,
								'overlayLoaded',
								function()
								{
									oMultiPointMarker.CollectionData.multiPointOverlay.element.addEvents({
										'mouseleave': function()
										{
											oMultiPointMarker.CollectionData.multiPointOverlay.setMap(null);
										}.bind(oMultiPointMarker)
									});

									var aChildren = this.getChildrenCollection(oMultiPointMarker.CollectionData.childrenIndex);
									if( aChildren instanceof Array && aChildren.length > 0 )
									{
										aChildren.each( function( oCurrentChild )
										{
											var oLink = oMultiPointMarker.CollectionData.multiPointOverlay.element.getElement('a[title='+oCurrentChild.CollectionData.index+']');
											if( oLink )
											{
												oLink.addEvents({
													'mouseenter': function()
													{
														oCurrentChild.fireEvent('mouseenter');
													},

													'mouseleave': function()
													{
														oCurrentChild.fireEvent('mouseleave');
													}
												})
											}
										});
									}
								}.bind(this)
							);
						}.bind(this));
					}
				}
			}
		});
		/*######## END BUILD MARKER COLLECTION ########*/

		/*######## BUILD ALL MARKERS AND PUSH INSIDE COLLECTION ########*/
		$$('.object').each
		(
			//For each point given
			function( oItem, sIndex )
			{
				var oInput = oItem.getElement("input[name='geoloc']");
				if( oInput )
				{
					var aCoords = oInput.get('value').split(';');
					if( aCoords.length == 2 )
					{
						oURI.clearData();
						oURI.setData('font_size', 2);
						oURI.setData('string', sIndex.toInt() + 1);
						oURI.setData('picto', 'picto_red.gif');
						oURI.setData('font_color', '255|255|255');

						//Build a valid marker object
						oMarkerCollection.pushMarkerWithExactPositionFilter(
							new MapMarker(
								{
									oGPSCoord : new Hash ({
										iLatitude: aCoords[0].replace(',', '.').toFloat(),
										iLongitude: aCoords[1].replace(',', '.').toFloat()
									}),
									oIcon :
									{
										sIconPath : oURI.toString()
									}
								}
							)
						);
					}
				}
			}
		);
		/*######## END BUILD ALL MARKERS AND PUSH INSIDE COLLECTION ########*/

		/*######## SHOW MARKERS AND INITIALIZE ENVENT ON EACH ########*/
		oMarkerCollection.removeEvents();
		oMarkerCollection.addEvent(
			'AllMarkerAppended',
			function()
			{
				// if obt is a domain wa have to centerBound on markers of stations
				if( sDomainSheet && sDomainSheet == true )
				{
					oMapManager.setCenterWidthBounds();
					oMapManager.getMap().setZoom(oMapManager.getMap().getZoom().toInt() - 1);
					oMapManager.getMap().panBy( 60, 0 );
				}

				/**############ INIT ALL EVENTS ON APPENDED MARKERS ############*/
				var oSynchronousEventsFunction = function( aSelector )
				{
					if( aSelector && aSelector.length > 0 )
					{
						var oCurrentHighlight;
						var aTotalMarkers = this.getAllValidMarkers();

						aTotalMarkers.each(
							//For each filtered markers array
							function( oItem )
							{
								oCurrentHighlight = null;
								var iIndex = oItem.CollectionData.index - 1;

								/**############ IF GOOGLE MARKER HAVE BEEN INITIALIZED ############*/
								if( oItem.getGMarker() )
								{
									/**############ INIT GMAP MARKER EVENTS ############*/
									oItem.oMarkerEvents.customover = google.maps.event.addListener(
										oItem.getGMarker(),
										'customover',
										function()
										{
											if( this.oIcon.sIconPath  != null && this.CollectionData.childrenIndex == null )
											{
												this.oIcon.sIconPath = this.oIcon.sIconPath.replace('picto_red', 'picto_blue');
												this.getGMarker().setIcon( this.getCustomMarkerImage() );
											}
										}.bind(oItem)
									);

									oItem.oMarkerEvents.customout = google.maps.event.addListener(
										oItem.getGMarker(),
										'customout',
										function()
										{
											if( this.oIcon.sIconPath  != null && this.CollectionData.childrenIndex == null )
											{
												this.oIcon.sIconPath = this.oIcon.sIconPath.replace('picto_blue', 'picto_red');
												this.getGMarker().setIcon( this.getCustomMarkerImage() );
											}
										}.bind(oItem)
									);

									oItem.oMarkerEvents.mouseover = google.maps.event.addListener(
										oItem.getGMarker(),
										'mouseover',
										function()
										{
											if( oCurrentHighlight instanceof MapMarker )
											{
												if( oCurrentHighlight.CollectionData.parentIndex == null )
													google.maps.event.trigger(oCurrentHighlight.getGMarker(), 'customout');
												else if( oCurrentHighlight.CollectionData.parentIndex != null )
													this.getParent( oCurrentHighlight.CollectionData.parentIndex ).CollectionData.multiPointOverlay.setMap(null);
											}

											if( oItem.oIcon.sIconPath  != null && oItem.CollectionData.childrenIndex == null )
											{
												oItem.oIcon.sIconPath = oItem.oIcon.sIconPath.replace('picto_red', 'picto_blue');
												oItem.getGMarker().setIcon( oItem.getCustomMarkerImage() );
											}
											oItem.fireEvent('mouseenter');

											oCurrentHighlight = oItem;
										}.bind(this)
									);
									/**############ END INIT GMAP MARKER EVENTS ############*/
								}
								/**############ END IF GOOGLE MARKER HAVE BEEN INITIALIZED ############*/

								/**############ INTERACTION BETWEEN LISTE AND MARKER ############*/
								if( aSelector[iIndex] )
								{
									aSelector[iIndex].addEvents({
										'mouseenter': function()
										{
											if( oCurrentHighlight instanceof MapMarker )
											{
												if( oCurrentHighlight.CollectionData.parentIndex == null )
													google.maps.event.trigger(oCurrentHighlight.getGMarker(), 'customout');
												else if( oCurrentHighlight.CollectionData.parentIndex != null )
													this.getParent( oCurrentHighlight.CollectionData.parentIndex ).CollectionData.multiPointOverlay.setMap(null);
											}

											if( oItem.CollectionData.parentIndex == null )
												google.maps.event.trigger(oItem.getGMarker(), 'customover');
											else if( oItem.CollectionData.parentIndex != null )
											{
												var oMarker = this.getParent( oItem.CollectionData.parentIndex );
												oMarker.CollectionData.multiPointOverlay.setPositionFromMarker(oMarker.getGMarker());
												oMarker.CollectionData.multiPointOverlay.setMap(oMarker.getGMarker().getMap());
											}

											oCurrentHighlight = oItem;
										}.bind(this)
									});
								}
								/**############ END INTERACTION BETWEEN LIST AND MARKER ############*/

								/**############ INTERACTION BETWEEN MARKER AND LIST ############*/
								oItem.addEvents({
									'mouseenter': function()
									{
										if( aSelector[iIndex] )
										{
											if( $chk(aSelector[iIndex].onmouseover))
												aSelector[iIndex].onmouseover();
											else
												aSelector[iIndex].fireEvent('mouseenter');

											if( $('objects_list') )
											{
												var oElement = aSelector[iIndex];
												var oElementPosition = oElement.getPosition($('objects_list'));
												var oParentPosition = oElement.getParent().getPosition($('objects_list'));

												if( oElementPosition.y + 150 > $('objects_list').getStyle('height').toInt() )
												{
													oElement.getParent().setStyle(
														'top',
														-1 * ( oElementPosition.y + 150 + Math.abs(oParentPosition.y) - $('objects_list').getStyle('height').toInt() )
													);
												}
												else if( oElementPosition.y < 0 )
												{
													oElement.getParent().setStyle(
														'top',
														oParentPosition.y + Math.abs(oElementPosition.y)
													);
												}
											}
										}
									},

									'mouseleave': function()
									{
										if( aSelector[iIndex] )
										{
											if( $chk(aSelector[iIndex].onmouseout))
												aSelector[iIndex].onmouseout();
											else
												aSelector[iIndex].fireEvent('mouseleave');
										}
									}
								});
								/**############ END INTERACTION BETWEEN MARKER AND LIST ############*/
							}.bind(this)
						);
					}
				}
				oSynchronousEventsFunction.bind( this ).run( [ $$('.object') ] );
				/**############ END INIT ALL EVENTS ON APPENDED MARKERS ############*/

				oEventsManager.fireEvent('nextStep');
			}
		);

		oMarkerCollection.showMarkers( oMapManager );
		/*######## END SHOW MARKERS AND INITIALIZE ENVENT ON EACH ########*/
	}
	else
		oEventsManager.fireEvent('nextStep');
}

var cartoFollowScroll = function( oEventsManager )
{
	if ( $$('.gmap_container #gmap') && $$('.gmap_container #gmap').length > 0 )
	{
		var iMapPosition = $('gmap').getPosition();

		$$('.gmap_container').setStyle('position', 'relative');

		window.addEvent('scroll', function()
		{
			if ( window.getScroll() )
			{
				var iScrollY = window.getScroll().y;
				if ( iScrollY > iMapPosition.y )
				{
					var iNewPos =  (iScrollY.toInt() - iMapPosition.y.toInt());
					$('gmap').setStyles({ 'top' : iNewPos + 'px', 'margin-top' : '15px'});
				}
				else
					$('gmap').setStyles({ 'top' : 0, 'margin-top' : 0});
			}
		});
	}

	oEventsManager.fireEvent('nextStep');
}

var cufonLoad = function( oEventsManager )
{
	Cufon.replace('.menu h3');
	Cufon.replace('.page .content .menu .menu_item .thumbs li .title');
	Cufon.replace('.menu .direct_access .bloc h4');
	Cufon.replace('.page_layout .left_content .sejours h4');
	Cufon.replace('.more_content .links h5');
	Cufon.replace('.list .top_list .informations span');
	Cufon.replace('.page_content h2');

	oEventsManager.fireEvent('nextStep');
}

var addTextLmit = function( oEventsManager )
{
	var aContainer = $$('.more_content .limited');
	var aSize = new Array();
	
	if (aContainer.length > 0)
	{
		aContainer.each(function(elem, index){
			var oText = elem.getElement('p');
			if (oText)
			{
				var sText = oText.get('html');
				if (sText.length > 200)
				{
					// getting elements
					var oMoreText = elem.getElement('.more_text');
					var oLink = elem.getElement('.more_link');
					oLink.setStyle('display', 'block');
					
					// text cut
					var sTextLimited = sText.substr(0, 200);
					var iIndex = sTextLimited.lastIndexOf(" ");
					oText.set('html', sTextLimited.substr(0, iIndex) + ' ...');
					oMoreText.set('html', sText.substr(iIndex, sText.length));
					
					// getting size
					var oSize = oMoreText.getSize();
					aSize[index] = oSize.y;
					oMoreText.setStyle('height', 0);
					
					oLink.addEvent('click', function(){
						var oFx = new Fx.Tween(oMoreText);
						
						if ( oMoreText.getSize().y == 0)
						{
							oFx.start('height', aSize[index]+'px');
							this.addClass('up');
						}
						else
						{
							oFx.start('height', 0);
							this.removeClass('up');
						}
					});
				}
			}
		});
	}
	
	oEventsManager.fireEvent('nextStep');
}

var cycloAllCarto = function( oEventsManager )
{
  if ($('infos_hidden'))
	{
    if ($('infos_hidden').fiche_cyclo && $('infos_hidden').fiche_cyclo.value == 1)
  	{
      var aElements = $$('div.selection ul li');
      var oElement = aElements[0];
    	oElement.getElement('a').fireEvent('click');
  	}
	}
	
	oEventsManager.fireEvent('nextStep');
}

if ( oEventsManager != null && oEventsManager != undefined )
{
	oEventsManager.registerEvent('addHebEvent', addHebEvent);
	oEventsManager.registerEvent('addHebResEvent', addHebResEvent);
	oEventsManager.registerEvent('addDomaineEvent', addDomaineEvent);
	oEventsManager.registerEvent('addLocationEvent', addLocationEvent);
	oEventsManager.registerEvent('addMapEvent', addMapEvent);
	oEventsManager.registerEvent('addSliderItemBtnEvent', addSliderItemBtnEvent);
	oEventsManager.registerEvent('addItemCartoListEvent', addItemCartoListEvent);
	oEventsManager.registerEvent('cartoFollowScroll', cartoFollowScroll);
	oEventsManager.registerEvent('cufonLoad', cufonLoad);
	oEventsManager.registerEvent('addCartoKMZ', addCartoKMZ);
	oEventsManager.registerEvent('addCartoMarker', addCartoMarker);
	oEventsManager.registerEvent('addEventCartoDisplayPrestaLiee', addEventCartoDisplayPrestaLiee);
	oEventsManager.registerEvent('displayMarkerOnMap', displayMarkerOnMap);
	oEventsManager.registerEvent('addTextLmit', addTextLmit);
	oEventsManager.registerEvent('cycloAllCarto', cycloAllCarto);
}

function ShowLightbox()
{
	var aImages = $$('.pl_photo .smoothbox');
	var aSmooth = new Array();
	if (aImages.length > 0)
	{
		aImages.each(function(elem, index){
			aSmooth[index] = new Array(elem.get('href'), (elem.get('title') == null)?'':elem.get('title'));
		});
	}
	
	if (aSmooth.length > 0)
	{
		Slimbox.open(aSmooth, 0);
	}
}
