$(document).ready(function() {
	
	/* -------------------------------------------------------------- 
	   RESA DESCRIPTIF
	-------------------------------------------------------------- */
	var id = $('#resa-descriptif');
	
	$('.resa-descriptif-menu a', id).click(function() {
		var aList = $(this).parents('.resa-descriptif-menu');
		var index = $('a', aList).index(this);
		$('.resa-descriptif-contenu>div').hide();
		$('#'+$(this).attr('name')).show();
		$('a:not(:eq('+index+'))  ._on', aList).hide();
		$('a:not(:eq('+index+'))  ._off', aList).show();
		$(this).find('._off').hide();
		$(this).find('._on').show();
		return false;
	});
	
	/* -------------------------------------------------------------- 
	   RESA PAIEMENT
	-------------------------------------------------------------- */
	var id = $('#form-change-arrhes');
	$('input[name="arrhes"]', id).change(function() {
		$('#form-paiement-moyen').html('<br /><span class="strong">Chargement...</span><br /><br />');
		var f = $(this).parents('form');
		f.find('input:radio').unbind().bind('click change', function(e) {e.stopPropagation(); return false;});
		f.submit();
	});
	$('.resa-recap-expand').toggle(
		function() {$(this).removeClass('resa-recap-expand-down').addClass('resa-recap-expand-up'); $('.resa-recap-expand-target').show('fast');},
		function() {$(this).removeClass('resa-recap-expand-up').addClass('resa-recap-expand-down'); $('.resa-recap-expand-target').hide('fast');}
	);
	
	/* -------------------------------------------------------------- 
	   TOOL TIPS
	-------------------------------------------------------------- */
	$('.resa-tooltip').cluetip({ local:true, showTitle:false, arrows:true });
	$('.option-tarif').livequery(function() {
		$(this).cluetip({ local:true, showTitle:false, arrows:true });
	});
	
	/* -------------------------------------------------------------- 
	   RESA EXPRESS
	-------------------------------------------------------------- */
	var id = $('.resa-express');
	
	loadingExpress = function(display) {
		if (display) {
			var devisWidth = $(".resa-express").width();
			var devisHeight = $(".resa-express").height();
			$('#resa-express-loading').css({opacity: 0.75, width: devisWidth, height: devisHeight}).show();
		} else {
			$('#resa-express-loading').hide();
		}
	};
	
	if ($('#index').length == 1) {
		$('<div class="resa-express-titre-anim"></div>').appendTo('#resa-express').delay(600).fadeOut(1000, function () {$(this).remove();});
	}
	
	if (id.length == 1) {
		datepickerInit(id);
		
		id.find('#_resaExpressDestination, #_resaExpressSejour, #_resaExpressPrestation').livequery('change', function() {
			var form = $(this).parents('form');
			callResaExpress(form.serialize());
			return false;
		});
		
		id.find('.resa-express-new').livequery('click', function() {
			callResaExpress();
			return false;
		});
	}
    
    var id = $('#resa');
    if (id.length == 1) {
    
        loading = function(display) {
        	var aSubmit = $('a.submit').parent();
			if (display) {
				aSubmit.css('visibility', 'hidden');
				var resaWidth = $("#resa .contenu-ajax").width();
				var resaHeight = $("#resa .contenu-ajax").height();
				$('#resa-loading').css({opacity: 0.75, width: resaWidth, height: resaHeight}).show();
			} else {
				$('#resa-loading').hide();
				aSubmit.css('visibility', 'visible');
			}
		};
		
		//Permet de déselectionner un input de type radio
		$('input.option-groupe:radio:checked', id).livequery('click', function() {
			$(this).removeAttr('checked');
			if ($(this).is('[liveresatarif]')) liveResaTarif();
		});
    
        parseResponse = function(data) {
			var variables = {erreur:1, html:'Erreur inconnue'};
			
			if (data.substring(0, 14) === 'erreur=0&html=') {
				variables.erreur = 0;
				variables.html = data.substring(14);
			} else if (data.substring(0, 14) === 'erreur=1&html=') {
				variables.erreur = 1;
				variables.html = data.substring(14);
			}
			
			return variables;
		};
		
		createErrorMessage = function(msg) {
			var html;
			html = '<div class="alert">';
				html += '<div class="erreur">';
					html += '<div class="ico png"></div>';
					html += '<div class="wrap">'+msg+'</div>';
				html += '</div>';
			html += '</div>';
			
			return html;
		};
        
		id.find('.resa-resultats-sort-destinations, .resa-resultats-sort-sejours').change(function() {
			var items = $('.resa-resultat');
			var searchValues = {destination:$('.resa-resultats-sort-destinations').val(), sejour:$('.resa-resultats-sort-sejours').val()};
			var searchCount = 0;
			
			items.each(function(k, v) {
				var itemShow = {destination:false, sejour:false};
				var itemValues = {destination:$('input[name="destination"]', v).val(), sejour:$('input[name="sejour"]', v).val()};
				
				if (searchValues.destination == 0 || itemValues.destination == searchValues.destination) itemShow.destination = true;
				if (searchValues.sejour == 0 || itemValues.sejour == searchValues.sejour) itemShow.sejour = true;
				
				if (itemShow.destination && itemShow.sejour) {searchCount++; $(v).show();} else $(v).hide();
			});
			$('.resa-resultats-total').html(searchCount+' '+(searchCount > 1 ? 'séjours trouvés' : 'séjour trouvé'));
			return false;
		});

		id.find('.resa-resultats-sort-prix').toggle(
			function() {
				$(this).attr('id', 'prix-asc');
				var items = $('.resa-resultat');
				items.sort(function(a, b) {
					return parseFloat($(a).find('input[name="tarif"]').val()) > parseFloat($(b).find('input[name="tarif"]').val()) ? 1 : -1;
				}).remove().appendTo(".resa-resultats-wrap");
				return false;
			},
			function() {
				$(this).attr('id', 'prix-desc');
				var items = $('.resa-resultat');
				items.sort(function(a, b) {
					return parseFloat($(a).find('input[name="tarif"]').val()) < parseFloat($(b).find('input[name="tarif"]').val()) ? 1 : -1;
				}).remove().appendTo(".resa-resultats-wrap");
				return false;
			}
		);
		id.find('select#resa_curiste_nb').livequery('change', function() {
			var form = $(this).parents('form');
			var nbCuriste = $(this).val();
			var nbMax = $('input#resa_max_pers').val();
			var nbNonCuriste = nbMax - nbCuriste;
			var selectNonCuriste = $('select#resa_non_curiste_nb', form);
			var _prev = $('option:selected', selectNonCuriste).val();
			$('option', selectNonCuriste).remove();
			for (var i = 0; i <= 2; i++) {
				if (i <= nbNonCuriste) selectNonCuriste.append('<option value="'+i+'"'+(_prev != undefined && _prev == i ? ' selected="selected"' : '')+'>'+i+' adulte'+(i > 1 ? 's' : '')+'</option>');
			}
		}).change();
        
        function getOptionsThalasso( obj ) {
        	var val = obj.val();
        	
        	//IE6 !! DEBUT
        	var selectedOption = $('option[value="'+val+'"]', obj);
			$('select.thalasso-liste').not(obj).each(function() {
				$('option', this).removeAttr('disabled').css('color', '#536267');
        		$('option[value="'+val+'"][qte="1"]', this).attr('disabled', 'disabled').css('color', '#c5c7c8');
        	});
			
			if (selectedOption.attr('disabled')) {
				getOptionsThalasso(obj.val(-1).blur());
				return;
			}
			//IE6 !! FIN
			
            loading(true);
			//On doit afficher les options de ce type de prestation !
			
			$('#focus-ie').focus();
			var curisteDomId = obj.parents('.curiste').attr('id');
			var curisteDomIdSplit = curisteDomId.split('-');
			curisteVal = curisteDomIdSplit[1];
			
			$.ajax({
				type: "POST",
				url: "/resa-verifications.php",
				data: {thalassoId: val, curisteNum:curisteVal, action: 'getOptionsThalasso'},
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						$('#etape-3 #'+curisteDomId+' .thalasso-options-ajax').html(variables.html);
						$('#resa #erreur-ajax').empty();
					} else {
						$('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
        }
        
        id.find('select.thalasso-liste').livequery('change', function() {
            getOptionsThalasso( $(this) );
		});
        /*
        id.find('#etape-2 a.bt-valider-2').livequery('click', function() {
        	if($('#resa #erreur-ajax').html() !== '') return false;
			return setEtape2(this);
		});
        */
        id.find('#etape-1 a.bt-valider-1').livequery('click', function() {
        	//if($('#resa #erreur-ajax').html() !== '') return false;
			/*$.ajax({
				type: "POST",
				url: "/resa-verifications.php",
				data: {dateDepart:dateDepartVal, duree:dureeVal, curisteNb:curisteNbVal, nonCuristeNb:nonCuristeNbVal, etape: 1},
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						var dom = $('#etape-1').html(variables.html);
						$('#resa #erreur-ajax').empty();
						if (adulteNbVal == 1) setEtape2($('#etape-2 a.bt-valider-2', dom));
					} else {
						$('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});*/
			return setEtape2(this);;
		});
        
        function setEtape2( obj ) {
        	loading(true);
			var form = $(obj).parents('form');
			var dateDepartVal = $('input#resa_date_depart', form).val();
			var dureeVal = $('#resa_duree', form).val();
			var curisteNbVal = $('select#resa_curiste_nb', form).val();
			var nonCuristeNbVal = $('select#resa_non_curiste_nb', form).val();
            $.ajax({
				type: 'POST',
				url: '/resa-verifications.php',
				data: {dateDepart:dateDepartVal, duree:dureeVal, curisteNb:curisteNbVal, nonCuristeNb:nonCuristeNbVal, etape: 2},
				success: function(response) {
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						loading(true);
						$('#etape-1').html(variables.html);
						$('#resa #erreur-ajax').empty();
						
                        $.ajax({
                            type: "POST",
                            url: "/resa-verifications.php",
                            data: {thalassoId: $('#champ-thalasso-curiste-1-prestation').attr('class'), curisteNum:1, action: 'getOptionsThalasso'},
                            success: function(response) {
                                loading(false);
                                var variables = parseResponse(response);
                                if (variables.erreur == 0) {
                                    $('#etape-3 #curiste-1 .thalasso-options-ajax').html(variables.html);
                                    $('#resa #erreur-ajax').empty();
                                    if (curisteNbVal > 1) {
                                    	loading(true);
                                        $.ajax({
                                            type: "POST",
                                            url: "/resa-verifications.php",
                                            data: {thalassoId: $('#champ-thalasso-curiste-1-prestation').attr('class'), action: 'getOtherCuristes'},
                                            success: function(response) {
                                                loading(false);
                                                var variables = parseResponse(response);
                                                if (variables.erreur == 0) {
                                                    $('#etape-3 #other-curistes-ajax').html(variables.html);
                                                    $('#resa #erreur-ajax').empty();
                                                } else {
                                                    $('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
                                                    self.location.href = "#erreur-ancre";
                                                }
                                            }
                                        });
                                    }
                                } else {
                                    $('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
                                    self.location.href = "#erreur-ancre";
                                }
                            }
                        });
					} else {
						loading(false);
						$('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
            
            return false;
        }
    
        function getOptionsHebergement( obj ) {
			var val = obj.val();
			loading(true);
			$.ajax({
				type: "POST",
				url: "/resa-verifications.php",
				data: {hebergementId:val, action: 'getOptionsHebergement'},
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						var etape = $('#etape-4 .hebergement-options-ajax').html(variables.html);
						//initPension(etape);
						$('#resa #erreur-ajax').empty();
					} else {
						$('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
        };
        
        id.find('select#hebergement-liste').livequery('change', function() {
			return getOptionsHebergement( $(this) );
		});
        /*
        id.find('#etape-3 a.bt-prec-2').livequery('click', function() {
			loading(true);
			$.ajax({
				type: "POST",
				url: "/resa-verifications.php",
				data: {etape: 1},
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						$('#etape-1').html(variables.html);
						$('#resa #erreur-ajax').empty();
					} else {
						$('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
		});
    	*/
        id.find('#etape-3 a.bt-valider-3').livequery('click', function() {
        	if($('#resa #erreur-ajax').html() !== '') return false;
        	
            var form = $(this).parents('form');
            
            var selected = true;
            form.find( '.thalasso-liste' ).each( function( k, v ) {
                if ( $( v ).val() == -1 ) {
                    selected = false;
                }
            } );
            
            if ( selected ) {
                loading(true);
                var formValues = form.serialize()+'&etape=3';
                $.ajax({
                    type: "POST",
                    url: "/resa-verifications.php",
                    data: formValues,
                    success: function(response) {
                        loading(false);
                        var variables = parseResponse(response);
                        if (variables.erreur == 0) {
                            $('#etape-3').html(variables.html);
                            $('#resa #erreur-ajax').empty();
                        } else {
                            $('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
                            self.location.href = "#erreur-ancre";
                        }
                    }
                });
            }
			return false;
        });
        
        id.find('#etape-4 a.bt-prec-3').livequery('click', function() {
            loading(true);
			$.ajax({
				type: "POST",
				url: "/resa-verifications.php",
				data: {etape: 2},
				success: function(response) {
					loading(false);
                    
                    /* on choppe les prestations */                    
                    var aPrestations = new Array();
                    $( '.selection-thalasso-curiste' ).each( function() {
                        var _curisteNum = parseInt( $( this ).find( '.curisteNum' ).html() );
                        var _thalassoId = parseInt( $( this ).find( '.thalassoId' ).html() );
                        
                        aPrestations.push( {thaId:_thalassoId , curisteNum:_curisteNum} );
                    } );
                    
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						$('#etape-1').html(variables.html);
						$('#resa #erreur-ajax').empty();
                        
                        if (parseInt($( '#curisteNb' ).html()) > 1) {
                            $.ajax({
                                type: "POST",
                                url: "/resa-verifications.php",
                                data: {thalassoId: aPrestations[ 0 ].thaId, action: 'getOtherCuristes'},
                                success: function(response) {
                                    loading(false);
                                    var variables = parseResponse(response);
                                    if (variables.erreur == 0) {
                                        var dom = $('#etape-3 #other-curistes-ajax').html(variables.html);
                                        $('#resa #erreur-ajax').empty();
                                        
                                        $.each( aPrestations, function( cle, valeur ) {
                                        	if (valeur.curisteNum > 1) {
	                                            dom.find( 'select[id=resa_curiste_' + valeur.curisteNum + ']' ).find( 'option[value=' + valeur.thaId + ']' ).attr( 'selected', 'selected' );
	                                            getOptionsThalasso( dom.find('select[id=resa_curiste_' + valeur.curisteNum + ']') );
                                        	}
                                        });
                                        
                                        
                                    } else {
                                        $('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
                                        self.location.href = "#erreur-ancre";
                                    }
                                }
                            });
                        }
                        $.ajax({
                            type: "POST",
                            url: "/resa-verifications.php",
                            data: {thalassoId: $('#champ-thalasso-curiste-1-prestation').attr('class'), curisteNum:1, action: 'getOptionsThalasso'},
                            success: function(response) {
                                loading(false);
                                var variables = parseResponse(response);
                                if (variables.erreur == 0) {
                                    $('#etape-3 #curiste-1 .thalasso-options-ajax').html(variables.html);
                                    $('#resa #erreur-ajax').empty();
                                } else {
                                    $('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
                                    self.location.href = "#erreur-ancre";
                                }
                            }
                        });
					} else {
						$('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
        });
        
        id.find('#etape-4 a.bt-valider-4').livequery('click', function() {
        	if($('#resa #erreur-ajax').html() !== '') return false;
            loading(true);
			var formValues = $(this).parents('form').serialize()+'&etape=4';
			$.ajax({
				type: 'POST',
				url: '/resa-verifications.php',
				data: formValues,
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						$('#etape-4').html(variables.html);
						$('#resa #erreur-ajax').empty();
					} else {
						$('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
        });
        
        id.find('#etape-5 a.bt-valider-offres').livequery('click', function() {
        	if($('#resa #erreur-ajax').html() !== '') return false;
            loading(true);
        	var form = $(this).parents('form');
			$.ajax({
				type: 'POST',
				url: '/resa-verifications.php',
				data: form.serialize()+'&etape=offres',
				success: function(response) {
					loading(false);
					var variables = parseResponse(response);
					if (variables.erreur == 0) {
						$('#etape-offres').html(variables.html);
						$('#resa #erreur-ajax').empty();
					} else {
						$('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
        });
        
        id.find('input:checkbox[name="resa[offres][]"]').livequery('change', function(e) {
        	$(this).parents('form').find('input:checkbox[name="resa[offres][]"]').not(this).removeAttr('checked');
        	liveResaTarif();
        	/* !pas de return false! */
        });
        
        id.find('#etape-5 a.bt-prec-4').livequery('click', function() {
            
            loading(true);
			$.ajax({
				type: 'POST',
				url: '/resa-verifications.php',
				data: {etape: 3},
				success: function(response) {
					loading(false);
                    
                    var variables = parseResponse(response);
					if (variables.erreur == 0) {
                        var chambreSelected = parseInt( $('.selection-chambre-curiste:eq(0) .chambreId').html() );
						var dom = $('#etape-3').html(variables.html);
                        dom.find('#hebergement-liste option[value^="'+chambreSelected+'|"]').attr('selected', 'selected');
                        getOptionsHebergement( dom.find('#hebergement-liste') );
                        
						$('#resa #erreur-ajax').empty();
					} else {
						$('#resa #erreur-ajax').html(createErrorMessage(variables.html)).show();
						self.location.href = "#erreur-ancre";
					}
				}
			});
			return false;
        });
    	
    	id.find('*[liveresatarif]').livequery(function() {
    		if ($(this).is(':checkbox, :radio, select')) {
	    		$(this).click(function() {liveResaTarif();});
    		}
    		
    		if ($(this).is('option')) {
	    		$(this).parents('select').change(function() {liveResaTarif();});
    		}
    	});
    	
    	$(document).ajaxStop(function() {
    		if ($('#resa-choix .resa-choix-panier').length == 1) liveResaTarif();
		});
    }
    
	function liveResaTarif() {
		var form = $('form');
		var panier = $('#resa-choix .resa-choix-panier');
		var liveResaTarifs = $('input[liveresatarif]:checkbox:checked, input[liveresatarif]:radio:checked, div[liveresatarif], select[liveresatarif], select option[liveresatarif]:selected', form);
		var somme = {prev:$('#resa-choix-panier-value', panier).val(), next:0};
		liveResaTarifs.each(function() {
			var prix = 0;
			if ($(this).is('select[liveresatarif]')) {
				var t = $(this).val().split('|');
				prix = parseFloat($(this).attr('liveresatarif')) * parseFloat(t[1]);
			} else {
				prix = parseFloat($(this).attr('liveresatarif'));
			}
			somme.next += prix;
		});
		$('#resa-choix-panier-value', panier).val(somme.next);
		if (somme.next != somme.prev) {
			var panierX = $('.resa-choix-panier-montant', panier).offset().left - $(panier).offset().left;
			var prix = somme.next - somme.prev > 0 ? '+ '+(somme.next - somme.prev) : '- '+Math.abs(somme.next - somme.prev);
			
			var animEl = $('.resa-choix-panier-anim', panier).length == 1 ? $('.resa-choix-panier-anim', panier).clearQueue().html(prix+' &euro;') : $('<div class="resa-choix-panier-anim">'+prix+' &euro;</div>').prependTo(panier);
			animEl
			.css({'position':'absolute', 'z-index':1500, 'bottom':-30, 'left':panierX})
			.animate({bottom:0, color:'#7ED6EE'}, 1000).delay(300)
			.animate({opacity: 0.1}, 200, function() {
				$(this).remove();
				$('var', panier).html(somme.next).parent().css({opacity: 0.1, color:'#fff'}).animate({opacity:1, color:'#7ED6EE'}, 1000);
			});
		}
	};
		
	function callResaExpress(donnees) {
		loadingExpress(true);
		$.ajax({
			type: 'get',
			url: '/reservation-express',
			data: donnees,
			success: function(response) {
				loadingExpress(false);
				var dom = $('.resa-express').html(response);
				datepickerInit(dom);
			}
		});
	};
	
	function datepickerInit(dom) {
		dom.find('#_resaExpressDateArrivee').datepicker({onSelect:datepickerSelect, minDate:'+3d', showAnim: 'fadeIn'});
		dom.find('#_clearResaExpressDateArrivee').click(function () {
			dom.find('#_resaExpressDateArrivee').datepicker('setDate', null);
			datepickerSelect('', dom.find('#_resaExpressDateArrivee').data("datepicker"));
			return false;
		});
	};
	
	function datepickerSelect(dateText, inst) {
		var form = $('#'+inst.id).parents('form');
		callResaExpress(form.serialize());
		return false;
	};
	
	/* -------------------------------------------------------------- 
	   RESA CALENDRIER
	-------------------------------------------------------------- */
	$('.resa-calendrier .resa-calendrier-prec-mois, .resa-calendrier .resa-calendrier-suiv-mois').click(function() {
		var etat = $(this).hasClass('resa-calendrier-prec-mois') ? 'prec' : 'suiv';
		var resaCalendrier = $(this).parents('.resa-calendrier');
		var resaCalendrierWrap = resaCalendrier.parents('.resa-calendrier-wrap');
		var resaCalendriers = $('.resa-calendrier', resaCalendrierWrap);
		var index = resaCalendriers.index(resaCalendrier);
		if (etat == 'prec' && index>0) {resaCalendrier.hide().prev().show(); return false;}
		if (etat == 'suiv' && index+1<resaCalendriers.length) {resaCalendrier.hide().next().show(); return false;}
		return false;
	});
	resaCalendrierSelected = function(d) {
		$('.resa-calendrier td').removeClass('resa-calendrier-date-selected');
		$('.resa-calendrier td#'+d).addClass('resa-calendrier-date-selected');
	};
	resaCalendrierSelected($('.resa-calendrier-value').val());
	
	var tableToShow = $('td#'+$('.resa-calendrier-value').val()).parents('.resa-calendrier');
	if (tableToShow.length != 1) $('.resa-calendrier:gt(0)').hide();
	else { $('.resa-calendrier').hide(); tableToShow.show(); }
	$('.resa-calendrier:eq(0) .resa-calendrier-prec-mois').hide();
	$('.resa-calendrier:last .resa-calendrier-suiv-mois').hide();
	$('.resa-calendrier-show').click(function() {$('.resa-calendrier-wrap').show(); $('#resa input, #resa select').css('visibility', 'hidden'); return false;});
	$('.resa-calendrier-wrap').click(function(event) {event.stopPropagation();});
	$('body').click(function() {$('.resa-calendrier-wrap').hide(); $('#resa input, #resa select').css('visibility', 'visible');});
	$('.resa-calendrier-date-prix').hover(
		function() {$(this).addClass('resa-calendrier-date-prix-hover');},
		function() {$(this).removeClass('resa-calendrier-date-prix-hover');}
	);
	

	$('select#resa_duree').change(function() {
		resaCalendrierLiveResa();
	});
	
	resaCalendrierSelect = function(a, d) {
		var extra = $(a).find('.resa-calendrier-date-extra').html();
		
		$('.resa-calendrier-show').html(extra).find('*').removeClass('hidden');
		$('.resa-calendrier-value').val(d);
		$('.resa-calendrier-wrap').hide();
		$('#resa input, #resa select').css('visibility', 'visible');
		var selectDuree = $('select#resa_duree');
		
		if (selectDuree.length == 1) {
			var tdJours = $(a).parents('.resa-calendrier-wrap').find('tbody td:not(.resa-calendrier-date-vide)');
			var indexDebut = tdJours.index($(a));
			var indexFin = indexDebut;
			var ok = true;
			while (ok) {
				var tdJour = tdJours.get(indexFin + 1);
				if(!$(tdJour).hasClass('resa-calendrier-date-prix')) ok = false; else indexFin++;
			}
			var qteDispo = indexFin - indexDebut + 1;
			var _prev = selectDuree.val();
			selectDuree.empty();
			var conf = {min:$('input[name="resa_duree_min"]'), max:$('input[name="resa_duree_max"]'), selected:$('input[name="resa_duree_selected"]')};
			for (var i = conf.min.val(); i <= conf.max.val(); i++) {
				if (i <= qteDispo) $(selectDuree).append('<option value="'+i+'"'+(i == conf.selected.val() ? ' selected="selected"' : '')+'>'+i+' jour'+(i>1 ? 's' : '')+'</option>');
			}
		}
		resaCalendrierSelected(d);
		resaCalendrierLiveResa();
	};
	
	resaCalendrierLiveResa = function () {
		var selectDuree = $('select#resa_duree');
		var liveResaPrix = parseFloat($('.resa-calendrier-prix').attr('prix'));
		var tdJours = $('.resa-calendrier-wrap tbody td.resa-calendrier-date-prix');
		var tdJourSelected = $('.resa-calendrier-date-selected');
		var indexSelected = tdJours.index(tdJourSelected);

		if (selectDuree.length == 1) {
			var duree = $(selectDuree).val();
			for (var i = 1; i < duree; i++) {
				indexSelected++;
				var tdJour = tdJours.get(indexSelected);
				liveResaPrix += parseFloat($(tdJour).find('*[prix]').attr('prix'));
			}
		}
		
		$('.resa-calendrier-show div[liveresatarif]').remove();
		$('.resa-calendrier-show').prepend('<div class="hidden" liveresatarif="'+liveResaPrix+'" />');
		$('#resa-choix .resa-choix-date').html($('.resa-calendrier-date-extra>span:eq(0)', tdJourSelected).text());
		liveResaTarif();
	};
	if ($('.resa-calendrier').length > 0) {
		$('select#resa_duree').change(function() { $('input[name="resa_duree_selected"]').val($(this).val()); });
		resaCalendrierSelect($('.resa-calendrier-date-selected'), $('.resa-calendrier-date-selected').attr('id'));
	}
});
