

/**
 * Closure ^^ !
 */
(function() {
	var window = this,
			snf_getBorneInfFiltered = window.snf_getBorneInfFiltered,
			snf_filtrer = window.snf_filtrer,
			choisirInterval = window.choisirInterval,
			choisir = window.choisir,
			choisirTout = window.choisirTout,
			snf_refresh = window.snf_refresh;

	// Initialisation des tableaux de filtrage
	window.arrayCOMPAGNIES = [];
	window.arrayESCALES = [];
	window.arrayAEROPORTS_DEPARTALLER = [];
	window.arrayAEROPORTS_ARRIVEEALLER = [];
	window.arrayAEROPORTS_DEPARTRETOUR = [];
	window.arrayAEROPORTS_ARRIVEERETOUR = [];
	window.arrayHEURES_DEPARTALLER = [];
	window.arrayHEURES_ARRIVEEALLER = [];
	window.arrayHEURES_DEPARTRETOUR = [];
	window.arrayHEURES_ARRIVEERETOUR = [];
	window.arrayDUREES_ALLER = [];
	window.arrayDUREES_RETOUR = [];

	// DEBUG : TODO : remove
	Array.toString = function() {
		var txt = 'Array{ length=' + this.length, i;
		for (i in this) {
			if (this.hasOwnProperty(i)) {
				txt += ', ' + i + '=' + this[i];
			}
		}
		return txt + ' }';
	};

	/**
	 * @constructor de vols
	 */
	window.Vol = function(id,direct,prixTTC,dateDepartAller,dateArriveeRetour,compagnie,
			IATADepartAller,IATAArriveeAller,IATADepartRetour,IATAArriveeRetour,
			heureDepartAller,heureArriveeAller,heureDepartRetour,heureArriveeRetour,
			dureeAller,dureeRetour,partenaire,nomPartenaire,link) {
		this.id = id;
		this.link = link;

		this.partenaire = partenaire;
		this.nomPartenaire = nomPartenaire;

		this.direct = direct;
		this.prixTTC = prixTTC;

		this.compagnie = compagnie;// Contient un code IATA

		this.dateDepartAller = dateDepartAller;// Contient le timestamp de la date et l'heure du depart aller
		this.dateArriveeRetour = dateArriveeRetour;// Contient le timestamp de la date et l'heure du depart retour

		this.IATADepartAller = IATADepartAller;// Contient le code IATA de l'aeroport de depart (vol aller)
		this.IATAArriveeAller = IATAArriveeAller;// Contient le code IATA de l'aeroport d'arrivee (vol aller)
		this.IATADepartRetour = IATADepartRetour;// Contient le code IATA de l'aeroport de depart (vol retour)
		this.IATAArriveeRetour = IATAArriveeRetour;// Contient le code IATA de l'aeroport d'arrivee (vol retour)

		this.heureDepartAller = heureDepartAller;// Contient le timestamp de l'heure de depart (vol aller)
		this.heureArriveeAller = heureArriveeAller;// Contient le timestamp de l'heure d'arrivee (vol aller)
		this.heureDepartRetour = heureDepartRetour;// Contient le timestamp de l'heure de depart (vol retour)
		this.heureArriveeRetour = heureArriveeRetour;// Contient le timestamp de l'heure d'arrivee (vol retour)

		this.dureeAller = dureeAller;// Contient le timestamp de la duree du vol aller
		this.dureeRetour = dureeRetour;// Contient le timestamp de la duree du vol retour
	};

	window.Vol.prototype.toString = function() {
		return this.id;
	};

	// Doit on ou non afficher un vol
	window.Vol.prototype.canDisplay = function() {
		if (snf_getBorneInfFiltered(window.arrayPRIX, this.prixTTC)) {
			return false;
		}
		if (window.arrayPARTENAIRES[this.partenaire]) {
			return false;
		}

		if (window.arrayCOMPAGNIES[this.compagnie]) {
			return false;
		}
		if (window.arrayESCALES[this.direct]) {
			return false;
		}

		if (window.arrayAEROPORTS_DEPARTALLER[this.IATADepartAller]) {
			return false;
		}
		if (window.arrayAEROPORTS_ARRIVEEALLER[this.IATAArriveeAller]) {
			return false;
		}
		if (window.arrayAEROPORTS_DEPARTRETOUR[this.IATADepartRetour]) {
			return false;
		}
		if (window.arrayAEROPORTS_ARRIVEERETOUR[this.IATAArriveeRetour]) {
			return false;
		}

		if (snf_getBorneInfFiltered(window.arrayHEURES_DEPARTALLER, this.heureDepartAller)) {
			return false;
		}
		if (snf_getBorneInfFiltered(window.arrayHEURES_ARRIVEEALLER, this.heureArriveeAller)) {
			return false;
		}
		if (snf_getBorneInfFiltered(window.arrayHEURES_DEPARTRETOUR, this.heureDepartRetour)) {
			return false;
		}
		if (snf_getBorneInfFiltered(window.arrayHEURES_ARRIVEERETOUR, this.heureArriveeRetour)) {
			return false;
		}

		if (snf_getBorneInfFiltered(window.arrayDUREES_ALLER, this.dureeAller)) {
			return false;
		}
		if (snf_getBorneInfFiltered(window.arrayDUREES_RETOUR, this.dureeRetour)) {
			return false;
		}

		return true;
	};

	//alert(snf_getBorneInfFiltered(window.arrayPRIX,this.prixTTC));
	//alert(window.arrayPARTENAIRES[this.partenaire]);
	//alert(window.arrayCOMPAGNIES[this.compagnie]);
	//alert(window.arrayESCALES[this.direct]);


	// Tris
	window.SORT_PRIXTTC = 0;
	window.SORT_COMPAGNIE = 1;
	window.SORT_DIRECT = 2;
	window.SORT_DATEALLER = 3;
	window.SORT_DATERETOUR = 4;
	window.SORT_AEROPORTDEPARTALLER = 5;
	window.SORT_AEROPORTARRIVEEALLER = 6;
	window.SORT_AEROPORTDEPARTRETOUR = 7;
	window.SORT_AEROPORTARRIVEERETOUR = 8;
	window.SORT_HEUREDEPARTALLER = 9;
	window.SORT_HEUREARRIVEEALLER = 10;
	window.SORT_HEUREDEPARTRETOUR = 11;
	window.SORT_HEUREARRIVEERETOUR = 12;
	window.SORT_DUREEALLER = 13;
	window.SORT_DUREERETOUR = 14;
	window.SORT_PARTENAIRE = 15;

	var sortType = -1, sortWay = 'asc';

	// Fonction de comparaison entre deux vols (utilise le flag sortType)
	function compareVols(vol1,vol2) {
		if (sortType == window.SORT_COMPAGNIE) {
			if (vol1.compagnie < vol2.compagnie) {
				return -1;
			}
			if (vol1.compagnie > vol2.compagnie) {
				return 1;
			}
			return 0;
		}
		if (sortType == window.SORT_DIRECT) {
			return vol1.direct - vol2.direct;
		}
		if (sortType == window.SORT_DATEALLER) {
			return vol1.dateDepartAller - vol2.dateDepartAller;
		}
		if (sortType == window.SORT_DATERETOUR) {
			return vol1.dateArriveeRetour - vol2.dateArriveeRetour;
		}

		if (sortType == window.SORT_PARTENAIRE) {
			return vol1.partenaire - vol2.partenaire;
		}

		if (sortType == window.SORT_AEROPORTDEPARTALLER) {
			if (vol1.IATADepartAller < vol2.IATADepartAller) {
				return -1;
			}
			if (vol1.IATADepartAller > vol2.IATADepartAller) {
				return 1;
			}
			return 0;
		}
		if (sortType == window.SORT_AEROPORTARRIVEEALLER) {
			if (vol1.IATAArriveeAller < vol2.IATAArriveeAller) {
				return -1;
			}
			if (vol1.IATAArriveeAller > vol2.IATAArriveeAller) {
				return 1;
			}
			return 0;
		}
		if (sortType == window.SORT_AEROPORTDEPARTRETOUR) {
			if (vol1.IATADepartRetour < vol2.IATADepartRetour) {
				return -1;
			}
			if (vol1.IATADepartRetour > vol2.IATADepartRetour) {
				return 1;
			}
			return 0;
		}
		if (sortType == window.SORT_AEROPORTARRIVEERETOUR) {
			if (vol1.IATAArriveeRetour < vol2.IATAArriveeRetour) {
				return -1;
			}
			if (vol1.IATAArriveeRetour > vol2.IATAArriveeRetour) {
				return 1;
			}
			return 0;
		}

		if (sortType == window.SORT_HEUREDEPARTALLER) {
			return vol1.heureDepartAller - vol2.heureDepartAller;
		}
		if (sortType == window.SORT_HEUREARRIVEEALLER) {
			return vol1.heureArriveeAller - vol2.heureArriveeAller;
		}
		if (sortType == window.SORT_HEUREDEPARTRETOUR) {
			return vol1.heureDepartRetour - vol2.heureDepartRetour;
		}
		if (sortType == window.SORT_HEUREARRIVEERETOUR) {
			return vol1.heureArriveeRetour - vol2.heureArriveeRetour;
		}

		if (sortType == window.SORT_DUREEALLER) {
			return vol1.dureeAller - vol2.dureeAller;
		}
		if (sortType == window.SORT_DUREERETOUR) {
			return vol1.dureeRetour - vol2.dureeRetour;
		}

		return vol1.prixTTC - vol2.prixTTC;// Par defaut tri prix
	}

	// Fonction de tri generique en fonction du type de tri demande
	function sortVols(type,way) {
		if (!way) {// si le parametre way manque
			if (sortType == type) {
				// si le type etait identique avant, on change le sens de tri
				sortWay = (sortWay === 'asc') ? 'desc' : 'asc';
			} else {
				sortWay = 'asc';// sinon on effectue un tri ascendant
				sortType = type;// avec le nouveau type de tri
			}
		}else {// si le parametre way est fourni
			sortType = type;
			sortWay = way;
		}
		window.array.sort(compareVols);
		if (sortWay == 'desc') {
			window.array.reverse();// si le tri est descendant, on renverse le tableau de vols
		}
		snf_refresh();
	}

	/*********************************/
	/****** Fonctions publiques ******/
	/*********************************/
	window.sortVols = sortVols;
	// Tous les tris possibles
	window.sortVolsPrixTTC = function(way) { sortVols(window.SORT_PRIXTTC, way); };
	window.sortVolsCompagnie = function(way) { sortVols(window.SORT_COMPAGNIE, way); };
	window.sortVolsDirect = function(way) { sortVols(window.SORT_DIRECT, way); };
	window.sortVolsDateAller = function(way) { sortVols(window.SORT_DATEALLER, way); };
	window.sortVolsDateRetour = function(way) { sortVols(window.SORT_DATERETOUR, way); };
	window.sortVolsAeroportDepartAller = function(way) { sortVols(window.SORT_AEROPORTDEPARTALLER, way); };
	window.sortVolsAeroportArriveeAller = function(way) { sortVols(window.SORT_AEROPORTARRIVEEALLER, way); };
	window.sortVolsAeroportDepartRetour = function(way) { sortVols(window.SORT_AEROPORTDEPARTRETOUR, way); };
	window.sortVolsAeroportArriveeRetour = function(way) { sortVols(window.SORT_AEROPORTARRIVEERETOUR, way); };
	window.sortVolsHeureDepartAller = function(way) { sortVols(window.SORT_HEUREDEPARTALLER, way); };
	window.sortVolsHeureArriveeAller = function(way) { sortVols(window.SORT_HEUREARRIVEEALLER, way); };
	window.sortVolsHeureDepartRetour = function(way) { sortVols(window.SORT_HEUREDEPARTRETOUR, way); };
	window.sortVolsHeureArriveeRetour = function(way) { sortVols(window.SORT_HEUREARRIVEERETOUR, way); };
	window.sortVolsDureeAller = function(way) { sortVols(window.SORT_DUREEALLER, way); };
	window.sortVolsDureeRetour = function(way) { sortVols(window.SORT_DUREERETOUR, way); };
	window.sortVolsPartenaire = function(way) { sortVols(window.SORT_PARTENAIRE, way); };

	// Comment agir sur les fitres a partir d'un affichage different (sans checkbox) ?
	// - Il suffit de faire appel aux fonctions suivantes :
	window.setAffichagePrixTTC = function(value,affiche,refresh) { snf_filtrer(window.arrayPRIX, value, affiche, refresh); };
	window.setAffichageEscales = function(value,affiche,refresh) { snf_filtrer(window.arrayESCALES, value, affiche, refresh); };
	window.setAffichageCompagnie = function(value,affiche,refresh) { snf_filtrer(window.arrayCOMPAGNIES, value, affiche, refresh); };

	window.setAffichageAeroportDepartAller = function(value,affiche,refresh) { snf_filtrer(window.arrayAEROPORTS_DEPARTALLER, value, affiche, refresh); };
	window.setAffichageAeroportArriveeAller = function(value,affiche,refresh) { snf_filtrer(window.arrayAEROPORTS_ARRIVEEALLER, value, affiche, refresh); };
	window.setAffichageAeroportDepartRetour = function(value,affiche,refresh) { snf_filtrer(window.arrayAEROPORTS_DEPARTRETOUR, value, affiche, refresh); };
	window.setAffichageAeroportArriveeRetour = function(value,affiche,refresh) { snf_filtrer(window.arrayAEROPORTS_ARRIVEERETOUR, value, affiche, refresh); };

	window.setAffichageHeureDepartAller = function(value,affiche,refresh) { snf_filtrer(window.arrayHEURES_DEPARTALLER, value, affiche, refresh); };
	window.setAffichageHeureArriveeAller = function(value,affiche,refresh) { snf_filtrer(window.arrayHEURES_ARRIVEEALLER, value, affiche, refresh); };
	window.setAffichageHeureDepartRetour = function(value,affiche,refresh) { snf_filtrer(window.arrayHEURES_DEPARTRETOUR, value, affiche, refresh); };
	window.setAffichageHeureArriveeRetour = function(value,affiche,refresh) { snf_filtrer(window.arrayHEURES_ARRIVEERETOUR, value, affiche, refresh); };

	window.choisiPrix = function(borneInf,borneSup,refresh) { choisirInterval(window.arrayPRIX, borneInf, borneSup, false, refresh); };
	window.choisiDirect = function(refresh) { choisir(window.arrayESCALES, '1', refresh); };
	window.choisiEscales = function(refresh) { choisir(window.arrayESCALES, '2', refresh); };
	window.choisiCompagnie = function(iata,refresh) { choisir(window.arrayCOMPAGNIES, iata, refresh); };

	window.choisiAeroportDepartAller = function(iata,refresh) { choisir(window.arrayAEROPORTS_DEPARTALLER, iata, refresh); };
	window.choisiAeroportArriveeAller = function(iata,refresh) { choisir(window.arrayAEROPORTS_ARRIVEEALLER, iata, refresh); };
	window.choisiAeroportDepartRetour = function(iata,refresh) { choisir(window.arrayAEROPORTS_DEPARTRETOUR, iata, refresh); };
	window.choisiAeroportArriveeRetour = function(iata,refresh) { choisir(window.arrayAEROPORTS_ARRIVEERETOUR, iata, refresh); };

	function choisiHeuresDepartAller(borneInf,borneSup,refresh) { choisirInterval(window.arrayHEURES_DEPARTALLER, borneInf, borneSup, false, refresh); }
	window.choisiHeuresDepartAller = choisiHeuresDepartAller;
	window.choisiHeuresDepartAllerMatin = function() { choisiHeuresDepartAller(5, 12); };
	window.choisiHeuresDepartAllerApresMidi = function() { choisiHeuresDepartAller(12, 18); };
	window.choisiHeuresDepartAllerSoir = function() { choisiHeuresDepartAller(18, 24); };

	function choisiHeuresArriveeAller(borneInf,borneSup,refresh) { choisirInterval(window.arrayAEROPORTS_ARRIVEEALLER, borneInf, borneSup, false, refresh); }
	window.choisiHeuresArriveeAller = choisiHeuresArriveeAller;
	window.choisiHeuresArriveeAllerMatin = function() { choisiHeuresArriveeAller(5, 12); };
	window.choisiHeuresArriveeAllerApresMidi = function() { choisiHeuresArriveeAller(12, 18); };
	window.choisiHeuresArriveeAllerSoir = function() { choisiHeuresArriveeAller(18, 24); };

	function choisiHeuresDepartRetour(borneInf,borneSup,refresh) { choisirInterval(window.arrayHEURES_DEPARTRETOUR, borneInf, borneSup, false, refresh); }
	window.choisiHeuresDepartRetour = choisiHeuresDepartRetour;
	window.choisiHeuresDepartRetourMatin = function() { choisiHeuresDepartRetour(5, 12); };
	window.choisiHeuresDepartRetourApresMidi = function() { choisiHeuresDepartRetour(12, 18); };
	window.choisiHeuresDepartRetourSoir = function() { choisiHeuresDepartRetour(18, 24); };

	function choisiHeuresArriveeRetour(borneInf,borneSup,refresh) { choisirInterval(window.arrayHEURES_ARRIVEERETOUR, borneInf, borneSup, false, refresh); }
	window.choisiHeuresArriveeRetour = choisiHeuresArriveeRetour;
	window.choisiHeuresArriveeRetourMatin = function() { choisiHeuresArriveeRetour(5, 12); };
	window.choisiHeuresArriveeRetourApresMidi = function() { choisiHeuresArriveeRetour(12, 18); };
	window.choisiHeuresArriveeRetourSoir = function() { choisiHeuresArriveeRetour(18, 24); };

	// Fonctions permettant de selectionner les valeurs de tableaux entiers
	window.choisiTousPrix = function(refresh) { choisirTout(window.arrayPRIX, refresh); };
	window.choisiToutesEscales = function(refresh) { choisirTout(window.arrayESCALES, refresh); };
	window.choisiToutesCompagnies = function(refresh) { choisirTout(window.arrayCOMPAGNIES, refresh); };

	window.choisiTousAeroportsDepartAller = function(refresh) { choisirTout(window.arrayAEROPORTS_DEPARTALLER, refresh); };
	window.choisiTousAeroportsArriveeAller = function(refresh) { choisirTout(window.arrayAEROPORTS_ARRIVEEALLER, refresh); };
	window.choisiTousAeroportsDepartRetour = function(refresh) { choisirTout(window.arrayAEROPORTS_DEPARTRETOUR, refresh); };
	window.choisiTousAeroportsArriveeRetour = function(refresh) { choisirTout(window.arrayAEROPORTS_ARRIVEERETOUR, refresh); };

	window.choisiToutesHeuresDepartAller = function(refresh) { choisirTout(window.arrayHEURES_DEPARTALLER, refresh); };
	window.choisiToutesHeuresArriveeAller = function(refresh) { choisirTout(window.arrayHEURES_ARRIVEEALLER, refresh); };
	window.choisiToutesHeuresDepartRetour = function(refresh) { choisirTout(window.arrayHEURES_DEPARTRETOUR, refresh); };
	window.choisiToutesHeuresArriveeRetour = function(refresh) { choisirTout(window.arrayHEURES_ARRIVEERETOUR, refresh); };
}());

