	

	 var voir_map ;

	function setInDate(val){
		document.formEtape1.date_debut.value = val;		
		$("#calendar").hide("normal");
	}
	
	function choixStation(id_station, nom){
		$('#id_station').val(id_station);
		$('#search_station').val(nom);
		$("#liste_stations").myHide();
		
		//liste des magasins de la station
		$.post(base_url+"/get_magasins/", { id_station: id_station }, function(magasins){
				if (magasins.length>0){
					
					// chargement de la carto
					var frame = document.getElementById('iframeCarto');
					frame.contentWindow.ChargerLaStation(id_station);
					$('#voir_carte:hidden').show();
					
					$('#sejour').parent('div').addClass('overlay');
					$('#sejour').find('div:first').addClass('overlay');
					
					afficheListeMagasins(magasins);
				}
				else{
					// station liée
					$.post(base_url+"/station_liee/", { id_station: id_station },function(station_liee){
						if (station_liee.length>0){
							$.nyroModalManual({
					      		ajax: {url: base_url+'/stations_liees/'+id_station}
							});
						}
					}, "json");
				}
		}, "json");
		
		
		
		return false;
		
	}
	

	
	// liste des magasins
	function afficheListeMagasins(magasins){
		mon_select = $('#id_magasin');
		if (magasins.length>0){
			mon_select.find('option').remove();
			if (magasins.length>1){
				$.ajax({
					   type: "GET",
					   url: base_url+"/traduc/choisissez_votre_magasin",
					   success: function(data){
							mon_select.append('<option value="0">'+data+'</option>');
						},
						 async: false
				});	
				voir_map();
			}
			else{
				
				afficheMagasin(magasins[0].id_magasin);
			}
			for (i in magasins){
				mon_select.append('<option value="'+magasins[i].id_magasin+'">'+magasins[i].nom+' </option>');
			}
		
			
		}	
	}
	// affiche un magasin
	function afficheMagasin(id_magasin){
		$("#bloc_info").children('div:visible, object:visible').fadeOut('normal',
				function(){
					$('#infoMagasin:hidden').fadeIn();
				}		
		);
		
		$.post(base_url+"/get_magasin/"+id_magasin+"/1",function(str_magasin){
			$('#infoMagasin').html(str_magasin);
			$('.nyroModal').nyroModal();
			// affichage de la carto
			 $("#voir_map").click(voir_map);
		}, "json");
		 
		
	}
	
	
	function jumpToResa() {
	   window.location = String(window.location).replace(/\#.*$/, "") + "#box-reserver";
	}	


	
// script pour masquer le calendrier quand on clique � l'exterieur
var lien = 0;
$(document).ready(function() {
		
	
	
	
	
		// cache le calendrier si on clique ? l'exterieur du calendrier
		$(document.body).click(function(event) {
	   		target = $(event.target);
			if(target.attr("id")!='calendar' && target.attr("id")!='lien_calendar' && target.parents("#calendar").length ==0){
				$("#calendar").hide("normal");
			}
			
		});	
		// click sur l'input qui permet de chercher des stations
		$("#search_station").click( function() { 
					if ($("#id_station").val() == 0){
						this.value= '';
					}
				} 
		);
		// auto compl?tion apres saisi dans le champs input de recherche de station
		/*$("#search_station").keyup( function() {
					if (this.value.length==0){
						$("#liste_stations").myHide();						
					}
					else{								
						$("#liste_stations li").hide();						
						listeElem = $("#liste_stations li").filter(':contains-ic('+this.value+')');
						//listeElem = $("#liste_stations li").filter(':contains(AV)');						
						listeElem.show();
						if (listeElem.length == 0 ){
							// il n'y pas de resultats
							if ($("#liste_stations").is(":visible")){
								$("#liste_stations").myHide();								
							}
						}
						else{
							if ($("#liste_stations").is(":hidden")){
								$("#liste_stations").mySlideDown();
							}
						} // fin du else if (listeElem.length == 0 ){
					}// fin du else if (this.value.length==0){
				} 
			);*/
		var ind_courant;
		$('#search_station').keyup(function(keyEvent){
			switch (keyEvent.which){
			case 40:
				// flèche bas				
				if ($("#liste_stations").is(":visible")){
					if ($("#liste_stations li:visible.selected").length == 0){
						$("#liste_stations li:visible:first").addClass('selected');
						
					}
					else{
						ind = $("#liste_stations li:visible").index($("#liste_stations li.selected:visible"));
						//alert(ind);
						
						ind = ind + 1;
						var monLi = $("#liste_stations li:visible:eq("+ind+")");
						$("#liste_stations li.selected").removeClass('selected');
						monLi.addClass('selected');
						var offset = $("#liste_stations").offset();
						var  height = $("#liste_stations").height()+offset.top;
						var scrollTop = $("#liste_stations").scrollTop();
						offset = monLi.offset();
						
						if ((offset.top+monLi.height()) > height){
							//alert("il va falloir scroller "+offset.top+" > "+height);
							$("#liste_stations").scrollTop(scrollTop+60);
						}
					}
				}
				break;
			case 38:
				// flèche haut				
				if ($("#liste_stations").is(":visible")){
					if ($("#liste_stations li:visible.selected").length > 0){
						ind = $("#liste_stations li:visible").index($("#liste_stations li.selected:visible"));
						ind = ind - 1;
						if (ind<0){
							ind = 0;
						}
						var monLi = $("#liste_stations li:visible:eq("+ind+")");
						$("#liste_stations li.selected").removeClass('selected');
						monLi.addClass('selected');
						
						var offset = $("#liste_stations").offset();
						var  top = offset.top;
						var scrollTop = $("#liste_stations").scrollTop();
						offset = monLi.offset();
						
						if ((offset.top-monLi.height()) < top){
							//alert("il va falloir scroller "+offset.top+" > "+height);
							$("#liste_stations").scrollTop(scrollTop-60);
						}
						
					}
				}
				break;
			case 13 : 
				// enter
				station = $("#liste_stations li.selected");
				if (station.length>0){
					choixStation (station.attr('id'), station.find("a").text());
				}
				break;
			default:
				if (this.value.length==0){
					$("#liste_stations").myHide();						
				}
				else{								
					$("#liste_stations li").hide();						
					listeElem = $("#liste_stations li").filter(':contains-ic('+this.value+')');
					//listeElem = $("#liste_stations li").filter(':contains(AV)');						
					listeElem.show();
					if (listeElem.length == 0 ){
						// il n'y pas de resultats
						if ($("#liste_stations").is(":visible")){
							$("#liste_stations").myHide();								
						}
					}
					else{
						if ($("#liste_stations").is(":hidden")){
							$("#liste_stations").mySlideDown();
							$("#liste_stations").scrollTop(0);
						}
					} // fin du else if (listeElem.length == 0 ){
				}// fin du else if (this.value.length==0){
			 
				
				break;
			}
			
		});
	
	
		 // choix d'un magasin
		 $('#id_magasin').change(function(){
			id_magasin = this.value;
			if (id_magasin>0){
				//id_station = $('#id_station').value;
				afficheMagasin(id_magasin);
				// liste des durées
				listeDurees();
			}
			else{
				voir_map();
			}
			
		 });
		 // affichage de la carto
		 voir_map = function( ) {
			$("#bloc_info").children('div:visible, object:visible').fadeOut('normal',
						function(){
							$('#map:hidden').fadeIn();
						}		
				);
		 };
		 $("#voir_map,#voir_carte").click(voir_map);
		  // calendar
		 var calendar = function(){
			
			 id_station = $('#id_station').val();
			 id_magasin = $('#id_magasin').val();
			 date_debut='';
			 if (arguments.length>0){
				 t = typeof arguments[0];
				 if (t == 'string') { 
					 date_debut = arguments[0];
				 }
				
			 }
			 if (date_debut==''){
				 date_debut = $(this).val();
			}
			 
			 $.post(base_url+"/get_calendar/",				 
					{ 	id_station: id_station,
				 		id_magasin:id_magasin,
				 		date_debut:date_debut
				 	},
					function(data){
				 		 
					 	var_calendar = $("#calendar");
						if (data.error=="1"){
							alert(data.message);
						}
						else{
							var_calendar.html(data.calendar);
							var_calendar.show();
							var_calendar.find('td a').click(function(){
								var reg1=new RegExp("[#]","g");
								var tableau=$(this).attr("href").split(reg1);
								if (tableau.length>0){
									// SF :  je fais ça car sur ie8 le fonctionnnement est différent
									if (tableau.length==1){
										var value = tableau[0];
									}
									else{
										var value = tableau[1];
									}
									
								}
								else{
									var value = $(this).attr("href");
									
								}
								
								
								$("#date_debut").val(value);
								var_calendar.hide();
								listeDurees();
								return false;
							});
							var_calendar.find('th a#calendar_next, th a#calendar_previous').click(function(){
								calendar($(this).attr("href").replace('#',''));
								return false;
							});
						}
						
						
					
				 	},
			 		"json");
			 
		 };
		
		  $("#date_debut").click(calendar);
		 
		 // récupération de la liste des durées de réservation
		 listeDurees = function(){
			
			 id_station = $('#id_station').val();
			 id_magasin = $('#id_magasin').val();
			 date_debut = $('#date_debut').val();
			 $.post(base_url+"/get_liste_durees/",				 
						{ 	id_station: id_station,
					 		id_magasin:id_magasin,
					 		date_debut:date_debut
					 	},
						function(data){
					 		durees = $('#id_duree_reservation');
					 		id_duree_reservation = durees.val();
					 		
					 		
					 		if (data.error==1){
					 			if (data.type_error=='date'){
					 				$('#date_debut').val('');
					 			}
					 		}
					 		else{
					 			durees.find('option').remove()
					 			for (i in data.durees){
					 				d = data.durees[i];
					 				if(id_duree_reservation == d.id_duree_reservation){
					 					selected = "selected";
					 				}
					 				else{
					 					selected = '';
					 				}
					 				durees.append('<option value="'+d.id_duree_reservation+'" '+selected+'>'+d.traduc+' </option>');
								}
					 		}
					
						},
				 		"json");
		 };
		 
		 $("#date_debut").change(listeDurees);
		 // ----------------------------
		 $.fn.myHide = function(){
		 
			 if ($.browser.msie && $.browser.version.substr(0,1)<7){
					 $('#formEtape1 select').show();
			}
			$(this).hide();
		 }
		// ----------------------------
		 $.fn.mySlideDown = function(){
			 
			 if ($.browser.msie && $.browser.version.substr(0,1)<7){
				
				 $('#formEtape1 select').hide();
			}
			$(this).slideDown();
		 }
		 // --------------------------
		 // affichage de la carto
		 voir_stations = function( ) {
			$("#bloc_info").children('div:visible, object:visible').fadeOut('normal',
						function(){
							$('#listeCompleteStations:hidden').fadeIn();
						}		
				);
		 };
		 $("#voir_liste_stations").click(voir_stations);
		
		
		
	
});