window.addEvent('load', function(){

	tabfiltre();

	$$('input[type=checkbox]').addEvent('click', function() {
		tabfiltre();
	});

	$('tous-pgm').getElements('img').addEvents({
		'mouseenter': function(){
			$$('div[class=transparent]').each(function(odiv) { 
				oimg = document.getElementById("img" + odiv.get('id'));
				cimgnoirblanc = oimg.get('srcout');
				oimg.set('src', cimgnoirblanc);
				odiv.style.display = 'none';
			});

			cimgcouleur = this.get('srcover');
			this.set('src', cimgcouleur);			
			var divtransp = this.getNext('div');
			if (divtransp.style.display = 'none') {
				divtransp.set('opacity', '0');
				divtransp.style.display = 'block';
				//divtransp.set('display', 'block');
				divtransp.morph({'opacity': 0.85, 'duration': 500});
			}
		}/*,
		'mouseleave': function(){
			var divtransp = this.getNext('div');
			if (divtransp.style.display = 'block') {
				divtransp.morph({'opacity': 0});
				divtransp.style.display = 'none';
				//divtransp.set('display', 'none');
			}
		}*/	
	});

	$$('div[class=transparent]').addEvent('mouseleave', function() {
		oimg = document.getElementById("img" + this.get('id'));
		cimgnoirblanc = oimg.get('srcout');
		oimg.set('src', cimgnoirblanc);
		if (this.style.display = 'block') {
			this.morph({'opacity': 0});
			this.style.display = 'none';
			//divtransp.set('display', 'none');*
		}
	});
	
});

	// Fonction de contruction des filtres
	function tabfiltre () {
		var tfiltre = new Array();
		tfiltre['F'] = new Array();
		tfiltre['R'] = new Array();
		tfiltre['T'] = new Array();
		tfiltre['L'] = new Array();
		tfiltre['B'] = new Array();
		$$('input[type=checkbox]').each(function(ocheck) { 
			if (ocheck.checked) {
				tfiltre[ocheck.get('crit')].push(ocheck.value);
			}
		});

		// Déclaration du tableau de valeurs des programmes
		var tvaleur = new Array();

		// Déclaration du tableau des filtres à conserver (checkbox)
		var tfiltreinclu = new Array();
		tfiltreinclu['F'] = new Array();
		tfiltreinclu['R'] = new Array();
		tfiltreinclu['T'] = new Array();
		tfiltreinclu['L'] = new Array();
		tfiltreinclu['B'] = new Array();	

		$$('div[class=transparent]').each(function(odiv) { 

			// Mise en tableau des valeurs du programme courant
			tvaleur['F'] = odiv.get('F').split(',');
			tvaleur['R'] = odiv.get('R').split(',');
			tvaleur['T'] = odiv.get('T').split(',');
			tvaleur['L'] = odiv.get('L').split(',');
			tvaleur['B'] = odiv.get('B').split(',');
			// Test de l'existence d'au moins un élément pour tout tableau de valeur dans chaque tableau de filtre correspondant
			var lactif = comparetableaux(tvaleur['F'], tfiltre['F']) && comparetableaux(tvaleur['R'], tfiltre['R']) && comparetableaux(tvaleur['T'], tfiltre['T']) 
				&& comparetableaux(tvaleur['L'], tfiltre['L']) && comparetableaux(tvaleur['B'], tfiltre['B']);
			odivpar = odiv.getParents('div[id=miniatures]');
			if (lactif) {
				if (odivpar.getStyle('display')=='none') {
					odivpar.set('opacity', '0');
					odivpar.setStyle('display', 'block');
					odivpar.morph({'opacity': 1});
				}
				// Alimentation en valeurs uniques du tableau des filtres à conserver
				tfiltreinclu['F'].combine(tvaleur['F']);
				tfiltreinclu['R'].combine(tvaleur['R']);
				tfiltreinclu['T'].combine(tvaleur['T']);
				tfiltreinclu['L'].combine(tvaleur['L']);
				tfiltreinclu['B'].combine(tvaleur['B']);				
			} else {
				if (odivpar.getStyle('display')!='none') {
					odivpar.morph({'opacity': 0});
					cachediv(odivpar);
					
/*=================================*/	
/*		var fnChain = new Chain();
		
		fnChain.chain(
			function(){odivpar.morph({opacity: 0});},
			(function(){odivpar.setStyle('display', 'none');}).delay(1000)
		);

		fnChain.callChain();
		(function(){fnChain.callChain();}).delay(1000);
		
		//(function(){fnChain.callChain();}).delay(100);
		fnChain.clearChain();

		//(function(){fnChain.callChain();}).delay(1000);
*/
/*=================================*/	
				}
			}
		});	

		// Mise à jour des cases à cocher restantes disponibles en fonction des programmes actifs
		$$('input[type=checkbox]').each(function(ochk) { 
			var critere = ochk.get('crit');
			var valeur  = ochk.get('value');
			var parent 	= ochk.getParent();
			var texte 	= ochk.getParent().getNext();
			if (existetableau(valeur, tfiltreinclu[critere])) {
				parent.morph({'opacity': 1});
				texte.morph({'opacity': 1});
				ochk.disabled = false;
			} else {
				parent.morph({'opacity': 0.4});
				texte.morph({'opacity': 0.4});
				ochk.disabled = true;
			}
		});	
	}

	
	function fadediv(){
		this.setStyle('display', 'none');
	};
	
	// Fonction de masquage des vignettes
	function cachediv(odiv) {
		odiv.setStyle('display', 'none');
	}
	
	// Fonction de décochage de tous les checkbox
	function resetfiltre () {
		$$('input[type=checkbox]').each(function(ocheck) { 
			ocheck.checked=false;
		});	
		tabfiltre ();
	}	

	// Fonction de test d'existence d'au moins 1 élément du tableau 'ta' dans le tableau 'tb'
	function comparetableaux(ta, tb) { 
		if (tb.length==0) {
			return true;
		} else {
			for (j=0;j<ta.length;j++) {
				for (k=0;k<tb.length;k++) {
					if (ta[j]==tb[k]) {
						return true;
					}
				} 
			} 
		   return false; 
		}
	} 
	
	// Fonction de test d'existence d'un élément dans un tableau
	function existetableau(elt, ta) {
		if (ta.length==0) {
			return true;
		} else {
			for (j=0;j<=ta.length;j++) {
				if (ta[j]==elt) {
					return true;
				} 
			} 
		   return false; 
		}	
	}


