Event.observe(window, 'load',function(){
	if($('promenade_accueil') || ( $('promenade_fiche') && $('map_promenade'))){
		baseIcon = new GIcon(G_DEFAULT_ICON);
		baseIcon.shadow = "images/commun/gmap/ombre.png";
		baseIcon.iconSize = new GSize(24,29);
		baseIcon.shadowSize = new GSize(39,29);
		baseIcon.iconAnchor = new GPoint(12,29);
	}
	if($('promenade_accueil')){
		map = new GMap2($("map"));
		map.addControl(new GMapTypeControl());
		map.addControl(new GSmallMapControl());
		initialize(0);
		if($('page_s')){
			Event.observe('page_s', 'click', function(){
				initialize(parseInt($F('page'))+1);
			});	
		}
		if($('page_p')){
			Event.observe('page_p', 'click', function(){
				initialize(parseInt($F('page'))-1);
			});
		}
		Event.observe('tri', 'change', function(){
			initialize(0);
		});
		if($('sb_tri')){
			multiToggle('sb_tri','onglets_tri','contenu_tri');
		}
	}
	if($('promenade_fiche') && $('map_promenade')){
		map = new GMap2($("map_promenade"));
		map.addControl(new GMapTypeControl());
		map.addControl(new GSmallMapControl());
		initialize2();
		$$('table td div').each(function(e){
			Event.observe(e,'click',function(){
				window.location.href = e.down().href;			 
			});
		});
	}
	
	if($('map_ajout_promenade')){
		initialize();
		makeItCount('description_promenade',500);
		Event.observe('bt_recherche_promenade','click',function(){
			if($F('adresse')!='' && $F('pays')!=''){
				$('adresse').removeClassName('inputError');
				$('pays').removeClassName('inputError');
				adressePromenade($F('adresse'),$F('pays'));
			}else{
				if($F('adresse')==''){
					$('adresse').addClassName('inputError');
				}else{
					$('adresse').removeClassName('inputError');
				}
				if($F('pays')==''){
					$('pays').addClassName('inputError');
				}else{
					$('pays').removeClassName('inputError');
				}
			}
		});
		
		$$('table td input').each(function(e,i){
			Event.observe(e,'click',function(){
				if($F('verifHoraire')==''){
					$('verifHoraire').value=0;
				}
				if(e.checked == true){
					$('verifHoraire').value = parseInt($('verifHoraire').value) + 1;
				}else{
					$('verifHoraire').value = parseInt($('verifHoraire').value) - 1
				}
			});
		});
	}
});
Event.observe(window,'unload',function(){
	if($('promenade_accueil') || ( $('promenade_fiche') && $('map_promenade'))){
		GUnload();
	}
});

var map = null;
tab_marker = new Array();
url_page = '?RID=144&wid=';


function tri_nombres(a,b){
	return a-b;
}

var calculCoord = function(calcul){
	var tab_lat = new Array();
	var tab_lng = new Array();
	tab_point.each( function(e){
		tab_lat.push(e.latitude);
		tab_lng.push(e.longitude);
	});
	var lat_min = parseFloat(tab_lat.sort(tri_nombres).first());
	var lat_max = parseFloat(tab_lat.sort(tri_nombres).last());
	var lng_min = parseFloat(tab_lng.sort(tri_nombres).first());
	var lng_max = parseFloat(tab_lng.sort(tri_nombres).last());
	if(calcul=='lat_moy'){return (lat_min+lat_max)/2;}
	if(calcul=='lng_moy'){return (lng_min+lng_max)/2;}
	if(calcul=='lat_min'){return lat_min;}
	if(calcul=='lat_max'){return lat_max;}
	if(calcul=='lng_min'){return lng_min;}
	if(calcul=='lng_max'){return lng_max;}
}

