

(function () {
	var window = this,
			ev = window.ev,
			Date = window.Date;

	if (!ev) { throw 'mev.searchHistoryManager#<init>: Needs ev.core module!'; }
	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 criteres est définie on sort
	if (ev.meh.Criteres) {return;}

	/**
	 * Objet critere : permet de stocker les
	 * données concernant un jeu de criteres
	 */
	ev.meh.Criteres = function() {
		/*
		this.ville = "paris";
		this.chambres = 2;
		this.etoiles = 0;
		this.paxAdultes = 2;
		this.paxEnfants = 0;
		this.dateDebut = 12/06/2008;
		this.dateFin = 15/06/2008;
		 */
	};

	ev.meh.Criteres.prototype = {
		getVille: function() {
			return this.ville;
		},

		getChambres: function() {
			return this.chambres + ' chambre' + (this.chambres > 1 ? 's' : '');
		},

		getNbPaxAdultes: function() {
			return this.paxAdultes;
		},

		getPaxAdultes: function() {
			return this.paxAdultes + ' Adulte' + (this.paxAdultes > 1 ? 's' : '');
		},

		getNbPaxEnfants: function() {
			return this.paxEnfants;
		},

		getPaxEnfants: function() {
			return this.paxEnfants + ' Enfant' + (this.paxEnfants > 1 ? 's' : '');
		},

		getDateDebut: function() {
			if (typeof(this.dateDebut) != 'undefined') {
				return this.dateDebut.getDate() + ' ' + window.getMoisTexteLong(this.dateDebut.getMonth() + 1) + ' ' + this.dateDebut.getFullYear();
			}
		},

		getDateFin: function() {
			if (typeof(this.dateFin) != 'undefined') {
				return this.dateFin.getDate() + ' ' + window.getMoisTexteLong(this.dateFin.getMonth() + 1) + ' ' + this.dateFin.getFullYear();
			}
		},

		getNbChambres: function() {
			return Math.round(this.chambres);
		},

		getNbEtoiles: function() {
			return Math.round(this.etoiles);
		},

		getNbNuits: function() {
			if ((typeof(this.dateDebut) != 'undefined') && (typeof(this.dateFin) != 'undefined')) {
				return Math.round((this.dateFin.getTime() - this.dateDebut.getTime()) / 86400000);
			}
		},

		/**
		 * Injection des données critères par la syntaxe EV
		 */
		print: function() {
			ev.meh.ConstructHtmlCode().printHtmlCode_CriteresRecherche('ev_criteresRecherche', this);
		},

		/**
		 * Permet de copier les propriétés de l'objet
		 * critères donné vers l'objet courant.
		 * @param c autre objet critères.
		 */
		updateFrom: function(c) {
			ev.tools.copy(this, c);
		},

		/**
		 * Construit une chaine corespondant aux paramètres
		 * à passer dans l'url pour ces critères.
		 */
		inUrlParams: function() {
			var d = '';
			var month = '';

			var url = 'ville='+ this.ville;
			if (this.chambres) {
				url += '&chambres='+ this.chambres;
			}
			if (this.etoiles) {
				url += '&etoiles='+ this.etoiles;
			}
			if (this.paxAdultes) {
				url += '&paxAdultes='+ this.paxAdultes;
			}
			if (this.paxEnfants) {
				url += '&paxEnfants='+ this.paxEnfants;
			}
			if (this.dateDebut) {
				//url+="&dateDebut="+this.dateDebut;
				d = Date.convertStringFrToDate(this.dateDebut);
				if (d) {
					month = d.getMonth() + 1;
					if (month < 10) {
						month = '0' + month;
					}
					url += '&dateDebut='+ d.getDate() + '-'+ month + '-'+ d.getFullYear();
				}
				else {
					ev.log.error('meh.criteres#inUrlParams(): date debut non utilisable. [' + this.dateDebut + ']');
				}
			}
			if (this.dateFin) {
				//url+="&dateFin="+this.dateFin;
				d = Date.convertStringFrToDate(this.dateFin);
				if (d) {
					month = d.getMonth() + 1;
					if (month < 10) {
						month = '0' + month;
					}
					url += '&dateFin='+ d.getDate() + '-'+ month + '-'+ d.getFullYear();
				}
				else {
					ev.log.error('meh.criteres#inUrlParams(): date fin non utilisable. [' + this.dateFin + ']');
				}
			}
			if (this.crikey) {
				url += '&crikey='+ this.crikey;
			}
			return url;
		},

		/**
		 * fonction qui permet d'alimenter un objet critere à partir d'un objet XMLDocument passé en paramètre
		 * @param {Object} doc document a lire
		 *
		 * FIXME à supprimer dès que la version rjs fonctionne.
		 */
		DOMToObjet: function(doc) {
			if (doc) {
				if (doc.getElementsByTagName('ville')[0] && doc.getElementsByTagName('ville')[0].firstChild) {
					this.ville = doc.getElementsByTagName('ville')[0].firstChild.nodeValue;
				}
				if (doc.getElementsByTagName('chambres')[0] && doc.getElementsByTagName('chambres')[0].firstChild) {
					this.chambres = doc.getElementsByTagName('chambres')[0].firstChild.nodeValue;
				}
				if (doc.getElementsByTagName('etoiles')[0] && doc.getElementsByTagName('etoiles')[0].firstChild) {
					this.etoiles = doc.getElementsByTagName('etoiles')[0].firstChild.nodeValue;
				}
				if (doc.getElementsByTagName('paxAdultes')[0] && doc.getElementsByTagName('paxAdultes')[0].firstChild) {
					this.paxAdultes = doc.getElementsByTagName('paxAdultes')[0].firstChild.nodeValue;
				}
				if (doc.getElementsByTagName('paxEnfants')[0] && doc.getElementsByTagName('paxEnfants')[0].firstChild) {
					this.paxEnfants = doc.getElementsByTagName('paxEnfants')[0].firstChild.nodeValue;
				}
				if (doc.getElementsByTagName('dateDebut')[0] && doc.getElementsByTagName('dateDebut')[0].firstChild) {
					this.dateDebut = Date.convertStringFrToDate(doc.getElementsByTagName('dateDebut')[0].firstChild.nodeValue);
				}
				if (doc.getElementsByTagName('dateFin')[0] && doc.getElementsByTagName('dateFin')[0].firstChild) {
					this.dateFin = Date.convertStringFrToDate(doc.getElementsByTagName('dateFin')[0].firstChild.nodeValue);
				}
				if (doc.getElementsByTagName('crikey')[0] && doc.getElementsByTagName('crikey')[0].firstChild) {
					this.crikey = doc.getElementsByTagName('crikey')[0].firstChild.nodeValue;
				}
			} else {
				throw new Error("impossible de creer un objet critere à partir de l'objet passé en paramètre");
			}
		},

		toString: function() {
			return 'Criteres{'+ this.crikey + '}';
		}
	};

	ev.log.debug('ev/meh/criteres.js ok');
}());

