/*********************************************************
Bibliothèque de fonctions Javascript
Auteur : LMC (www.lmcfrance.com)
Création : 29 Août 2006
**********************************************************/

/****************************************************************
* Fonction qui charge le XML extérieur dans un DIV ( FILRSS)
*/
function charge_RSS(id_div, flux_rss){
	//getObjectById(id_div).innerHTML = "Chargement en cours...";
	//alert("/global/proxy_rss.aspx?flux="+flux_rss );
	// On déclare l'objet XmlHttpRequest
	var xhr = creer_xhr();

	// Actions lorsqu'on obtient réponse (état = 4)
	xhr.onreadystatechange = function(){
	// On ne fait quelque chose que si on a tout reçu et que le serveur est ok
	    if(xhr.readyState == 4 && xhr.status == 200){
		    var reponse = nettoieXML(xhr.responseXML.documentElement);
		    getObjectById(id_div).innerHTML = "";

			//On air
			var noeud = reponse.getElementsByTagName("item")[reponse.getElementsByTagName("item").length-2];

			var titre = noeud.getElementsByTagName("title")[0].firstChild.nodeValue;
			
			//Heure
			var _heure = titre.substring(0,2);
			if(_heure>=1) titre = (_heure-1) + titre.substring(2);
			else titre = (_heure-1+24) + titre.substring(2);
			
			getObjectById(id_div).innerHTML += titre +"<br />";
			//next
			noeud = reponse.getElementsByTagName("item")[reponse.getElementsByTagName("item").length-1];
			getObjectById(id_div).innerHTML += '<span id="next">NEXT: '+noeud.getElementsByTagName("title")[0].firstChild.nodeValue+"</span>" ;

	    } // end if xhr...
        else {
		    //S'il est impossible de récupérer les données
		    if(xhr.readyState == 4 && xhr.status != 200)  {
		        getObjectById(id_div).innerHTML = "Erreur lors du chargement des données...";
		    } // end IF
		} // end else
	} // end function
	
	//On envoie la requête avec l'heure pou éviter le cache
	var da = new Date();
	xhr.open("GET",encodeURI(flux_rss+"?time="+da.getHours()+da.getMinutes()+da.getSeconds()+da.getMilliseconds()),true);
	xhr.send(null);
	
} // end function charge_RSS


/*****************************************************************
* Méthodes multi-navigateur de sélection d'un Element via son id
**/
function checkBrowser()
{	
	this.ver=navigator.appVersion;
	this.dom=document.getElementById?1:0;
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
	this.ie55=((this.ver.indexOf("MSIE 5.5")>-1 || this.ie6) && this.dom)?1:0;
	this.ie5=((this.ver.indexOf("MSIE 5")>-1 || this.ie5 || this.ie6) && this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.ie4plus=(this.ie6 || this.ie5 || this.ie4);
	this.ie5plus=(this.ie6 || this.ie5)
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns5);
	return this;
}
			
function getObjectById(ID) 
{
	var obj;
	var bw = new checkBrowser();
	if (bw.dom)
		return document.getElementById(ID);
	else if (bw.ie4)
		return document.all(ID);
	else if (bw.ns4)
		return eval('document.' + ID);
}


/*****************************************************************
 Fonctions de traitement du XML
**/
function traiteXML(c){
	if(!c.data.replace(/\s/g,''))
		c.parentNode.removeChild(c);
}

function nettoieXML(d){
	var bal=d.getElementsByTagName('*');

	for(i=0;i<bal.length;i++){
		a=bal[i].previousSibling;
		if(a && a.nodeType==3)
			traiteXML(a);
		b=bal[i].nextSibling;
		if(b && b.nodeType==3)
			traiteXML(b);
	}
	return d;
}

/*****************************************************************************
* Crée et renvoie un objet XMLHttpRequest utilisable dans tous les navigateurs
*/
function creer_xhr() {
	var xhr = null;
	if(window.XMLHttpRequest) // Firefox et autres
	   xhr = new XMLHttpRequest(); 
	else if(window.ActiveXObject){ // Internet Explorer 
		   try {
                xhr = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                xhr = new ActiveXObject("Microsoft.XMLHTTP");
            }
	}
	else { // XMLHttpRequest non supporté par le navigateur 
	   //alert("Votre navigateur ne supporte pas les objets XMLHTTPRequest..."); 
	   xhr = false; 
	}
	return xhr;
}
