// JavaScript Document


function getXMLHTTP(){
  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) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e1) {
        xhr = null;
      }
    }
  }
  else { // XMLHttpRequest non supporté par le navigateur
   location.href+=(location.href.indexOf("?")!=-1)?("&ajax=non"):("?ajax=non");
  }
  return xhr;
}

function getXMLParser(){
  var xhr=null;
  var xpar=null
  if(window.XMLHttpRequest) // Firefox et autres
  xhr = null
  else if(window.ActiveXObject){ // Internet Explorer
    try {
      xhr = new ActiveXObject("Msxml2.XMLHTTP");
      xpar = new ActiveXObject("Msxml2.DOMDocument.3.0");
    } catch (e) {
      try {
        xhr = new ActiveXObject("Microsoft.XMLHTTP");
        xpar = new ActiveXObject("Microsoft.DOMDocument.3.0");
      } catch (e1) {
        xhr = null;
      }
    }
  }
  else { // XMLHttpRequest non supporté par le navigateur
   location.href+=(location.href.indexOf("?")!=-1)?("&ajax=non"):("?ajax=non");
  }
  return xhr;
}


var _resultCache=new Object(); // mécanisme de cache des requetes

// echappe les caractère spéciaux
function escapeURI(La){
  if(encodeURIComponent) {
    return encodeURIComponent(La);
  }
  if(escape) {
    return escape(La)
  }
}


var _xmlHttp = null; //l'objet xmlHttpRequest utilisé pour contacter le serveur

var _adresseRecherche = "fiche.php"
var _adresseRecherche2 = "rmd/xmlacteur.php"




fiche=function(id_fiche,id_form){
   obj=document.getElementById("completefiche")
   
   if(id_fiche==0){
      document.getElementById('cacheprint').className=''
      obj.style.display="none";return;
   }else{
        if(_xmlHttp&&_xmlHttp.readyState!=0){_xmlHttp.abort()}
        _xmlHttp=getXMLHTTP();
        if(_xmlHttp){
           url=_adresseRecherche+"?id_fiche="+id_fiche+"&id_formulaire="+id_form;
           _xmlHttp.open("GET",url,true);
            _xmlHttp.onreadystatechange=function() {
                    if(_xmlHttp.readyState==4 && _xmlHttp.responseXML) {
                        ficheHTML=createFiche(_xmlHttp.responseXML)
                        obj.innerHTML=ficheHTML;
                        obj.style.top=document.body.parentNode.scrollTop+"px"
                        c=document.getElementById("centredata")
                        m=document.getElementById("milieu")

                        obj.style.display="block"
                        c.style.marginBottom="-150px"
                        m.style.height=c.offsetHeight-150+"px"

                   }
              }
           _xmlHttp.send(null);

        }else{
         var liste=getElementsByClass("loupehand");
           for(i in liste){liste[i].style.display="block"}
        }
   }

}











createFiche=function(xmlDoc){

   document.getElementById('cacheprint').className='ficheView'
   
    var couple = new Array();
 $("item",xmlDoc).each(function(i){
							couple[i]=new Array();
							couple[i]['type']=$(this).attr("type")
							couple[i]['champ']=$("champ",this).text()
							couple[i]['valeur']=$("valeur",this).text().replace(/\n/gi,"<br/>");
								
								
})
   
  debut="<div class=\"haut\"></div><div class=\"milieu\" id=\"milieu\"><div class=\"data\" id=\"centredata\">";

   fin="</div></div><div class=\"bas\"></div>";

milieu=""
   for(i=0;i<couple.length;i++){
      test=false;
         if(couple[i-1])test=(couple[i]['champ']==couple[i-1]['champ'])

         if(couple[i]['champ']=="Nom" || couple[i]['champ']=="Titre" || couple[i]['champ']=="Stage"){
            milieu+="<h2>"+couple[i]['valeur']+"</h2>";
         }else if(couple[i]['champ']=="ref"){
            milieu+="<h6 class=\"ref\">"+couple[i]['valeur']+"</h6>";

         }else if(couple[i]['champ']=="chapo"){
            milieu+="<p class=\"chapo\">"+couple[i]['valeur']+"</p>";


         }else if(couple[i]['champ']=="inscription en ligne"){
            milieu+="<p class='lien'><a href='99-inscription-en-ligne.html'>Inscription en ligne</a></p>";

         }else if(couple[i]['champ']=="témoignages"){
            milieu+="<p class='lien'><a href='34-temoignages.html'>Témoignages</a></p>";

         }else if(couple[i]['type']=="9"){
            ext=couple[i]['valeur'].split(".")[1];
            if(ext=='jpg' ||ext=='png' || ext=='gif'){

               milieu+="<p><img src='upload/"+couple[i]['valeur']+"' class='imagefiche'/></p>";
            }else if(ext!=undefined){
               milieu+="<p><a href='upload/"+couple[i]['valeur']+"'>Cliquez ici pour télécharger le Tract</a></p>";
            }

         }else if(test){
            milieu+="<p >"+couple[i]['valeur']+"</p>";
         }else{
            milieu+="<h3 >"+couple[i]['champ']+" /</h3>";
            if(couple[i]['valeur'].indexOf("http://")>-1){
               milieu+="<p ><a href='"+couple[i]['valeur'].toString()+"'>"+couple[i]['valeur'].toString()+"</a></p>";

            }else if(couple[i]['valeur'].indexOf("@")>-1){

               milieu+="<p ><a href='mailto:"+couple[i]['valeur'].toString()+"'>"+couple[i]['valeur'].toString()+"</a></p>";
            }else{

               milieu+="<p >"+couple[i]['valeur'].toString()+"</p>";
            }


         }

   }



  return debut+milieu+fin;

}




