Event.observe(window,'load',function(){
	if($('localisation')){
		localisation();
	}
	if($('guide_plan')){
		Event.observe('a_carte_plan','click',function(){
			document.getElementById('a_carte').click();
		});
	}
	if($('guide')){
		Event.observe('a_carte','click',function(){
			if($('cont_carte').style.display == 'none'){
				affiche_carte_gmaps();
			}else{
				masquer_carte_gmaps();
			}
		});
	}
	if($('reserver')){
		$$('input.calendrier').each( function(e){
			Event.observe(e,'focus',function(){
				date_split1 = $F('date_arrivee').split('/');
				var date_test1 = date_split1[2]+date_split1[1]+date_split1[0]
				date_split2 = $F('date_depart').split('/');
				var date_test2 = date_split2[2]+date_split2[1]+date_split2[0]
			
				if((parseInt(date_test2)-parseInt(date_test1))>0){
					$('cont_nb_nuit').show();
					var date1 = $F('date_arrivee');
					var date2 = $F('date_depart');
					var debut = temps(date1.split("/"));
					var fin = temps(date2.split("/"));
					var nb = (fin - debut) / (1000 * 60 * 60 * 24); // + " jours";
					if(Math.round(nb)<2)
					{
						$('nb_nuit').update('Soit '+Math.round(nb)+' nuit  &nbsp;('+Math.round(nb)+' night)');
					}
					else
					{
						$('nb_nuit').update('Soit '+Math.round(nb)+' nuits &nbsp;('+Math.round(nb)+' nights)');
					}
				}else if(date_test2 == date_test1){
					$('cont_nb_nuit').hide();
				}else{
					$('nb_nuit').update('Erreur');
					//$('cont_nb_nuit').hide();
				}
			});									  
		});
	}
	if($('liste_offre')){
		$$('.a_detail').each( function(e){
			Event.observe(e,'click',function(){
				if(e.up(1).next().style.display == 'none'){
					new Effect.BlindDown(e.up(1).next(),{duration:0.5, afterFinish:
						function(){
							e.update('Fermer');	
						}
					});
				}else{
					new Effect.BlindUp(e.up(1).next(),{duration:0.5, afterFinish:
						function(){
							e.update('( En savoir plus )');	
						}
					});
				}
			});									  
		});
	}
});
Event.observe(window,'unload',function(){
	if($('localisation') || $('guide')){
		GUnload();
	}
});




var localisation = function() {
	var map = null;
	map = new GMap2($("map"));
	var customUI = map.getDefaultUI();
	customUI.controls.scalecontrol = false;
	map.setUI(customUI);
	var baseIcon = new GIcon(G_DEFAULT_ICON);
	baseIcon.shadow = "images/commun/ombre_marker.png";
	baseIcon.shadowSize = new GSize(27, 27);
	baseIcon.iconSize = new GSize(16, 27);
	baseIcon.iconAnchor = new GPoint(8, 27);
	baseIcon.infoWindowAnchor = new GPoint(0, 0);
	var IconType = new GIcon(baseIcon);
	IconType.image = "images/commun/marker.png";
	markerOptions = {icon:IconType,clickable:false};
	var point = new GLatLng(tab_point.latitude,tab_point.longitude);
	var marker = new GMarker(point, markerOptions);
	map.setCenter(point, 17);
	map.addOverlay(marker);
};


/* GUIDE */
/*
map = null;
*/
var createMarker = function(point,i) {
	var baseIcon = new GIcon(G_DEFAULT_ICON);
	baseIcon.shadow = "images/commun/ombre_marker2.png";
	baseIcon.iconSize = new GSize(20, 20);
	baseIcon.shadowSize = new GSize(35, 20);
	baseIcon.iconAnchor = new GPoint(10, 20);
	baseIcon.infoWindowAnchor = new GPoint(12, 0);
	// Create a lettered icon for this point using our icon class
	var IconType = new GIcon(baseIcon);
	IconType.image = "images/commun/marker"+tab_point[i].type+".png";
	
	if (tab_point[i].type == "0")
	{
		IconType.image = "images/commun/marker_RH.png";
		IconType.iconSize = new GSize(16, 27);
		IconType.shadow = "images/commun/ombre_marker.png";
		IconType.shadowSize = new GSize(27, 27);
	}
	
	markerOptions = {
		title : tab_point[i].nom,
		icon:IconType
	};
	var marker = new GMarker(point, markerOptions);

	return marker;
};