var initialize = function(page) {
			if(tab_point.size()>0){
		tab_point.each( function(e,i){
			if( e.address != undefined ) {
				var adresse = e.address;
				if( i > 0 ) {
					var geocoder = new google.maps.ClientGeocoder();
					geocoder.setBaseCountryCode(e.lnd);
					geocoder.getLatLng(adresse, function (coord) {
						e.latitude = coord.lat();
						tab_point[i].latitude = e.latitude;
						e.longitude = coord.lng();
						tab_point[i].longitude = e.longitude ;
						});
					}
				}
				});
		var sw = new GLatLng(calculCoord('lat_min'), calculCoord('lng_min'));
		var ne = new GLatLng(calculCoord('lat_max'), calculCoord('lng_max'));
		var bounds = new GLatLngBounds(sw, ne);
		level  = map.getBoundsZoomLevel(bounds);

		if($F('premiere_recherche')=='true'){
			if($F('pays_origine')!=''){ // Centrage sur le pays d'origine
				geocoder = new GClientGeocoder();
				geocoder.getLatLng(
					$F('pays_origine'),
					function(point){
						map.setCenter(point, 5);
				});
			}else{
				map.setCenter(new GLatLng(46.5, 2.5), 5);// Centrage sur la france
			}
		}else{
			map.setCenter(new GLatLng(calculCoord('lat_moy'), calculCoord('lng_moy')), level);
		}
		if( tab_point.size()){
		creerPoint();
		creerListe();
		}
		if ($('page_p') && $('page_s') && $('page')) {
			if(parseInt(page)==0){
				$('page_p').hide();	
			}else{
				$('page_p').show();	
			}
			if(parseInt(page)==$F('nb_page')){
				$('page_s').hide();
				$('page_p').addClassName('unique');
			}else{
				$('page_s').show();	
				$('page_p').removeClassName('unique');
			}
		}
		if($('page')) $('page').value = page;
		$('loading_liste').remove();
	}else{
		$('f_liste').update('');
		geocoder = new GClientGeocoder();
		geocoder.getLatLng(
			$F('promenade_ville')+", "+$('pays').options[$('pays').selectedIndex].text,
			function(point){
				map.setCenter(point, 12);
		});
	}
};

/* CREATION DES POINTS */
var creerPoint = function(){
	tab_point.each( function(e,i){
		var point = new GLatLng(e.latitude,e.longitude);
		map.addOverlay(createMarker(point, i));
	});
};

var createMarker = function(point,i) {
	IconType = new GIcon(baseIcon);
	IconType.image = "images/commun/gmap/marker"+(i+1)+".png";
	markerOptions = {icon:IconType};
	var marker = new GMarker(point, markerOptions);
	GEvent.addListener(marker, "click", function() {
		etiquette(point,i);
		deplaceListe(i);
	});
	return marker;
};

var etiquette = function(point,i){
	var infoOptions = {
		pixelOffset : new GSize(-5,-8),
		onOpenFn : 	openInfo,
		onCloseFn : closeInfo
	}
	var myHtml = '<div class="visuel"><img src="images/commun/visu_promenade.gif" alt="" /></div>';
	myHtml+= '<div class="info">';
	myHtml+= '	<a href="'+url_page+tab_point[i].wid+'" class="nom">'+tab_point[i].nom+'</a><br/>';
	myHtml+= '	<strong class="ville">'+tab_point[i].ville+'</strong><br/>';
	if(tab_point[i].inscription > 0){
	myHtml+= '	<p>'+tab_point[i].inscription+' inscrits</p>';
	}
	myHtml+= '<span class="note'+tab_point[i].note+'"></span>';
	myHtml+= '</div>';
	myHtml+= '<p class="description">'+tab_point[i].description+'</p>';
	myHtml+= '<div class="lien"><a href="'+url_page+tab_point[i].wid+'">Voir sa fiche</a></div>';
	map.openInfoWindowHtml(point,'<div style="width:300px; height:205px;" class="etiquette">'+myHtml+'</div>', infoOptions);
}

var openInfo = function(){};
var closeInfo = function(){
	$$('.item').each(function(e){
		e.removeClassName('in');
	});
};