ficheRMD=function(id){
   obj=document.getElementById("completefiche")

   if(id==0){
      document.getElementById('cacheprint').className=''
      obj.style.display="none";return;
   }else{
        if(_xmlHttp&&_xmlHttp.readyState!=0){_xmlHttp.abort()}
        _xmlHttp=getXMLHTTP();
        if(_xmlHttp){
           url=_adresseRecherche2+"?REF_ACTEUR="+id;
           _xmlHttp.open("GET",url,true);
            _xmlHttp.onreadystatechange=function() {//&& _xmlHttp.responseXML
                    if(_xmlHttp.readyState==4 && _xmlHttp.responseText) {
                        ficheHTML=createFicheRMD(_xmlHttp.responseText)
                        obj.innerHTML=ficheHTML;
                        obj.style.top=document.body.parentNode.scrollTop+"px"
                        c=document.getElementById("centredata")
                        m=document.getElementById("milieu")

                        obj.style.display="block"
                        c.style.marginBottom="-150px"

                        m.style.height=c.offsetHeight-150+"px"


                   }
              }
           _xmlHttp.send(null);







        }else{
         var liste=getElementsByClass("loupehand");
           for(i in liste){liste[i].style.display="block"}
        }
   }

}



createFicheRMD=function(xmlDoc){

   document.getElementById('cacheprint').className='ficheView'

  debut="<div class=\"haut\"></div><div class=\"milieu\" id=\"milieu\"><div class=\"data\" id=\"centredata\">";

   fin="</div></div><div class=\"bas\"></div>";
   milieu=xmlDoc.toString()



  return debut+milieu+fin;

}




getElementsByClass=function(maClass,elementSel) {
      var tabRetour = new Array();var tabTmp = new Array();
      if(elementSel==undefined){tabTmp = document.getElementsByTagName("*");}
     else{tabTmp = elementSel.getElementsByTagName("*"); }j=0;
      for (i=0; i<tabTmp.length; i++) {if (tabTmp[i].className==maClass) {tabRetour[j]=tabTmp[i]; j++;}}return tabRetour;
}



resiEncart=function(titre,id,height){
   variable="titre="+titre+"&id="+id+"&h="+height
   flashCreate('e'+id,'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width', '35px','height',height+'px','src', 'titreencart','quality', 'medium','pluginspage', 'http://www.macromedia.com/go/getflashplayer','align', '','play', 'true','loop', 'true','scale', 'noscale','wmode', 'transparent','devicefont', 'false','FlashVars',variable,'id', 'titreencart','bgcolor', '#2E2F30','name', 'titreencart','menu', 'true','allowScriptAccess','sameDomain','movie', 'titreencart','salign', 'lt');
   return(true)
}


montreMenu=function(id,posy,color){

   loc=document.getElementById("menu");
   cach=document.getElementById("cache");
   cachclose=document.getElementById("cacheclose");
   tabMenu=getElementsByClass("sousmenu");

   for(i in tabMenu){
      if(tabMenu[i].style)tabMenu[i].style.display="none";
   }
   cachclose.style.display=cach.style.display="none";
   cible=document.getElementById("s"+id);

   if(cible){
      h=document.getElementById("delim").offsetHeight
      if(document.all){posy+=5;h-=22}
      cible.style.display="block";
      cible.style.top=Number(posy+40)+"px";
      cachclose.style.display=cach.style.display="block";
      cachclose.style.height=cach.style.height=h+22+"px";
      atab=cible.getElementsByTagName("*");
      for(i in atab){
         if(atab[i].style){atab[i].style.color="#"+color}
      }
   }
}

