/*-------------------------*/
//  photo	
//	flb pour neXXen-multimedia
//  2005-12-16 - Version 1-0
/*-------------------------*/

function fermerPhoto(){
  var blk = getElem("blockPhoto");
  blk.style.display = "none";
}

function afficherPhoto() { 
  var pht = getElem("photoActive");
  var blk = getElem("blockPhoto");
  var lgd = getElem("legend");
  if(pht && blk){
    blk.style.display = "block";
    var i = this.index; 
    pht.src = tabCaracPhoto[i].src;
    pht.width = tabCaracPhoto[i].width;
    pht.height = tabCaracPhoto[i].height;
    if(lgd){
      var text = (tabCaracPhoto[i].legende.length > 0)? tabCaracPhoto[i].legende:"";
      var textLgd = document.createTextNode(text);
      if(lgd.firstChild) lgd.replaceChild(textLgd, lgd.firstChild);
      else lgd.appendChild(textLgd);
    }
    photoActive = i;
  }
  return false;
}

function photoSuivante() { 
  var pht = getElem("photoActive");
  var blk = getElem("blockPhoto");
  var lgd = getElem("legend");
  if(pht && blk){
    blk.style.display = "block";
    var i = photoActive + 1; 
    if(i >= tabCaracPhoto.length) i = 0;
    pht.src = tabCaracPhoto[i].src;
    pht.width = tabCaracPhoto[i].width;
    pht.height = tabCaracPhoto[i].height;
    if(lgd){
      var text = (tabCaracPhoto[i].legende.length > 0)? tabCaracPhoto[i].legende:"";
      var textLgd = document.createTextNode(text);
      lgd.replaceChild(textLgd, lgd.firstChild);
    }
    photoActive = i;
  }
  return false;
}

function initPhoto(){
  domPhoto = getElem(idPhoto);
  domVgn = getElem(idVgn);
  
  if(domPhoto && domVgn) {
    domPhoto.style.display = "none";
    // on recupere les liens des vignettes
    var tabA = domVgn.getElementsByTagName("A");
    for(var i=0; i<tabA.length; i++){ 
      tabA[i].index = i;
      tabA[i].onclick = afficherPhoto;
    }
    var tabIMG = domPhoto.getElementsByTagName("IMG");
    for(var i=0; i<tabIMG.length; i++){ 
      tabCaracPhoto[i] = {
        "src": tabIMG[i].src,
        "width": tabIMG[i].width,
        "height": tabIMG[i].height,
        "legende": tabIMG[i].alt
      }
    }
    
    var divPhoto = document.createElement("DIV");
    divPhoto.id = "blockPhoto";
    
    var btnFermer = document.createElement("A");
    btnFermer.href = "#";
    btnFermer.title = "Fermer la galerie";
    btnFermer.className = "fermerPhoto";
    btnFermer.onclick = fermerPhoto;
    var textFermer = document.createTextNode("Fermer");
    btnFermer.appendChild(textFermer);
    
    var btnSuivant = document.createElement("A");
    btnSuivant.href = "#";
    btnSuivant.title = "Image suivante";
    btnSuivant.onclick = photoSuivante;
        
    var imgPhoto = document.createElement("IMG");
    imgPhoto.id = "photoActive";
    
    var legend = document.createElement("P");
    legend.id = "legend";
    
    btnSuivant.appendChild(imgPhoto);
    divPhoto.appendChild(btnSuivant);
    divPhoto.appendChild(legend);
    divPhoto.appendChild(btnFermer);
    
    var elemSuivant = domVgn.nextSibling; 
    domVgn.parentNode.insertBefore(divPhoto, elemSuivant);
    //domVgn.appendChild(divPhoto);
    divPhoto.style.display = "none";
    
    supLienPointille(divPhoto);
  }
  else {
    return false;
  }  
}

var idPhoto = "photo";
var domPhoto;
var tabPhoto = new Array();
var idVgn = "conteneurVignette";
var domVgn;
var tabCaracPhoto = new Array();
var photoActive;

if(dom){
  if(BrowserDetect.browser != "Netscape" && (BrowserDetect.browser != "Explorer" || BrowserDetect.version >= 6)){
    var css = document.getElementById("domCss");
    css.href = "style/ecran_dom.css"; 
    addOnLoad(initPhoto); 
  }
}