var etiquette = function(point,i){
	var infoOptions = {
		pixelOffset : new GSize(-5,-8)
	}
	var myHtml= '<h4><a href="'+tab_point[i].id+'" class="nom" target="_blank">'+tab_point[i].nom+'</a></h4>';
	myHtml+= '<div class="visuel"><img src="'+tab_point[i].visuel+'" ></div>';
	myHtml+= '<div class="info">';
	myHtml+= '	<p>'+tab_point[i].data+'</p>';
	myHtml+= '	<a href="'+tab_point[i].id+'" class="lien" target="_blank">(+ d\'infos)</a>';
	myHtml+= '</div>';
	map.openInfoWindowHtml(point,'<div style="width:300px; height:130px;" class="etiquette">'+myHtml+'</div>', infoOptions);
}

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

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() {
	map = new GMap2($("map_guide"));
	map.setUIToDefault();

	// INIT
	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);
	creerPoint();
};

var resetTheme = function(){
	map.clearOverlays();
	$$('.carte .filtres li a').each( function(bt){
		bt.removeClassName('on');	
	});
};

var toutTheme = function(){
	$$('.carte .filtres li a').each( function(bt){
		bt.addClassName('on');	
	});
};

var toutAfficher = function(){
	Event.observe($$('.tout_afficher input')[0], 'click', function(){
		if($$('.tout_afficher input')[0].checked == true){
			creerPoint();
			toutTheme();
			tab_theme = tab_theme_sav;
		}else{
			resetTheme();
			tab_theme=new Array();
		}
	});
};

/* FILTRES */
var creerTheme = function(){
	tab_original = tab_point;
	tab_theme = new Array();
	tab_theme_sav = new Array();
	tab_original.each( function(e,i){
		if(e.type.length>1){
			e.type.split("|").each( function(s){
				tab_theme.push(s);			   
			});
		}else{
			tab_theme.push(e.type.substr(0,1));					
		}
	});
	tab_theme_sav = tab_theme.uniq();
	tab_theme = tab_theme.uniq().toJSON();

	$$('.carte .filtres li').each( function(bt, i){
		if(!tab_theme.include(bt.className.substr(5))){
			bt.update('<span>'+bt.down(0).innerHTML+'</span>');
		}
	});
	tab_theme = tab_theme_sav;
	choixTheme();
};

var choixTheme = function(){
	$$('.carte .filtres li a').each( function(bt){
		Event.observe(bt, 'click', function(){
			map.clearOverlays();
			if(bt.className == 'on'){
				bt.removeClassName('on');
				tab_theme = tab_theme.without(bt.up(0).className.substr(5));
			}else{
				bt.addClassName('on');	
				tab_theme.push(bt.up(0).className.substr(5));
			}
			tab_original.each( function(e,i){
				tab_theme.each( function(e2,j){
					if(e.type.substr(0,1) == e2){
						var point = new GLatLng(e.latitude,e.longitude);
						map.addOverlay(createMarker(point, i));	
					}
				});
			});	
		});
	});
};
function temps(date){
	var d = new Date(date[2], date[1] - 1, date[0]);
	return d.getTime();
}




function affiche_carte_gmaps()
{
	//var carte = $('a_carte');
	var map = document.getElementById('map');
	var cont_carte = document.getElementsByName('cont_carte')[0];
	var a_carte = document.getElementsByName('a_carte')[0];
	
	cont_carte.blur();
	new Effect.BlindDown('cont_carte',{
		duration:0.3,
		afterFinish:function(){
			if(document.getElementById('a_carte').innerHTML == 'Afficher la carte')a_carte.update('Masquer la carte');
			else a_carte.update('Hide Map');
			a_carte.addClassName('on');
			if(!map){
				initialize();
				toutAfficher();
				creerTheme();
			}
		}
	});
}

function masquer_carte_gmaps()
{
	$('a_carte').blur();
	new Effect.BlindUp('cont_carte',{duration:0.3,
		afterFinish:function(){
			if(document.getElementById('a_carte').innerHTML == 'Masquer la carte')a_carte.update('Afficher la carte');
			else a_carte.update('Display Map');
			$('a_carte').removeClassName('on');
		}
	});
}
/* Ajuoté Nicolas Danvin 6 décembre 2011 */
$(function() {
	function launch() {
		 $('#sign_up').lightbox_me({centered: true, onLoad: function() { $('#sign_up').find('input:first').focus()}});
	}
	
	$('#try-1').click(function(e) {
		$("#sign_up").lightbox_me({centered: true, onLoad: function() {
			$("#sign_up").find("input:first").focus();
		}});
		
		e.preventDefault();
	});
	
	
	$('table tr:nth-child(even)').addClass('stripe');
});
