var gallery = new Array();
var curIndex = new Array();

function initGallery(idGallery) {

  gallery[idGallery] = new Array();

}

function addPhoto(idGallery, path, date, description) {
      
  var photo = new Image();
  photo.src = path;
  gallery[idGallery].push({ photo: photo, date: date, description: description });
        
}

function displayPhoto(idGallery, index) {

  //  Instancier l'image à afficher pour récupérer ses dimensions
  var photo = gallery[idGallery][index].photo;

  //  Créer le contenu du popup
  curIndex[idGallery] = index;
  var content = "<table class='Photo' style=\"width:" + photo.width + "; height:" + photo.height + "; background-image:url(" + photo.src + ");\"><tr>"
      + "<td id='PhotoPrev' class='PhotoPrevNext' onMouseOver=\"showButton('PhotoPrev');\" onMouseOut=\"hideButton('PhotoPrev');\"><input type='image' src='composants/BtnPrevPhoto.png' onClick='prevPhoto(" + idGallery + ");' /></td>"
      + "<td></td>"
      + "<td id='PhotoNext' class='PhotoPrevNext' onMouseOver=\"showButton('PhotoNext');\" onMouseOut=\"hideButton('PhotoNext');\"><input type='image' src='composants/BtnNextPhoto.png' onClick='nextPhoto(" + idGallery + ");' /></td>";
      
 
  //  Afficher la photo dans une popup
  var width = photo.width + 20;
  var height = photo.height + 50;
  var title = "Photo " + (index + 1) + " sur " + gallery[idGallery].length 
      + " --- &laquo; " + gallery[idGallery][index].description + " &raquo; le " + gallery[idGallery][index].date;
  showPopup(width, height, title, content);   

}

function showButton(cellId) {

  var cell = document.getElementById(cellId);
  cell.className = 'PhotoPrevNextActive';

}

function hideButton(cellId) {

  var cell = document.getElementById(cellId);
  cell.className = 'PhotoPrevNext';

}

function prevPhoto(idGallery) {

  var prevIndex = ((curIndex[idGallery] - 1) >= 0 ? curIndex[idGallery] - 1 : gallery[idGallery].length - 1);
  displayPhoto(idGallery, prevIndex);
 
}

function nextPhoto(idGallery) {

  var nextIndex = ((curIndex[idGallery] + 1) < gallery[idGallery].length ? curIndex[idGallery] + 1 : 0); 
  displayPhoto(idGallery, nextIndex);

}
