// Variables Globales
var tableauNews;
var nombrenews;
var tableauPhotos;
var nombrephotos;

//IE ou pas IE : That's the question !!
function addListener (element, baseName, handler) {
  if (element.addEventListener) {
	  element.addEventListener(baseName, handler, false);
  }	
  else if (element.attachEvent) {
	  element.attachEvent('on' +  baseName, handler);
  }	
} 

function RelierEcouteurs() {	
  ////////////////////////////////FONCTIONS///////////////////////////////////////////////  
  ////////////////////////////MODULE////////////////////////////////////////////////  
  addListener(document.getElementById('texteFacebookMetz'), 'click', function() {
    expandingWindowLocation("http://fr-fr.facebook.com/people/Imc-Metz-Technopole/100000093452662");
  });	
  addListener(document.getElementById('texteFacebookNancy'), 'click', function() {
    expandingWindowLocation("http://fr-fr.facebook.com/people/Imc-Nancy/100001187962578");
  });	  
  if(document.getElementById('plusActualites')) {
    addListener(document.getElementById('plusActualites'), 'click', function() {
      document.location="Actualites.php";
    });  
  }
  ////////////////////////////MENU////////////////////////////////////////////////
  addListener(document.getElementById('accueil'), 'click', function() {
    document.location="index.php";
  });	
  addListener(document.getElementById('IMC'), 'click', function() {
    document.location="IMCMetz.php";
  });
  addListener(document.getElementById('formations'), 'click', function() {
    document.location="ProgrammeEtudes.php";
  });  
  addListener(document.getElementById('telechargements'), 'click', function() {
    document.location="PostesAPourvoir.php";
  });
  addListener(document.getElementById('inscription'), 'click', function() {
    document.location="Inscription.php";
  });      
}	
											
//Instanciation de l'objet AJAX
function GetXmlHttpObject() {
  var objXMLHttp = null;
  //IE ou pas ?
  if (window.XMLHttpRequest) {
		objXMLHttp= new XMLHttpRequest();
  }  
  else if (window.ActiveXObject){
		objXMLHttp= new ActiveXObject("Microsoft.XMLHTTP");
  }  
  //Si pas d'objet alors retourne null
  return objXMLHttp;
}		

//////////////////////////////INITIALISATION////////////////////////////////////
function Load() {
 ChercherPhotos();
 setInterval("Carrousel()",2000);  
 ChercherActualites();
 setInterval("Actualites()",5000); 
}

///////////////////////////////////CHERCHER LES ACTUALITES////////////////////////
function ChercherActualites() {
  var xhrChercherActualites = GetXmlHttpObject(); 
  if (xhrChercherActualites == null){
		alert("Votre navigateur ne supporte pas les requêtes AJAX !");
		return false;
  }	
  xhrChercherActualites.onreadystatechange=function(){
    if (xhrChercherActualites.readyState == 4 && xhrChercherActualites.status == 200){
       //Récupération des données
       tableauNews=xhrChercherActualites.responseText.split("#|#");
       nombrenews=tableauNews.length;
       //Par défaut, on affiche au moins la première news
       document.getElementById("texteFlashInfo").innerHTML=tableauNews[0];
    }
  }			 	  	  			 	 			 	 	 	   	 
  // On exécute la demande		
  xhrChercherActualites.open("GET","Controleur/Controleur.php?action=10", true);
  xhrChercherActualites.send(null);	   		 
  return true; 
}

///////////////////////////////////CARROUSEL ACTUALITES////////////////////////
function Actualites() {
  //Numéro de l'actualité au hasard
  var numNews= parseInt(Math.floor(Math.random()*nombrenews));
  
  if(numNews==nombrenews) {
    numNews=numNews-1;
  }
  
  //On affiche
  document.getElementById("texteFlashInfo").innerHTML=tableauNews[numNews];     
}

///////////////////////////////////CHERCHER LES PHOTOS////////////////////////
function ChercherPhotos() {
  var xhrChercherPhotos = GetXmlHttpObject(); 
  if (xhrChercherPhotos == null){
		alert("Votre navigateur ne supporte pas les requêtes AJAX !");
		return false;
  }	
  xhrChercherPhotos.onreadystatechange=function(){
    if (xhrChercherPhotos.readyState == 4 && xhrChercherPhotos.status == 200){
       //Récupération des données
       tableauPhotos=xhrChercherPhotos.responseText.split("#+#");
       nombrephotos=tableauPhotos.length;
       //Par défaut, on affiche au moins la première news
       document.getElementById("photoDefilement").innerHTML=tableauPhotos[0];
    }
  }			 	  	  			 	 			 	 	 	   	 
  // On exécute la demande		
  xhrChercherPhotos.open("GET","Controleur/Controleur.php?action=15", true);
  xhrChercherPhotos.send(null);	   		 
  return true; 
}

///////////////////////////////////CARROUSEL DE PHOTOS////////////////////////
function Carrousel() {
  //Numéro de l'actualité au hasard
  var numPhotos= parseInt(Math.floor(Math.random()*nombrephotos));
  
  if(numPhotos==nombrephotos) {
    numPhotos=numPhotos-1;
  }
    
  //On affiche
  document.getElementById("photoDefilement").innerHTML=tableauPhotos[numPhotos];  
}

///////////////////////////////////OUVERTURE DE FICHIER////////////////////////
function expandingWindowLocation(url) {
  window.open(url,'','location=yes,toolbar=yes,directories=yes,menubar=yes,resizable=yes,scrollbars=yes,status=yes,width=900,height=500,screenY=0,screenX=0');
}

///////////////////////////////////INITIALISATION///////////////////////////////
//Initialise les écouteurs lors de l'include
addListener(window, 'load', RelierEcouteurs);	
