var imageAresize;
var periodical;
var Hotel = {
	last_proposition_view:'',
	changeEnfantNb:function(chambre_num) {
		var chambre_nb=document.getElementById('search_chambre_nb').value;
		var enfant_nb_max=0;
		var enfant_nb_total=0;
		for (i=1;i<=chambre_nb;i++) {
			if (document.getElementById('chambre_'+i+'_enfant_nb').value>enfant_nb_max) {
				enfant_nb_max=document.getElementById('chambre_'+i+'_enfant_nb').value;
			}
			enfant_nb_total+=parseInt(document.getElementById('chambre_'+i+'_enfant_nb').value);
		}
		for (i=1;i<=document.getElementById('chambre_1_enfant_nb').length;i++) {
			if (i<=enfant_nb_max) {
				td_style='';
			} else {
				td_style='none';
			}
			$$('.sysEnfant' + i).each(function(el) {el.style.display=td_style;});
		}
		if (enfant_nb_total>0) {
			document.getElementById('tr_age_enfant_entete').style.display='';
			document.getElementById('tr_age_enfant_choix').style.display='';
		} else {
			document.getElementById('tr_age_enfant_entete').style.display='none';
			document.getElementById('tr_age_enfant_choix').style.display='none';
		}
		
		if (chambre_num!='') {
			//chambre_<?php echo $i; ?>_enfant_<?php echo $j; ?>
			var enfant_nb_encours=document.getElementById('chambre_'+chambre_num+'_enfant_nb').value;
	
			for (i=1;i<document.getElementById('chambre_1_enfant_nb').length;i++) {
				//alert('chambre_'+chambre_num+'_enfant_' + i);
				if (i<=enfant_nb_encours) {
					document.getElementById('chambre_'+chambre_num+'_enfant_' + i).style.display='';
					//alert('show');
				} else {
					document.getElementById('chambre_'+chambre_num+'_enfant_' + i).style.display='none';
					//alert('hide');
				}
			}
			if (enfant_nb_encours>0) {
				document.getElementById('chambre_'+chambre_num+'_age_enfant_tr').style.display='';
			} else {
				document.getElementById('chambre_'+chambre_num+'_age_enfant_tr').style.display='none';
			}
			
		}
	},
	changePays:function() {
		var code_pays=document.getElementById('search_pays_code').value;
		document.getElementById('search_code').value='';
		document.getElementById('search_libelle').value='';
		var myHTMLRequest = new Request.HTML({url:'index.php?action=poi_hotel_country'}).post($('frm_search_hotel'));
	},
	setDestination:function(code,libelle,num) {
		document.getElementById('search_code').value=code;
		document.getElementById('search_libelle').value=libelle;
		if (document.getElementById('destination_choix_' + num)) {
			document.getElementById('destination_choix_' + num).checked=true;
		}
		
	},
	changeChambreNb:function() {
		var chambre_nb=document.getElementById('search_chambre_nb').value;
		for(i=1;i<=document.getElementById('search_chambre_nb').length;i++) {
			if (i<=chambre_nb) {
				style_tr='';
			} else {
				style_tr='none';
			}
			document.getElementById('chambre_'+i+'_repartition_tr').style.display=style_tr;
			if (style_tr=='' && parseInt(document.getElementById('chambre_'+i+'_enfant_nb').value)==0) {
				style_tr='none';
			}
			document.getElementById('chambre_'+i+'_age_enfant_tr').style.display=style_tr;
		}
		Hotel.changeEnfantNb('');
	},
	lanceRecherche:function() {
		Common.showLoading('recherche_moteur', 5);
		//if (typeof tab_destination[$('search_libelle').value] != 'undefined') {
		//	$('search_code').value=tab_destination[$('search_libelle').value];
		//} else {
		//	$('search_code').value='';
		//}
		document.getElementById('frm_search_hotel').submit();
	},
	showRepartition:function() {
		document.getElementById('div_repartition').style.position='absolute';
		document.getElementById('div_repartition').style.display='';
	},
	hideRepartition:function() {
		document.getElementById('div_repartition').style.display='none';
	},
	updateCode:function(obj) {
		if (tab_destination[obj.value.trim()]) {
			document.getElementById('search_code').value=tab_destination[obj.value.trim()];
		} else {
			document.getElementById('search_code').value='';
		}
	},
	showOnglet:function(obj_click) {
		var obj_parent=$(obj_click).getParent();
		obj_parent.getChildren('.onglet').each(function(elem) {
			$(elem.id + '_pannel').style.display='none';
			$(elem.id).removeClass('onglet_sel');
		});

		$(obj_click).addClass('onglet_sel');
		$(obj_click.id + '_pannel').style.display='';
		Hotel.togglePhotoMain(obj_click);
	},
	showOngletCWT:function(obj_click) {
		var obj_parent=$(obj_click).getParent().getParent().getParent();
		obj_parent.getChildren().each(function(elem) {
			if (elem.className.indexOf('spacerLeft')<0 && elem.className.indexOf('cB')<0) {
				elem.getChildren().each(function(elem_sub) {
					elem_sub.getChildren('.onglet').each(function(elem_sub_sub) {
						elem_sub_sub.removeClass('onglet_sel');
						if (document.getElementById(elem_sub_sub.id + '_pannel')) {
							$(elem_sub_sub.id + '_pannel').style.display='none';
						}
					});
					elem_sub.getChildren('.line').each(function(elem_sub_sub) {
						elem_sub_sub.removeClass('line_sel');
					});
				});
			}
		});
		$(obj_click).addClass('onglet_sel');
		var obj_parent=$(obj_click).getParent();
		obj_parent.getChildren('.line').each(function(elem) {
			$(elem).addClass('line_sel');
		});
		$(obj_click.id + '_pannel').style.display='';
		Hotel.togglePhotoMain(obj_click);
		//
	},
	togglePhotoMain:function(obj_click) {
		var tab=obj_click.id.split('_');
		if (document.getElementById('photo_' + tab[1] + '_main')) {
			if (tab[0]=='photo') {
				document.getElementById('photo_' + tab[1] + '_main').style.display='none';
			} else {
				document.getElementById('photo_' + tab[1] + '_main').style.display='';
			}
		}
	},
	navPage:function(str) {
		Common.showLoading('');
		document.getElementById('paging').value=str;
		document.getElementById('frm_tri_reulstat_hotel').submit();
	},
	triListe:function() {
		Common.showLoading('');
		document.getElementById('frm_tri_reulstat_hotel').submit();
	},
	showPhoto:function(code,src) {
		if (document.getElementById('photo_detail_'+code+'_src')) {
			document.getElementById('photo_detail_'+code+'_src').setAttribute('src', src);
			var periodical = Hotel.imageResize.periodical(500);
			$('photo_detail_'+code+'_src').addEvent('load', function() {
				Hotel.imageResize('photo_detail_'+code+'_src');
				$clear(periodical); 
			});
		}
	},
	calendrier: function() {
		/*if($defined($('search_date_du')) && $('search_date_du').hasClass('calendar') && $defined($('search_date_au')) && $('search_date_au').hasClass('calendar')) {
			calendar = new Calendar({ 
				search_date_du : 'd/m/Y', 
				search_date_au : 'd/m/Y'
			}, { direction: 1, tweak: { x: 6, y: 0 }});
		}*/
		if($defined($('search_date_du')) && $defined($('search_date_au'))) {
			afficheEntete(false);
			setCheminCalendrier('/templates/ladepeche');
			cache_calendrier();
			setSensInput("depart","search_date_du");
			setAllerRetour(1);
			setSensInput("retour","search_date_au");
		}
	},
	imageResize: function(element){
		// inMW  : Largeur maximale
		// inMH   : Hauteur maximale
		if(Browser.Engine.name == 'gecko') {
			$(element).setProperties({width: '',height: ''});
		}
		var maxWidth = 220;
		var maxHeight = 300;
		// Declarations des variables "Nouvelle Taille"
		var dW = 0;
		var dH = 0;
		// On recupere les tailles reelles
		var image = $(element).getSize();
		var h = dH = image.y;
		var w = dW = image.x;
		// Si la largeur ou la hauteur depasse la taille maximale
		if ((h >= maxHeight) || (w >= maxWidth)) {
			// Si la largeur et la hauteur depasse la taille maximale
			if ((h >= maxHeight) && (w >= maxWidth)) {
				// On cherche la plus grande valeur
				if (h > w) {
					dH = maxHeight;
					// On recalcule la taille proportionnellement
					dW = parseInt((w * dH) / h, 10);
					if (dW > maxWidth) {
						dW = maxWidth;
						dH = parseInt((dH * dW) / w, 10);
					}
				} else {
					dW = maxWidth;
					// On recalcule la taille proportionnellement
					dH = parseInt((h * dW) / w, 10);
					if (dH > maxHeight) {
						dH = maxHeight;
						dW = parseInt((dW * dH) / h, 10);
					}
				}
			} else if ((h > maxHeight) && (w < maxWidth)) {
				// Si la hauteur depasse la taille maximale
				dH = maxHeight;
				// On recalcule la taille proportionnellement
				dW = parseInt((w * dH) / h, 10);
			} else if ((h < maxHeight) && (w > maxWidth)) {
				// Si la largeur depasse la taille maximale
				dW = maxWidth;
				// On recalcule la taille proportionnellement
				dH = parseInt((h * dW) / w, 10);
			}
		}
		//alert('dW='+dW);
		if(image.y != 0 && image.x != 0) {
			$(element).setProperties({
				width: dW,
				height: dH
			});
			$clear(periodical); 
		}
    },
    navResa:function() {
    	Common.showLoading('');
    	$('frm_reservation').action.value='valid_part1';
    	$('frm_reservation').submit();
    },
    navCoords:function() {
    	Common.showLoading('');
    	$('frm_reservation').action.value='';
    	$('frm_reservation').frm_part.value='part1';
    	$('frm_reservation').submit();
    },
    checkCoupon:function() {
    	Common.showLoading('');
		$('frm_reservation').action.value='valid_coupon';
		$('frm_reservation').submit();
    },
    reservation:function() {
		Common.showLoading('');
		$('frm_reservation').action.value='reserver';
		$('frm_reservation').submit();
    },
    filtre:function(active) {
    	Common.showLoading('');
    	var valeur='N';
    	if (active) {
    		valeur='Y';
    	}
    	$('frm_filtre_hotel').filtre_actif.value=valeur;
    	$('frm_filtre_hotel').submit();
    },
    setResaPrincipal:function(code) {
    	if (document.getElementById('radio_principal_' + code)) {
    		$('radio_principal_' + code).click();
    	}
    },
    submitFrm:function(frm_id) {
    	$(frm_id).submit();
    },
    newSearchType: function(type_search) {
    	$$('.tr_poi, .tr_adresse').setStyle('display', 'none');
    	if (type_search=='poi') {
    		$('bt_search_poi').addClass('sel');
    		$('bt_search_adresse').removeClass('sel');
    		$$('.tr_poi').setStyle('display', '');
    	} else {
    		$('bt_search_adresse').addClass('sel');
    		$('bt_search_poi').removeClass('sel');
    		$$('.tr_adresse').setStyle('display', '');
    	}
    	$('search_type').value=type_search;
    },
    setSearchType:function(type_search) {
		document.getElementById('search_destination_elem').style.display='none';
		if (document.getElementById('search_destination_extend_elem')) {
			document.getElementById('search_destination_extend_elem').style.display='none';
		}
		document.getElementById('search_adresse_elem').style.display='none';
		if (document.getElementById('search_adresse_extend_elem')) {
			document.getElementById('search_adresse_extend_elem').style.display='none';
		}
		document.getElementById('search_ville_elem').style.display='none';
		if (document.getElementById('search_ville_extend_elem')) {
			document.getElementById('search_ville_extend_elem').style.display='none';
		}
		document.getElementById('search_cp_elem').style.display='none';
		if (document.getElementById('search_cp_extend_elem')) {
			document.getElementById('search_cp_extend_elem').style.display='none';
		}
		document.getElementById('search_pays_elem').style.display='none';
		if (document.getElementById('search_pays_extend_elem')) {
			document.getElementById('search_pays_extend_elem').style.display='none';
		}
		if (document.getElementById('bt_search_adresse')) {
			$('bt_search_adresse').removeClass('sel');
		}
		if (document.getElementById('bt_search_poi')) {
			$('bt_search_poi').removeClass('sel');
		}
		if (type_search=='poi') {
			$('bt_search_poi').addClass('sel');
			document.getElementById('search_destination_elem').style.display='';
			if (document.getElementById('search_destination_extend_elem')) {
				document.getElementById('search_destination_extend_elem').style.display='';
			}
		} else {
			$('bt_search_adresse').addClass('sel');
			document.getElementById('search_adresse_elem').style.display='';
			if (document.getElementById('search_adresse_extend_elem')) {
				document.getElementById('search_adresse_extend_elem').style.display='';
			}
			document.getElementById('search_ville_elem').style.display='';
			if (document.getElementById('search_ville_extend_elem')) {
				document.getElementById('search_ville_extend_elem').style.display='';
			}
			document.getElementById('search_cp_elem').style.display='';
			if (document.getElementById('search_cp_extend_elem')) {
				document.getElementById('search_cp_extend_elem').style.display='';
			}
			document.getElementById('search_pays_elem').style.display='';
			if (document.getElementById('search_pays_extend_elem')) {
				document.getElementById('search_pays_extend_elem').style.display='';
			}
		}
        document.getElementById('search_type').value=type_search;
    },
    showMap:function() {
    	Hotel.changeViewResultat('carte');
    },
    showList:function() {
    	Hotel.changeViewResultat('liste');
    },
    changeViewResultat:function(str) {
    	Common.showLoading('');
    	document.getElementById('search_mode_aff').value=str;
    	document.getElementById('frm_tri_reulstat_hotel').submit();
    },
    showDetailProposition:function(obj_id) {
    	if (Hotel.last_proposition_view!=''
    		&& document.getElementById(Hotel.last_proposition_view)) {
    		document.getElementById(Hotel.last_proposition_view).style.display='none';
    	}
    	document.getElementById(obj_id).style.display='';
    	Hotel.last_proposition_view=obj_id;
    },
    scrollTo:function(ancre,nb_call) {
    	if (typeof nb_call=='undefined') {
			nb_call=0;
		}
    	if (document.getElementById('proposition_hotel_' + ancre)) {
    		new Fx.Scroll(window).toElement('proposition_hotel_' + ancre);
    		nb_call++;
    		if (nb_call<2) {
    			Hotel.scrollTo(ancre,nb_call);
    		}
    		
    	}
    },
    setAdresse:function(adresse,cp,ville,pays,lat,lng,num) {
		document.getElementById('search_adresse').value=adresse;
		document.getElementById('search_cp').value=cp;
		document.getElementById('search_ville').value=ville;
		document.getElementById('search_pays').value=pays;
		document.getElementById('search_adresse_code').value='latlng_'+lat+'_'+lng;
		if (document.getElementById('destination_choix_' + num)) {
			document.getElementById('destination_choix_' + num).checked=true;
		}
    },
    setAdresseCodeEmpty:function() {
    	if (document.getElementById('search_adresse_code')) {
    		document.getElementById('search_adresse_code').value='';
    	}
    },
    
    init: function() {
    	if (document.getElementById('search_adresse')) {
    		$('search_adresse').addEvent('change', function(){
    		    Hotel.setAdresseCodeEmpty();
    		});
    	}
    	if (document.getElementById('search_cp')) {
    		$('search_cp').addEvent('change', function(){
    		    Hotel.setAdresseCodeEmpty();
    		});
    	}
    	if (document.getElementById('search_ville')) {
    		$('search_ville').addEvent('change', function(){
    		    Hotel.setAdresseCodeEmpty();
    		});
    	}
    	if (document.getElementById('search_pays')) {
    		$('search_pays').addEvent('change', function(){
    		    Hotel.setAdresseCodeEmpty();
    		});
    	}
    	/*
    	 * Calendrier
    	 */
    	if(typeof YAHOO!="undefined") {
    		YAHOO.namespace("calendrier");
    		function handleSelect(type, args, obj) {
    			var selected = args[0]; 
    			selected = this.toDate(selected[0]); 
    			var day = selected.getDate();
    			if (day<10) {
    				day = "0" + day;
    			}
    			var month = selected.getMonth()+1;
    			if (month < 10) {
    				month = "0" + month;
    			}
    			var year = selected.getFullYear();
    			this.input.value = day + '/' + month + '/' + year;
    			
    			// Si inputFin est renseigné, alors on le change à J+5
    			if(this.inputFin) {
    				selected.setTime(selected.getTime() + 1000*60*60*24*5);
    				day = selected.getDate();
    				if (day<10) {
    					day = "0" + day;
    				}
    				month = selected.getMonth()+1;
    				if (month < 10) {
    					month = "0" + month;
    				}
    				year = selected.getFullYear();
    				selected =  day + '/' + month + '/' + year;
    				this.inputFin.value = selected;
    			}
    			$('calendarContainer').innerHTML = '';
    		}

    		YAHOO.calendrier.init = function(input, debut, fin) {
    			var input = $(input), debut = $(debut), fin = $(fin);
    			// Si on choisit la date de debut, alors on recalculera la fin
    			dateDebut = debut.value.split('/');
    			dateFin = fin.value.split('/');
    			var selected_date = dateDebut[1] + "/" + dateDebut[0] + "/" + dateDebut[2] + " - " + dateFin[1] + "/" + dateFin[0] + "/" + dateFin[2];
    			var page_date = dateDebut[1] + "/" + dateDebut[2];
    			// Calcul de la date mini (J+2)
    			var minDate = new Date();
    			var timestamp = minDate.getTime();
    			timestamp += 60*60*24*2*1000;	// = 2 jours en millisec
    			minDate.setTime(timestamp);

    			YAHOO.calendrier.cal = new YAHOO.widget.CalendarGroup("calendrier", "calendarContainer", {PAGES:2});
    			YAHOO.calendrier.cal.input = input;
    			if (input == debut) {
    				// En renseignant l'inputFin, il sera automatiquement position à J+5
    				YAHOO.calendrier.cal.inputFin = fin;
    			}
    			YAHOO.calendrier.cal.cfg.setProperty("MONTHS_SHORT",   ["Jan", "Fev", "Mar", "Avr", "Mai", "Jui", "Jui", "Aou", "Sep", "Oct", "Nov", "Dec"]); 
    			YAHOO.calendrier.cal.cfg.setProperty("MONTHS_LONG",    ["Janvier", "F&eacute;vrier", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "D&egrave;cembre"]); 
    			YAHOO.calendrier.cal.cfg.setProperty("WEEKDAYS_1CHAR", ["D", "L", "M", "M", "J", "V", "S"]); 
    			YAHOO.calendrier.cal.cfg.setProperty("WEEKDAYS_SHORT", ["Di", "Lu", "Ma", "Me", "Je", "Ve", "Sa"]); 
    			YAHOO.calendrier.cal.cfg.setProperty("WEEKDAYS_MEDIUM",["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam"]); 
    			YAHOO.calendrier.cal.cfg.setProperty("WEEKDAYS_LONG",  ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi"]); 

    			YAHOO.calendrier.cal.cfg.setProperty("selected", selected_date);
    			YAHOO.calendrier.cal.cfg.setProperty("MULTI_SELECT", true);
    			
    			YAHOO.calendrier.cal.cfg.setProperty("START_WEEKDAY", "1");	
    			YAHOO.calendrier.cal.cfg.setProperty("mindate", minDate);
    			YAHOO.calendrier.cal.cfg.setProperty("pagedate", page_date);
    			YAHOO.calendrier.cal.name = input.id;
    			
    			YAHOO.calendrier.cal.render();	
    			YAHOO.calendrier.cal.selectEvent.subscribe(handleSelect, YAHOO.calendrier.cal, true);
    			YAHOO.calendrier.cal.deselectEvent.subscribe(handleSelect, YAHOO.calendrier.cal, true);
    		};
    	}
    	if (c = $('calendarContainer')) {
    		c.addEvent('mouseleave', function() {
    			$('calendarContainer').innerHTML = '';
    		});
    	}
    }
}
window.addEvent('domready', Hotel.init);
