// Va chercher le numéro de la photo
function trouveNumeroPhoto(chainePhoto) {
  var resultat = -1;
  var debut = chainePhoto.indexOf("<!-- IMG ");
  if (debut != -1) {
    var fin = chainePhoto.indexOf(" -->");
    if (fin != -1) {
      resultat = chainePhoto.substring(debut + 9, fin);
    }
  }
  return resultat;
}

// Numéro de la photo actuellement chargée
var numeroPhoto = 0;
// Galerie
var galerie = null;
// Sens par défaut
var sens = 'suivant';
// Interval
var idInterval = null;

// Charge une nouvelle photo
function photoNormale() {

  var xhr_object = null;

  if (window.XMLHttpRequest) // Firefox
    xhr_object = new XMLHttpRequest();
  else if (window.ActiveXObject) // Internet Explorer
    xhr_object = new ActiveXObject("Microsoft.XMLHTTP");
  else { // XMLHttpRequest non supporté par le navigateur
    return false;
  }

  if (numeroPhoto != null && numeroPhoto != -1) {
    xhr_object.open("GET", "http://www.festivalharajuku.org/services/photos/" + galerie + "/" + sens + "/" + numeroPhoto + "/", true);
  } else {
    xhr_object.open("GET", "http://www.festivalharajuku.org/services/photos/" + galerie + "/" + sens + "/", true);
  }

  xhr_object.onreadystatechange = function() { 
    if (xhr_object.readyState == 4) {
      // On vérifie qu'on trouve bien une photo
	  numeroPhoto = trouveNumeroPhoto(xhr_object.responseText);
      if (numeroPhoto != -1) {
        document.getElementById("ciblePhoto").innerHTML = xhr_object.responseText;
      }
    }
  }

  xhr_object.send(null);

  return false;
}

function photoPrecedente() {
  photoPause();
  photoContinu();
  sens = 'precedent';
  photoNormale();
  sens = 'suivant';
  return false;
}
function photoSuivante() {
  photoPause();
  photoContinu();
  photoNormale();
  return false;
}
function photoPause() {
  clearInterval(idInterval);
  var photoBouton;
  photoBouton = document.getElementById('p-pause');
  photoBouton.style.display = 'none';
  photoBouton = document.getElementById('p-lecture');
  photoBouton.style.display = 'inline';
  return false;
}
function photoLecture() {
  var photoBouton;
  photoBouton = document.getElementById('p-lecture');
  photoBouton.style.display = 'none';
  photoBouton = document.getElementById('p-pause');
  photoBouton.style.display = 'inline';
  photoNormale();
  idInterval = setInterval("photoNormale();", 10000);
  return false;
}
function photoContinu() {
  sens = 'suivant';
  var photoBouton;
  photoBouton = document.getElementById('p-continu');
  photoBouton.style.display = 'none';
  photoBouton = document.getElementById('p-aleatoire');
  photoBouton.style.display = 'inline';
  return false;
}
function photoAleatoire() {
  sens = 'aleatoire';
  var photoBouton;
  photoBouton = document.getElementById('p-aleatoire');
  photoBouton.style.display = 'none';
  photoBouton = document.getElementById('p-continu');
  photoBouton.style.display = 'inline';
  return false;
}

function lancePhoto(pGalerie) {
  galerie = pGalerie;
  var photoBouton;
  photoBouton = document.getElementById('p-pause');
  photoBouton.style.display = 'inline';
  photoBouton = document.getElementById('p-aleatoire');
  photoBouton.style.display = 'inline';
  idInterval = setInterval("photoNormale();", 10000);
}

