/*

	Gestion complète des restaurants

*/


Event.observe(window,'load',function() {
									 
	var module = $('module').value ;
	
	if(module==1) { // Affichage d'un restarant
			
		/*
			Gestion du panel de photo
		*/
		var numPictures = $('numberPictures').value;
		//alert(numPictures);
		if(numPictures>1) {
			
			var scroolI = 0 ;
			var nbrElementBox = 0 ;
			var timer = 0 ;
			var stateControl = 0 ;
			
			$('contentPictures').childElements().each(function(e) {
				nbrElementBox = nbrElementBox + 1 ;
			});
			
			$('contentPictures').observe('mouseover',function() {
				if(timer!=0) 
					clearInterval(timer);
					if(stateControl==0) 
						$('deplaceSlide').appear({duration:0.4});
				stateControl = 1 ;
			});
			$('contentPictures').observe('mouseout',function() {
				timer = setInterval(function() {
							$('deplaceSlide').fade({duration:0.4});
							stateControl = 0 ;
							clearInterval(timer);
						},500);
			});
			$('deplaceSlide').observe('mouseover',function() {
				if(timer!=0) 
					clearInterval(timer);
			});
			$('deplaceSlide').observe('mouseout',function() {
				timer = setInterval(function() {
							$('deplaceSlide').fade({duration:0.4});
							stateControl = 0 ;
							clearInterval(timer);
						},500);
			});
				// Gestion des fleches
				$('prevPicture').observe('click',function() {
					if(scroolI>0) {
						$('contentPictures').style.left = ((-scroolI*300)+300)+'px' ;
						scroolI = scroolI - 1 ;
					}
				});
				
				$('nextPicture').observe('click',function() {
					if(scroolI<=(nbrElementBox-2)) {
						$('contentPictures').style.left = ((-scroolI*300)-300)+'px' ;
						scroolI = scroolI + 1 ;
					}
				});
			
			
			
		}
			
			
			
			
			
		/*
			Gestion de l'itinéraire
		*/
		var localisationState = $('localisationState').value ;
	
		if(localisationState==1) {
			
			//affichage de la carte 
			var lat = $('coord_lat').value ;
			var long = $('coord_long').value ;
			
			
			// VERIFCIATION DE LA VILLE POUR L'itinéraire
			var mapGeo = new GClientGeocoder();
			
			$('itineraireState').observe('click',function(event) {
				Event.stop(event);
				latLong = mapGeo.getLatLng($('cityItineraire').value, function(point) {
					if(point) {
						$('itiFound').setStyle({ display: 'block'});
						$('itiNotFound').setStyle({ display: 'none'});	
						$('geoCoorDepart').value = $('cityItineraire').value;
					} else {
						$('itiFound').setStyle({ display: 'none'});
						$('itiNotFound').setStyle({ display: 'block'});
					}
				});
			});
			
			$('linkSubmitItin').observe('click',function(event) {
				Event.stop(event);
				$('itineraireForm').submit();		   
			});
		}

		/*
			Gestion du panorama du restaurant
		*/
		var nombrePanorama = $('statePanorama').value ;
		
		if(nombrePanorama==1) {
			//alert('ok');
			//affichage des elements (menu, block panorama 
			//$('itemPanoramaMenu').setStyle({ 'display': '' });			
			//$('Panoramique').setStyle({ 'display': '' });			
			//$('contentPanoramas').setStyle({ 'display': '' });			
			
			//direction  1=>right; 0=>left
			var direction = 0;
			var vitesse = 50;
			
			//verification du nombre de panorama
			function panoramaSlide() {
				
				if(direction==0) {
					//on verifie que l'on ai arrivé au bout (left=0)
					etatSlideDirection = parseInt($('panorama').style.left);
					
					if(etatSlideDirection==0) {
						direction = 1 ;
					} else {
						$('panorama').style.left = (etatSlideDirection+1)+'px' ;
					}
					
				} else {
					
					etatSlideDirection = parseInt($('panorama').style.left);
					widthPanorama = parseInt($('panorama').down('img').width);
					
					//alert(widthPanorama+etatSlideDirection);
					if((widthPanorama+etatSlideDirection)==680) {
						direction = 0;
					} else {
						$('panorama').style.left = (etatSlideDirection-1)+'px' ;
						direction = 1 ;
					}
				}
			}
			
			//evenement sur les hover
			$$('.panSpeed').each(function(e) {
				e.observe('mouseover',function() {
					direction = (e.id=='rightS') ? 1 : 0; 						   
					clearInterval(timer);
					vitesse = 10;  
					timer = setInterval(panoramaSlide,vitesse);
				});
				e.observe('mouseout',function() {
					clearInterval(timer);
					vitesse = 50;  
					timer = setInterval(panoramaSlide,vitesse);
				});
			});
			
			$$('.panMoreSpeed').each(function(e) {
				e.observe('mouseover',function() {
					direction = (e.id=='rightMS') ? 1 : 0; 
					clearInterval(timer);
					vitesse = 25;  
					timer = setInterval(panoramaSlide,vitesse);
				});
				e.observe('mouseout',function() {
					clearInterval(timer);
					vitesse = 50;  
					timer = setInterval(panoramaSlide,vitesse);
				});
			});
			//demarage du timer
			var timer = setInterval(panoramaSlide,vitesse);
			
		}
		/*
			Gestion personnalisation Stars
		*/
		function getPos(pId) {
			
			switch(pId) {
				case '1':
					return '-64px 0px';
					break;
				case '2':
					return '-48px 0px';
					break;
				case '3':
					return '-32px 0px';
					break;
				case '4':
					return '-16px 0px';
					break;
				case '5':
					return '0px 0px';
					break;
				default:
					return '-80px 0px';
					break;
			}
		}
			
		
		
		$$('.selectNote').each(function(e) {
			var replaceSelect = '<input type="hidden" name="'+e.name+'" id="'+e.id+'" value=""/>' ;
			
			$$('.el_opt_'+(e.id.substring(5,6))+'').each(function(option) {
				replaceSelect += '<a href="#" id="'+option.value+'" title="'+option.innerHTML+'" class="stars_'+(e.id.substring(5,6))+'"></a>';   
			});
			
			replaceSelect += '' ;
			$(e.id).up().update(replaceSelect);
		});
		
		for(i=1; i<=8; i++) {
			
			$$('.stars_'+i).each( function(e) {
				var iTem = i ;
				e.observe('mouseover', function(event) {
					pos = getPos(e.id);
					e.up().setStyle({ backgroundPosition: pos});
				});
				e.observe('mouseout', function(event) {
					pos = getPos($('note_'+iTem).value);
					e.up().setStyle({ backgroundPosition: pos});
				});
				e.observe('click',function(event) {
					Event.stop(event);
					 $('note_'+iTem).value = e.id ;  
					 pos = getPos(e.id);
					e.up().setStyle({ backgroundPosition: pos});
				});
			});
		}
		
		
		
		/*
			Gestion de l'ajout de commentaire
		*/
		$('submitNotation').observe('click',function(event) {
			Event.stop(event);						
			
			var note_cuisine = encodeURIComponent($('note_1').value) ;
			var note_cadre = encodeURIComponent($('note_2').value) ;
			var note_ambiance = encodeURIComponent($('note_3').value) ;
			var note_accueil = encodeURIComponent($('note_4').value) ;
			var note_proprete = encodeURIComponent($('note_5').value) ;
			var note_service = encodeURIComponent($('note_6').value) ;
			var note_recommandation = encodeURIComponent($('note_7').value) ;
			var note_prix = encodeURIComponent($('note_8').value) ;
			var idRestaurant = encodeURIComponent($('idRestaurant').value);
			var observation = encodeURIComponent($('observation').value);
			
			new Ajax.Request('php_include/gestionnaireRestaurants.php', {
				method : 'POST',
				parameters : 'modeAjax=1&mode=comments&idRestaurant='+idRestaurant+'&observation='+observation+'&note_1='+note_cuisine+'&note_2='+note_cadre+'&note_3='+note_ambiance+'&note_4='+note_accueil+'&note_5='+note_proprete+'&note_6='+note_service+'&note_7='+note_recommandation+'&note_8='+note_prix,
				onSuccess: function(transport) {
					$('confirmNotation').update(transport.responseText);
					//remise des champs à zéro 
					for(i=1;i<=8; i++) {
						 $('note_'+i).up().setStyle({ backgroundPosition: '-80px 0px'}) ;
						 $('note_'+i).value = '';
						 $('observation').value = '';
					}
				}
			});
		});

	} else if(module==2) { // Modification d'un restaurant
		
	} else if(module==3) { // Ajout d'un restaurant
		
	} else { 
		
	}
	
									 
});