initEncart=function(num,typepage){

   largBand=(typepage=="home")?710:650;

   document.getElementById("fond").style.height=document.body.offsetHeight+"px";


/* flashCreate('fond','codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width', '100%','height', '100%','src', 'onde','quality', 'medium','pluginspage', 'http://www.macromedia.com/go/getflashplayer','align', 'middle','play', 'true','loop', 'true','scale', 'noscale','wmode', 'transparent','devicefont', 'false','id', 'onde','bgcolor', '#128ae8','name', 'onde','menu', 'true','allowScriptAccess','sameDomain','movie', 'onde','salign', '');*/

   flashCreate('flash','codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width', '45px','height', '760px','src', 'menu','quality', 'medium','pluginspage', 'http://www.macromedia.com/go/getflashplayer','align','','play', 'true','loop', 'true','scale', 'noscale','wmode', 'transparent','devicefont', 'false','FlashVars','init='+num,'id', 'menuflash','bgcolor', '#2E2F30','name', 'menuflash','menu', 'true','allowScriptAccess','sameDomain','movie', 'menu','salign', 'lt');



   flashCreate('personnage','codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width', largBand+'px','height', '189px','src', 'personnage','quality', 'medium','pluginspage', 'http://www.macromedia.com/go/getflashplayer','align','','play', 'true','loop', 'true','scale', 'noscale','wmode', 'transparent','devicefont', 'false','FlashVars','init='+num,'id', 'perso','bgcolor', '#2E2F30','name', 'perso','menu', 'true','allowScriptAccess','sameDomain','movie', 'personnage','salign', 'lt');


   flashCreate('musique','codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0','width', '50px','height', '50px','src', 'music','quality', 'medium','pluginspage', 'http://www.macromedia.com/go/getflashplayer','align','','play', 'true','loop', 'true','scale', 'noscale','wmode', 'transparent','devicefont', 'false','FlashVars','init='+num,'id', 'music','bgcolor', '#2E2F30','name', 'music','menu', 'true','allowScriptAccess','sameDomain','movie', 'music','salign', 'lt');



   titreflash=getElementsByClass("titreflash");
   modif=50;

   for(i in titreflash){
      titre=titreflash[i].getElementsByTagName("span")[0].innerHTML;
      id=titreflash[i].id.substr(1);
      height=titreflash[i].offsetHeight;
      resiEncart(titre,id,height);
   }

   feu=document.getElementById("contenu");
   enc=document.getElementById("encarts");
   h1=enc.offsetHeight;
   h2=feu.offsetHeight-modif;

   mh=Math.max(h1,h2)

   enc.style.minHeight=enc.style.height=mh+"px"
   feu.style.minHeight=feu.style.height=mh+modif+"px"

    var s = document.getElementsByTagName('select');
      for (var i=0; i<s.length; i++) {
        if(s[i].id!='')selectReplacement(s[i]);
      }



}


/*  change select */


function selectReplacement(obj) {



  obj.className += ' replaced';
  var ul = document.createElement('ul');
  ul.className = 'selectReplacement';
  var opts = obj.options;
  for (var i=0; i<opts.length; i++) {
   var selectedOpt;
   if (opts[i].selected) {
     selectedOpt = i;
     break;
   } else {
     selectedOpt = 0;
   }
  }
  for (var i=0; i<opts.length; i++) {
   var li = document.createElement('li');
   var txt = document.createTextNode(opts[i].text);
   li.appendChild(txt);
   li.selIndex = opts[i].index;
   li.selectID = obj.id;
   li.onclick = function() {
     selectMe(this); }
   if (i == selectedOpt) {
     li.className = 'selected';
     li.onclick = function() {
      this.parentNode.className += ' selectOpen';
      this.onclick = function() {
        selectMe(this);
      }
     }
   }
   if (window.attachEvent) {
     li.onmouseover = function() {
      this.className += ' hover';
     }
     li.onmouseout = function() {
      this.className =
        this.className.replace(new RegExp(" hover\\b"), '');
     }
   }
   ul.appendChild(li);
  }
  obj.parentNode.insertBefore(ul,obj);
}

function submitForm(obj){
      document.getElementById(obj).submit()
}

function selectMe(obj) {




  var lis = obj.parentNode.getElementsByTagName('li');
  for (var i=0; i<lis.length; i++) {
   if (lis[i] != obj) {
     lis[i].className='';
     lis[i].onclick = function() {
      selectMe(this);
     }
   } else {
     setVal(obj.selectID, obj.selIndex);
     obj.className='selected';
     obj.parentNode.className =
     obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
     obj.onclick = function() {
      obj.parentNode.className += ' selectOpen';
      this.onclick = function() {selectMe(this);}
     }
   }
  }
}


function setVal(objID, selIndex) {
      var obj = document.getElementById(objID);
     var action=obj.getAttribute("onChange");
      obj.selectedIndex = selIndex;
     if(action)submitForm("formulairepage");
}



    function setForm() {
      var s = document.getElementsByTagName('select');
      for (var i=0; i<s.length; i++) {
        selectReplacement(s[i]);
      }
    }