var creerListe = function(){
	tab_point.each( function(e,i){
		var tpl='<div class="item" id="item#{id}">';
		tpl+='		<div class="f_item">';
		tpl+='			<div class="num">#{num}</div>';
		tpl+='			<div class="texte">';
		tpl+='				<h4><a href="javascript:void(0);">#{nom}</a></h4>';
		tpl+='				<strong>#{ville}</strong>';
		if(tab_point[i].inscription > 0){
		tpl+='				<p>#{inscription} inscrits</p>';
		}
		tpl+='				<span class="note#{note}"></span>';
		tpl+='				<div class="auteur">';		
		tpl+='					<div class="avatar"><a href="#{url_auteur}"><img src="#{avatar}" alt="#{auteur}" /></a></div>';		
		tpl+='					<p>Par <a href="#{url_auteur}">#{auteur}</a></p>';		
		tpl+='				</div>';		
		tpl+='			</div>';
		tpl+='			<div class="picto"><span class="num#{num}"></span></div>';
		tpl+='		</div>';
		tpl+='</div>';

		var valeur_template = {id:tab_point[i].id, num:(i+1), nom:tab_point[i].nom, avatar:tab_point[i].avatar, auteur:tab_point[i].auteur, url_auteur:tab_point[i].url_auteur, ville:tab_point[i].ville, inscription :tab_point[i].inscription, note:tab_point[i].note, description:tab_point[i].description};
		var template = new Template(tpl);
		var nouveau = template.evaluate(valeur_template);	
		$('f_liste').insert({bottom:nouveau});
	});
	
	$$('.item').each(function(e,i){
		// roll over sur l'item
		Event.observe(e, 'mouseover', function(){
			e.addClassName('on');							   
		});
		// roll over sur l'item
		Event.observe(e, 'mouseout', function(){
			e.removeClassName('on');							   
		});
		// clic sur l'item
		var div = $$('#'+e.id+' div.f_item')[0];
		Event.observe(div, 'click', function(){
			var point = new GLatLng(tab_point[i].latitude,tab_point[i].longitude);											 
			etiquette(point,i);
			deplaceListe(i);
		});
	});
};

var deplaceListe = function(i){
	var id = tab_point[i].id;
	var hauteur = 0;
	$('item'+id).previousSiblings().each( function(e){
		hauteur = hauteur + e.offsetHeight;
	});
	$('f_liste').scrollTop = hauteur;
	$$('.item').each(function(e){
		e.removeClassName('in');
	});
	$('item'+id).addClassName('in');
};


// FICHE PROMENADE
var initialize2 = function() {
	var IconType = new GIcon(baseIcon);
	tab_point.each( function(e,i){
		if( i > 0 ) {
			if( e.address != undefined ) {
				var adresse = e.address;
				var geocoder = new google.maps.ClientGeocoder();
				geocoder.setBaseCountryCode(e.lnd);
				geocoder.getLatLng(adresse, function (coord) {
					e.latitude = coord.lat();
					tab_point[i].latitude = e.latitude;
					e.longitude = coord.lng();
					tab_point[i].longitude = e.longitude;
					IconType.image = "images/commun/gmap/marker"+e.num+tab_point[i].type+".png";
					markerOptions = {icon:IconType,clickable:false};
					point = new GLatLng(tab_point[i].latitude,tab_point[i].longitude);
					var marker = new GMarker(point, markerOptions);
					map.addOverlay(marker);
				});
			}
		}
		else
		{
			IconType.image = "images/commun/gmap/animal6.png";
		markerOptions = {icon:IconType,clickable:false};
		point = new GLatLng(tab_point[i].latitude,tab_point[i].longitude);
		var marker = new GMarker(point, markerOptions);
		map.addOverlay(marker);
		}
	});
	var sw = new GLatLng(calculCoord('lat_min'), calculCoord('lng_min'));
	var ne = new GLatLng(calculCoord('lat_max'), calculCoord('lng_max'));
	var bounds = new GLatLngBounds(sw, ne);
	level  = map.getBoundsZoomLevel(bounds);
	//map.setCenter(new GLatLng(calculCoord('lat_moy'), calculCoord('lng_moy')), level);
	map.setCenter(new GLatLng(calculCoord('lat_moy'), calculCoord('lng_moy')), level-3);
};

