///////////////////////////////////////////////////////////////////////////
// Le code encadé qui suit est déstiné à simuler la lecture d'un doc XML AJAX //
///////////////////////////////////////////////////////////////////////////

var xmlNews = "<?xml version='1.0' encoding='utf-8' ?><actu>	<nouvelleActu>		<filtre>Agenda</filtre>		<titre>La consommation électrique de 8 bÃ¢timents communaux va diminuer</titre>		<lien>#</lien>		<date>04.04</date>		<heure>18:30</heure>	</nouvelleActu>	<nouvelleActu>		<filtre>Ville</filtre>		<titre>Téléphonie fixe: baisse des tarifs chez Citycable!</titre>		<date>06.04</date>		<heure>18:30</heure>		<lien>#</lien>	</nouvelleActu>	<nouvelleActu>		<filtre>Police</filtre>		<titre>Incendie dans un appartement au chemin de Bon-Abri</titre>		<date>06.04</date>		<heure>18:30</heure>		<lien>#</lien>	</nouvelleActu></actu>";

jQuery.createXMLDocument = function(string){
	var browserName = navigator.appName;	
	if (browserName == 'Microsoft Internet Explorer'){
		$.xml = new ActiveXObject('Microsoft.XMLDOM');
		$.xml.async = 'false'
		$.xml.loadXML(string);
	}else {
		$.xml = (new DOMParser()).parseFromString(string, 'text/xml');
	}
}

///////////////////////////////////////////////////////////////////////////////
// fin                                                                       //
///////////////////////////////////////////////////////////////////////////////


actus_municipales = {	
		timeToRefresh :15,	// temps en [s] pour le rafraichissement de la boÃ®te actu		
		filtre :"All", 		
		ajourdhui : "",
		init: function(){
			interval = setInterval(this.majActu, self.timeToRefresh*1000 ); 	
			interval2 = setInterval(this.refreshScrollBar, 1000);
			date = new Date();	
			var mois = date.getMonth() + 1;
			var jour = date.getDate();
			if(mois < 9 ) mois = "0" + mois;
			if(jour < 9 ) jour = "0" + jour;
			this.ajourdhui = jour+'.'+mois;
			
			this.distinctActu();
			
			customSelect.onChange('actus_municipales_select', this.filtreChange);
		},
		addLigneActu:function(subXML){		
			var htm = '<div class="actuLigne '+subXML.find('filtre').text()+'" style="background:#ffffda; display:none"><div class="dateHeure">'	
			if(subXML.find('date').text() == this.ajourdhui){
				htm += '<i>'+subXML.find('heure').text()+'</i></div><div class="txt"><a href="' + subXML.find('lien').text() + '">' + subXML.find('titre').text() + '</a></div></div>';				
			}else{
				htm +=       subXML.find('date').text() +    '</div><div class="txt"><a href="' + subXML.find('lien').text() + '">'  +subXML.find('titre').text() + '</a></div></div>';				
			}	
			$('#listeActu').prepend(htm);
			if(this.filtre == "All"){
				$('.actuLigne:first').slideDown("normal").animate({  backgroundColor: "#ffffda" }, 1000).parent().find('.actuLigne').first().animate({  backgroundColor: "#ffffff" }, 1000);	
				//$('.actuLigne:first').slideDown("normal").animate({  backgroundColor: "#ffffda" }, 1000);	
			}else{
				if(subXML.find('filtre').text() == this.filtre) $('.actuLigne:first').slideDown("normal").animate({backgroundColor: "#ffffda" }, 1000).parent().find('.actuLigne').first().animate({  backgroundColor: "#ffffff" }, 1000);				
			}
		},
		
		refreshScrollBar:function(){
				fleXenv.updateScrollBars();
		},
		majActu:function(){ // rafraichit périodiquement le contenu des actualité
			
			clearInterval(interval);
			// récupère les infos du serveur
			//$.createXMLDocument(xmlNews); // à remplacer par ajax pour récupérer les nouvelles infos
			
			$('#mycustomscroll').scrollTop(); //(false,0); // reviens en haut	
		
		// traite les nouvelles entrées
			$($.xml).find('nouvelleActu').each(function() {		
				//addLigneActu($(this).find('date').text(), $(this).find('heure').text(), $(this).find('titre').text());
				this.addLigneActu($(this));
			});
			interval = setInterval('actus_municipales.majActu()', this.timeToRefresh*1000 ); 
		},
		
		

		distinctActu:function(){
				$('#listeActu div.txt').each(function(){$(this).parent().show();})
	
				window.hackLastActuTexte='';
	
				$('#listeActu div.txt').each(function(){
					var act=$(this).parent();
					var texte=$(this).children().first().text();
					if(texte==hackLastActuTexte) act.hide();
					window.hackLastActuTexte=texte;
				});
		},

		filtreChange:function(id){ // fonction appelé quand l'utilisateur change le fitre 	
			window.nombre = 0; //window.actu_nombre=0;
			
			window.filtre = $('#actus_municipales_select').find('.select li').eq(customSelect.getSelectedIndex(id)).attr('data-value');							

			$('#pasActusTxt').remove();
			
			if (window.filtre == "All"){ // affiche tout
				$('.actuLigne').slideDown("normal");
				actus_municipales.distinctActu();
			}else{
				$('.actuLigne').each(function(){
					if($(this).hasClass(window.filtre)){
						$(this).slideDown("normal");
						window.nombre++;
					}else{
						$(this).slideUp("normal");
					}
				});
				
				if(window.nombre==0){
					$('.actuLigne').slideUp("normal");					
					$('#listeActu').append("<p id=\"pasActusTxt\">Il n'existe pas d'actualités correspondant à ce critère.<p>");
				}
			}
			$('.actuLigne').animate({  backgroundColor: "#ffffff" }, 1000);
		}
	}
	
	
	//var self = actus_municipales;

//})(); // fin de scope local



