var delai_formations=5;
var format_en_cours=0;
var lst_formations = new Array();
// local
//var urlF="/internet/xml/xml-formations.asp"
// cds
//var urlF="http://cds.groupehelice.fr/refonte/xml/xml-formations.asp"
// prod
var urlF="http://www.groupehelice.fr/internet/xml/xml-formations.asp";

//var urlF="http://srv029:81/xml/xml-formations.asp";

function mainLectureFormations(filtre){
	$('loader-formations').show();

	new Ajax.Request(urlF,{ method: 'post',	
			onSuccess: function(transport) {
				var ListeSessions = LireXMLFormations(transport.responseXML)				
				metsEnPlaceFormations(ListeSessions, filtre);
				$('loader-formations').hide();
			}}
		);
}


function UneSession(un_titre, une_date, une_description, un_lien) {
	this.titre=un_titre;
	this.date=une_date;
	this.description = une_description;
	this.lien = un_lien;
}
 
 // Transformation XML en tableau
function LireXMLFormations(xmlDoc) {
  
  var ListeSessions = xmlDoc.getElementsByTagName('listeFormations');
  var sessions = new Array();    
  
  for (var j=0; j < ListeSessions.length; ++j) {    
      var Liste = ListeSessions[j].getElementsByTagName('item');	    
      for (var i=0; i < Liste.length; ++i) { 
	    
	    var titre="";
		var lien="";
		var dte="";
		var description="";
		
		titre = Liste[i].getElementsByTagName("title")[0].firstChild.nodeValue;	
		lien = Liste[i].getElementsByTagName("link")[0].firstChild.nodeValue;		
		var pubdate = Liste[i].getElementsByTagName("pubDate")[0].firstChild.nodeValue;
		dte=titre.substring(titre.length-10,  titre.length);				
		description = Liste[i].getElementsByTagName("description")[0].firstChild.nodeValue;
		
		sessions.push(new UneSession(titre, dte, description,lien ));		
       }	   
    }
	
	return sessions;
}

function metsEnPlaceFormations(ListeSessions, filtre){
	
	// page d'accueil, le nombre max de sessions est renseigné dans le filtre
	var element = $("prochaines-sessions-accueil");
	element.update('');
	
	if (ListeSessions.length==0 ){
		$("prochaines-sessions-accueil").update("Aucune annonce trouvée !");
		$('prochaines-sessions-accueil').show();
		$('loader-formations').hide();
		return;
	}

	var contenu	="";
	var fMax = filtre;
	if(filtre>ListeSessions.length) fMax = ListeSessions.length;
	
	for(var f=0; f<fMax; ++f) {
		var titre = ListeSessions[f].titre.substring(9, ListeSessions[f].titre.length-13);
		var divId="prochaine-formation-" + f;
		var nouvelleDiv = Builder.node('div', {id: divId, className: 'prochaine-formation'});			
		$("prochaines-sessions-accueil").insert(nouvelleDiv);	
		
		var nouveauStrong = Builder.node('strong', ListeSessions[f].date);	
		$(divId).insert(nouveauStrong);		
		
		var HREF= ListeSessions[f].lien;		
		var nouveauLien = Builder.node('a', {id: "a-" + divId, href: HREF, className: 'session', title: titre});									
		$(divId).insert(nouveauLien);
		
		var contenuLien = Builder.node('h3', titre);
		$("a-" + divId).insert(contenuLien);
		contenuLien = Builder.node('span');
		$("a-" + divId).insert(contenuLien);
	}
	$("prochaines-sessions-accueil")
	lst_formations = $$(".prochaine-formation").toArray();
	 $$(".prochaine-formation").invoke("hide");
	 $(lst_formations.first()).show();	
	 format_en_cours = 0;
	 
	new PeriodicalExecuter(nextsessions, delai_formations);	
	
	$('prochaines-sessions-accueil').show();
}


function nextsessions()
{
	$(lst_formations[format_en_cours]).hide();	
	format_en_cours +=1;
	if(format_en_cours >= lst_formations.length) format_en_cours = 0;
	$(lst_formations[format_en_cours]).show();
}
