(function(){
	// Si les namespaces/classes nécessaires ne sont pas chargées : exception
	if(!window.ev){throw new Error("Le namespace 'ev' doit exister");}
	if(!ev.rjs){throw new Error("Le namespace 'ev.rjs' doit exister");}
	// On s'assure que le namespace ev.meh existe
	if(!ev.meh){ ev.meh={}; }
	//Si la classe ev.meh.Hotel est définie on sort
	if(ev.meh.Hotel){return;}

	/**
	 * Objet hotel : permet de stocker lesdonner concernant un hotel
	 * il contient une liste de propositions des partenaires
	 *
	 * @param {Object} idHotelEasy
	 * @param {Object} prixTTC
	 * @param {Object} critere
	 */
	ev.meh.Hotel=function(idHotelEasy, prixTTC, criteres){
	  	this.idHotelEasy = idHotelEasy; //id de l'hotel chez Easyvoyage
	  	this.prixTTC = prixTTC; //prix minimumTTC
	  	this.listeHotelPart = new Array(); //liste des resultats des partenaires pour l'hotel
	  	this.criteres = criteres;

		this.id;
	  	this.nom;
	  	this.description;
		this.descriptionCourte;

		this.idPartDescCourte //correspond au ID du partenaire qui a fournit la description courte
		this.coPartDescCourte //correspond au codeHotelPartenaire de l hotel qui a fournit la description courte
		this.idPartPhoto //correspond au ID du partenaire qui a fournit l icone hotel
		this.coPartPhoto //correspondau codeHotelPartenaire de l hotel qui a fournit la description courte

		this.amenagements;
	  	this.etoiles;
	  	this.urlPhoto;
	  	this.adresse;
	  	this.ville;
	  	this.pays;

	  	this.latitude;
	  	this.longitude;

		this.recupDetail = false;

		//moyenne des notes easyExpert
	  	this.noteEasyExperts;
		//moyenne generale des notes easyOpinion
	  	this.noteEasyOpinions;

		//TODO : voir si on recupere les attributs deja calculés comme actuellment ou si on recalcul les attributs a partir de la liste des opinions

		//notes easyexpert
		this.noteEEEmplacement;
		this.noteEEHebergement;
		this.noteEEPlage;
		this.noteEERestauration;

		//notes easyOpinions
		this.noteEOPInfrastructure;
		this.noteEOPLogement;
		this.noteEOPRestauration;
		this.noteEOPSituation;
		this.noteEOPSports;
		this.noteEOPAnimation;
		this.noteEOPPrix;

		//nb opinions
		this.nbNotesEOP=0;//FIXME à suppr. si pas utilisée
		this.nbOpinionsAffichees=1;
		this.firstOpinionAffichee=0;

		//liste des opinions d'easyopinions
		this.listeOpinions = new Array();
		this.listeOpinionsAffichees=new Array();

	  	this.listePhotos = new Array(); //liste des photos de l'hotel
	  	this.currentPhoto = 0;//index de la photo affichée dans le details
	  	this.listeAmenagements = new Array(); //liste des amenagements de l'hotel

		//boolean permettant de savoir si l'hotel doit etre affichï¿½ ou non en fonction des choix fait ï¿½ l'aide de la matrice
		this.displayMatrix = true;

		//boolean permettant de savoir si l'hotel doit etre affichï¿½ ou non en fonction des choix fait ï¿½ l'aide des filtres
		this.displayFilters = true;

		this.getId=function(){
			if (this.id != null) {
				return this.id;
			}
			return null;
		}

		this.getNom=function(){
			//si le nom fait plus de 48 caractere alors on le tronque sinon cela entraine un pb de mise en page
			if(this.nom.length > 48){
				return this.nom.substr(0, 45)+"...";
			}
			return this.nom;
		}

		this.getEtoiles=function(){
			if (this.etoiles != null) {
				return this.etoiles;
			}
			return 0;
		}

		this.getUrlPhoto=function(){
			if (this.urlPhoto != null) {
				return this.urlPhoto;
			}
			return(ev.me.URL_IMAGE+"/base/imgs/me3/meh/no_photo.gif");
		}

		this.getAdresse=function(){
			if (this.adresse != null) {
				return this.adresse;
			}
			return "n.c";
		}

		this.getVille=function(){
			if (this.ville != null) {
				return this.ville;
			}
			return "n.c";
		}

		this.getPays=function(){
			if (this.pays != null) {
				return this.pays;
			}
			return "n.c";
		}

		this.hasRecupDetail=function(){
			return this.recupDetail;
		}

		this.hasNoteEOP=function(){
			if (this.noteEasyOpinions == -1 || this.noteEasyOpinions == -1.0 || this.noteEasyOpinions == 0 || this.noteEasyOpinions == 0.0 || typeof(this.noteEasyOpinions) == "undefined") {
				return false;
			}
			return true;
		}

		this.hasNoteEE=function(){
			if(this.noteEasyExperts == '-1' || this.noteEasyExperts == '-1.0'  || this.noteEasyExperts == '0' || this.noteEasyExperts == '0.0' || typeof(this.noteEasyExperts) == "undefined" ){
					return false;
			}
			return true;
		}

		this.getNbOffres=function(){
			return this.listeHotelPart.length;
		}

		this.getFirstPhoto=function(){
			if(this.listePhotos.length > 0){
				return this.listePhotos[0];
			}
			return(ev.me.URL_IMAGE+"/base/imgs/me3/meh/no_photo.gif");
		}

		this.getDescription=function(){
			if (this.description != null) {
				//var reg = new RegExp("<.[^<>]*>", "gi");//on supprime toutes les balises pour ne pas avoir de problème de mise en forme si on tronque le texte
				//var reg2 = new RegExp("&lt;[^&]*&gt;", "gi" );

				//return this.description.replace(reg, "").replace(reg2," ");
				return this.description;
			}
			else {
				return "";
			}
		}

		this.getDescriptionCourte=function(){
			if(this.descriptionCourte != null){
				return this.descriptionCourte;
			}
			else {
				return "";
			}
		}

		this.getIdPartDescCourte = function(){
			if(this.idPartDescCourte != null){
				return this.idPartDescCourte;
			}
			else {
				return 0;
			}
		}

		this.getAmenagements=function(){
			if (this.getAmenagements != null) {
				return this.amenagements;
			}else{
				return "";
			}
		}

		this.getDistance=function(){
	    if(this.longitude == 0.0 || this.latitude == 0.0 || this.longitude == "Infinity" || this.latitude == "Infinity"
	         || typeof(this.longitude) == "undefined" || typeof(this.latitude) == "undefined" ){//dans ce cas on a filtré un cellule avec des elements dont la distance est inconue
	        return "n.c";
	    }
			if(ville.poiReference!=null && typeof(ville.poiReference) != "undefined"){
				return ville.poiReference.distanceFrom(this.latitude, this.longitude);
			}
			return "n.c";
		}

		this.getNoteEOP=function(){
			if(this.hasNoteEOP() == true){
				return this.noteEasyOpinions;
			}
			return "-";
		}

		this.getNoteEE=function(){
			if(this.hasNoteEE() == true){
				return this.noteEasyExperts;
			}
			return "-";
		}

		this.getNbSmileysEOP=function(){
			if(this.hasNoteEOP() == false){
				return 0;
			}
			if (this.noteEasyOpinions < 3){
				return 1;
			}else if (this.noteEasyOpinions < 8){
				return 2;
	    }else if (this.noteEasyOpinions < 13){
				return 3;
	    }else if (this.noteEasyOpinions < 18){
				return 4;
	    }else{
				return 5;
	    }
		}

		this.getIdPartMinPrix=function(){
			if(this.listeHotelPart[0] != undefined){
				return this.listeHotelPart[0].idPart;
			}
			return 0;
		}

		this.getNomPartMinPrix=function(){
			if (this.listeHotelPart[0] != undefined) {
				return this.listeHotelPart[0].nomPart;
			}
		}

		/**
		 * Permet de connaï¿½tre le nombre de propositions
		 * partenaires de l'hotel.
		 *
		 * @return un entier positif (0 si aucune proposition)
		 */
		this.getNbPropositionsPartenaires = function(){
			return this.listeHotelPart.length;
		}

		/**
		 * Permet de retrouver une proposition dans la liste de
		 * propositions partenaires de l'hotel.
		 *
		 * @param {Object} _idProposition : identifiant numï¿½rique de la proposition partenaire recherchï¿½e
		 * @return une proposition d'hotel partenaire (ou null si non trouvï¿½e)
		 */
		this.findPropositionPartenaireById = function(_idProposition){
			// on parcourt la liste de propositions pour rï¿½cupï¿½rer celle que l'on recherche
			for(var i=0; i<this.listeHotelPart.length; i++){
				if(this.listeHotelPart[i].id==_idProposition){
					// on renvoie l'objet Hotel trouvé
					return this.listeHotelPart[i];
				}
			}
			return null;
		}

		this.getNbPhotos = function(){
			return this.listePhotos.length;
		}

		this.getLongitude = function(){
			return this.longitude;
		}

		this.getLatitude = function(){
			return this.latitude;
		}

		this.getNoteEEEmplacement=function(){
			if(this.noteEEEmplacement == -1 || this.noteEEEmplacement == -1.0 || this.noteEEEmplacement == 0 || this.noteEEEmplacement == 0.0 || this.noteEEEmplacement == ""){
				return "-";
			}else{
				return this.noteEEEmplacement;
			}
		}

		this.getNoteEEHebergement=function(){
			if(this.noteEEHebergement == -1 || this.noteEEHebergement == -1.0 || this.noteEEHebergement == 0 || this.noteEEHebergement == 0.0 || this.noteEEHebergement == ""){
				return "-";
			}else{
				return this.noteEEHebergement;
			}
		}

		this.getNoteEEPlage=function(){
			if(this.noteEEPlage == -1 || this.noteEEPlage == -1.0 || this.noteEEPlage == 0 || this.noteEEPlage == 0.0 || this.noteEEPlage == ""){
				return "-";
			}else{
				return this.noteEEPlage;
			}
		}

		this.getNoteEERestauration=function(){
			if(this.noteEERestauration == -1 || this.noteEERestauration == -1.0 || this.noteEERestauration == 0 || this.noteEERestauration == 0.0 || this.noteEERestauration == ""){
				return "-";
			}else{
				return this.noteEERestauration;
			}
		}

		this.getNoteEOPInfrastructure=function(){
			if(this.noteEOPInfrastructure == -1 || this.noteEOPInfrastructure == -1.0 || this.noteEOPInfrastructure == 0 || this.noteEOPInfrastructure == 0.0 || this.noteEOPInfrastructure == ""){
				return "-";
			}else{
				return this.noteEOPInfrastructure;
			}
		}

		this.getNoteEOPLogement=function(){
			if(this.noteEOPLogement == -1 || this.noteEOPLogement == -1.0 || this.noteEOPLogement == 0 || this.noteEOPLogement == 0.0 || this.noteEOPLogement == ""){
				return "-";
			}else{
				return this.noteEOPLogement;
			}
		}

		this.getNoteEOPRestauration=function(){
			if(this.noteEOPRestauration == -1 || this.noteEOPRestauration == -1.0 || this.noteEOPRestauration == 0 || this.noteEOPRestauration == 0.0 || this.noteEOPRestauration == ""){
				return "-";
			}else{
				return this.noteEOPRestauration;
			}
		}

		this.getNoteEOPSituation=function(){
			if(this.noteEOPSituation == -1 || this.noteEOPSituation == -1.0 || this.noteEOPSituation == 0 || this.noteEOPSituation == 0.0 || this.noteEOPSituation == ""){
				return "-";
			}else{
				return this.noteEOPSituation;
			}
		}

		this.getNoteEOPSports=function(){
			if(this.noteEOPSports == -1 || this.noteEOPSports == -1.0 || this.noteEOPSports == 0 || this.noteEOPSports == 0.0 || this.noteEOPSports == ""){
				return "-";
			}else{
				return this.noteEOPSports;
			}
		}

		this.getNoteEOPAnimation=function(){
			if(this.noteEOPAnimation == -1 || this.noteEOPAnimation == -1.0 || this.noteEOPAnimation == 0 || this.noteEOPAnimation == 0.0 || this.noteEOPAnimation == ""){
				return "-";
			}else{
				return this.noteEOPAnimation;
			}
		}

		this.getNoteEOPPrix=function(){
			if(this.noteEOPPrix == -1 || this.noteEOPPrix == -1.0 || this.noteEOPPrix == 0 || this.noteEOPPrix == 0.0 || this.noteEOPPrix == ""){
				return "-";
			}else{
				return this.noteEOPPrix;
			}
		}

		this.getNbOpinions=function(){
			return this.nbNotesEOP;
		}

		this.getFirstOpinionAffichee=function(){
			if(this.nbNotesEOP == 0) return 0;
			return this.firstOpinionAffichee+1;
		}

		this.getMsgAucuneOpinion=function(){
			if(this.nbNotesEOP == 0) {

				if(window.lang=="es_ES"){
					return "No hay opiniones sobre este hotel.";
				}
				if(window.lang=="it_IT"){
					return "Non ci sono opinioni per questo hotel";
				}
				if(window.lang=="en_GB"){
					return "There is no comment on this hotel";
				}
				if(window.lang=="de_DE"){
					return "Für dieses Hotel gibt es noch keine Bewertung";
				}
				else {
					return "Il n'y a pas d'opinion pour cet hôtel";
				}
			}
			return "";
		}



		this.getMsgLocalisationNC=function(){
			if(typeof(ville.poiReference) == "undefined"
					 ||	this.longitude == 0.0  || this.latitude == 0.0
					 || this.longitude == "Infinity" || this.latitude == "Infinity"
	         || typeof(this.longitude) == "undefined" || typeof(this.latitude) == "undefined" ){//dans ce cas on a filtré un cellule avec des elements dont la distance est inconue
	        return "Nous ne sommes pas en mesure de localiser cet hôtel, veuillez nous en excuser.";
		  }
			return "";
		}

		/**** GETTERS AMENAGEMENTS ****/
		this.getPiscine=function(){
			if (this.listeAmenagements != null && this.listeAmenagements.length > 0) {
				for (i = 0; i < this.listeAmenagements.length; i++) {
					if (this.listeAmenagements[i] == 1) {
						return "ON";
					}
				}
			}
			return "OFF";
		}

		this.getRestaurant=function(){
			if (this.listeAmenagements != null && this.listeAmenagements.length > 0) {
				for (i = 0; i < this.listeAmenagements.length; i++) {
					if (this.listeAmenagements[i] == 110) {
						return "ON";
					}
				}
			}
			return "OFF";
		}

		this.getAccesInternet=function(){
			if (this.listeAmenagements != null && this.listeAmenagements.length > 0) {
				for (i = 0; i < this.listeAmenagements.length; i++) {
					if (this.listeAmenagements[i] == 15) {
						return "ON";
					}
				}
			}
			return "OFF";
		}

		this.getClimatisation=function(){
			if (this.listeAmenagements != null && this.listeAmenagements.length > 0) {
				for (i = 0; i < this.listeAmenagements.length; i++) {
					if (this.listeAmenagements[i] == 111) {
						return "ON";
					}
				}
			}
			return "OFF";
		}

		this.getParking=function(){
			if (this.listeAmenagements != null && this.listeAmenagements.length > 0) {
				for (i = 0; i < this.listeAmenagements.length; i++) {
					if (this.listeAmenagements[i] == 94) {
						return "ON";
					}
				}
			}
			return "OFF";
		}

		this.getAccesHandicapes=function(){
			if (this.listeAmenagements != null && this.listeAmenagements.length > 0) {
				for (i = 0; i < this.listeAmenagements.length; i++) {
					if (this.listeAmenagements[i] == 46) {
						return "ON";
					}
				}
			}
			return "OFF";
		}

		this.getSport=function(){
			listeSports = new Array(112,5,9,27,28,29,31,32,33,34,35,36,38,39,43,44,45,63,65,66,67,68,80,83,87,92,99,101,106,107,108); //liste de tous les sports
			if (this.listeAmenagements != null && this.listeAmenagements.length > 0) {
				for (var i = 0; i < this.listeAmenagements.length; ++i) {
					for (var j=0; j < listeSports.length; ++j){
						if (this.listeAmenagements[i] == listeSports[j]) {
							return "ON";
						}
					}
				}
			}
			return "OFF";
		}

		this.getEspaceBienEtre=function(){
			if (this.listeAmenagements != null && this.listeAmenagements.length > 0) {
				for (i = 0; i < this.listeAmenagements.length; i++) {
					if (this.listeAmenagements[i] == 113 || this.listeAmenagements[i] == 13 || this.listeAmenagements[i] == 14 || this.listeAmenagements[i] == 18 ||
						this.listeAmenagements[i] == 19 || this.listeAmenagements[i] == 20 || this.listeAmenagements[i] == 49 || this.listeAmenagements[i] == 56 ||
						this.listeAmenagements[i] == 76 || this.listeAmenagements[i] == 90 || this.listeAmenagements[i] == 103 || this.listeAmenagements[i] == 74) {
						return "ON";
					}
				}
			}
			return "OFF";
		}

		this.getNavette=function(){
			if (this.listeAmenagements != null && this.listeAmenagements.length > 0) {
				for (i = 0; i < this.listeAmenagements.length; i++) {
					if (this.listeAmenagements[i] == 64 || this.listeAmenagements[i] == 98) {
						return "ON";
					}
				}
			}
			return "OFF";
		}

		this.getAnimauxAcceptes=function(){
			if (this.listeAmenagements != null && this.listeAmenagements.length > 0) {
				for (i = 0; i < this.listeAmenagements.length; i++) {
					if (this.listeAmenagements[i] == 61) {
						return "ON";
					}
				}
			}
			return "OFF";
		}


		/**** GETTER LISTE AMENAGEMENTS ****/
		this.getAmenagements=function(){
			if (this.listeAmenagements != null && this.listeAmenagements.length > 0) {
				var results="id amgts = "
				for(i=0;i<this.listeAmenagements.length;i++){
					results+=this.listeAmenagements[i]+", ";
				}
				return "  --> "+results;
			}
			return "  --> "+false;
		}

	}

	ev.meh.Hotel.prototype={
		/**
		 * fonction utilisée pour permettre au filtre de prix de comparaer le prix de l'objet avec les valeurs filtrées
		 */
		getPrix: function(){
			return Math.round(this.prixTTC);
		},

		getPrix1Nuit: function(){//retourne le prix moyen par chambre et par nuit
			return Math.round(this.prixTTC/this.criteres.getNbChambres()/this.criteres.getNbNuits());
		},

		/**
		 * Retourne toutes les offres des partenaires.
		 */
		getPropositionsPartenaires: function(_index){
			return this.listeHotelPart[_index-1];
			//return this.listeHotelPart;
		},

		/**
		 * Retourne les offres des partenaires
		 */
		getPropositionsSummary: function(_index){
			return this.listeHotelPart[_index];
		},

		/**
		 * Initialise les opinions actuellement affichees.
		 */
		initOpinionsAffichees: function(){
			this.listeOpinionsAffichees=new Array();
			var nbOpinions = this.listeOpinions.length;
			if(nbOpinions==0) return;
			for(var i=0; i<this.nbOpinionsAffichees; ++i){
				var index=this.firstOpinionAffichee+i;
				index= index % nbOpinions;
				this.listeOpinionsAffichees.push(this.listeOpinions[index]);
				//log("index="+index, 'error');
			}
		},

		getOpinionsAffichees: function(_index){
			return this.listeOpinionsAffichees[_index];
		},

		toString: function(){
			return 'Hotel{' + this.idHotelEasy + " - " + this.nom + ", best=" + this.prixTTC + ' EUR}';
		}
	}

	/**
	 * Constantes du comparateur d'hotels
	 */
	ev.meh.Hotel.SORT_PRICE = 1;
	ev.meh.Hotel.SORT_DISTANCE = 2;
	ev.meh.Hotel.SORT_NAME = 3;
	ev.meh.Hotel.SORT_CATEGORY = 4;
	ev.meh.Hotel.SORT_EXPERTS = 5;
	ev.meh.Hotel.SORT_OPINIONS = 6;

	ev.meh.Hotel.SORT_ASC = 1;
	ev.meh.Hotel.SORT_DESC = 2;

	/**
	 * Paramètres du comparateur d'hotels
	 */
	ev.meh.Hotel.sortType = ev.meh.Hotel.SORT_PRICE;
	ev.meh.Hotel.sortWay = ev.meh.Hotel.SORT_ASC;

	/**
	 * Variables du sens de tri des colonnes
	 */
	ev.meh.Hotel.sortWayName = ev.meh.Hotel.SORT_DESC;
	ev.meh.Hotel.sortWayDistance = ev.meh.Hotel.SORT_DESC;
	ev.meh.Hotel.sortWayCategory = ev.meh.Hotel.SORT_DESC;
	ev.meh.Hotel.sortWayPrice = ev.meh.Hotel.SORT_DESC;
	ev.meh.Hotel.sortWayExperts = ev.meh.Hotel.SORT_ASC;
	ev.meh.Hotel.sortWayOpinions = ev.meh.Hotel.SORT_ASC;

	/**
	 * Comparateur d'hotels
	 *
	 * TODO: isoler les tests sur le type type de comparaison et le sens de tri
	 *
	 * @param {Object} hotel1
	 * @param {Object} hotel2
	 */
	ev.meh.Hotel.compare = function(hotel1, hotel2) {
		switch (ev.meh.Hotel.sortType) {
			case ev.meh.Hotel.SORT_PRICE:
				return ev.meh.Hotel.sortWay == ev.meh.Hotel.SORT_ASC ? hotel1.prixTTC - hotel2.prixTTC : hotel2.prixTTC - hotel1.prixTTC;

			case ev.meh.Hotel.SORT_DISTANCE:
				return ev.meh.Hotel.sortWay == ev.meh.Hotel.SORT_ASC ?
					(ville.poiReference.distanceFrom(hotel1.latitude, hotel1.longitude) - ville.poiReference.distanceFrom(hotel2.latitude, hotel2.longitude)):
					(ville.poiReference.distanceFrom(hotel2.latitude, hotel2.longitude) - ville.poiReference.distanceFrom(hotel1.latitude, hotel1.longitude));

			case ev.meh.Hotel.SORT_NAME:
				if (hotel1.nom > hotel2.nom)
					return ev.meh.Hotel.sortWay == ev.meh.Hotel.SORT_ASC ? 1 : -1;
				if (hotel1.nom < hotel2.nom)
					return ev.meh.Hotel.sortWay == ev.meh.Hotel.SORT_ASC ? -1 : 1;
				return 0;

			case ev.meh.Hotel.SORT_CATEGORY:
				return ev.meh.Hotel.sortWay == ev.meh.Hotel.SORT_ASC ? hotel1.etoiles - hotel2.etoiles : hotel2.etoiles - hotel1.etoiles;

			case ev.meh.Hotel.SORT_EXPERTS:
				return ev.meh.Hotel.sortWay == ev.meh.Hotel.SORT_ASC ? hotel1.noteEasyExperts - hotel2.noteEasyExperts : hotel2.noteEasyExperts - hotel1.noteEasyExperts;

			case ev.meh.Hotel.SORT_OPINIONS:
				return ev.meh.Hotel.sortWay == ev.meh.Hotel.SORT_ASC ? hotel1.noteEasyOpinions - hotel2.noteEasyOpinions : hotel2.noteEasyOpinions - hotel1.noteEasyOpinions;

			default : return 0;
		}
	}

	//fonction permettant la mise à jour des infos tels que le prix min par etoiles...
	//cette focntion est appelée une seule fois apres le "premier" adffichage des resultats lorsque tous les resultats ont ï¿½tï¿½ recupï¿½rï¿½s
	//TODO : il faut peut etre modifier le mode de calcul car ici on parcourt l'ensemble des resultats, il serait peut etre plus judicieux d'effectuer ce traitement ailleurs
	// index du premier resultat affiche (selon la page sur laquelle on se trouve)
	var indexAffichage=0;
	// nombre maximal de resultats affiches par page
	var nbAffichage=10;
	// variable permettant de stocker le nb total d'hotel presentï¿½ ï¿½ l'internaute (en fct des filtrages effectuï¿½s)
	var iAff=0;
	//la liste suivante va permettre de stocker le prix le moins cher chez chaque partenaire
	// tableau associatif de la forme clï¿½ = "id_"+idpartenaire ; valeur = prix le moins cher
	var listePartenairesPrixMin = new Array();

})();
