
/* Mise à jour de l'aperçu
 */
function updateThumbnail(ref, n) {

  if (typeof(ref) == "undefined") return false;
  if (typeof(n) == "undefined") return false;

  var xhr = getXhr();

  if (! xhr) {

    return false;
  }

  xhr.onreadystatechange = function() {

    if (xhr.readyState == 4 && xhr.status == 200) {

      document.getElementById('car-gallery-thumbnail').innerHTML = xhr.responseText;
      refreshPicture(ref, n);
    }
  }

  xhr.open('GET', '/app/www/index.php?module=stock&action=show:refreshGallery&n=' + n + '&ref=' + ref);
  xhr.send(null);
  return true;
}


/* Mise à jour du lien zoom, et de l'image centrale
 */
function refreshPicture(ref, n) {

  if (typeof(ref) == "undefined") return false;
  if (typeof(n) == "undefined") return false;

  n = n - 1;

  // Remplace l'image du centre

  var a = document.createElement('a');

  var rel = document.createAttribute('rel');
  rel.value = 'lightbox';
  a.setAttributeNode(rel);
  
  var href = document.createAttribute('href');
  href.value = '/app/www/pictures/' + ref + '_' + n + '.bigger.jpg';
  a.setAttributeNode(href);

  var img = document.createElement('img');

  var src = document.createAttribute('src');
  src.value = '/app/www/pictures/' + ref + '_' + n + '.big.jpg';
  img.setAttributeNode(src);

  var id = document.createAttribute('id');
  id.value = 'car-picture-img';
  img.setAttributeNode(id);

  if (navigator.product == 'Gecko') {

    var oc = document.createAttribute('onclick');
    oc.value = 'initLightbox();';
    img.setAttributeNode(oc);
  }

  a.appendChild(img);

  var car_picture = document.getElementById('car-picture');

  while(car_picture.firstChild) {

    car_picture.removeChild(car_picture.firstChild);
  }

  car_picture.appendChild(a);


  // Remplace le href du lien de l'icone zoom

  var zoom = document.getElementById('car-zoom-link');

  var attr_href = zoom.getAttributeNode('href');
  zoom.removeAttributeNode(attr_href);

  var new_href = document.createAttribute('href');
  new_href.value = '/app/www/pictures/' + ref + '_' + n + '.bigger.jpg';

  zoom.setAttributeNode(new_href);


  // Supprime le onclick de l'image
  /*var im = zoom.firstChild;
  var attr_onclick = im.getAttributeNode('onclick');
  im.removeAttributeNode(attr_onclick);*/


  // Re-init de lightbox
  if (navigator.product != 'Gecko') {

    initLightbox();
  }

  return true;
}


