jQuery(document).ready(function () {

  // Diaporama réalisations
  if (jQuery('.gal-img li').length > 0) {
    var $timer = 5000;

    // Click sur les numéros
    jQuery('.gal-num li.link').click(function () {
      var $this = jQuery(this);
      if (!$this.hasClass('current'))
        slide_num(jQuery('.gal-num li.link').index($this));
    });

    // Click sur les liens précédent / suivant
    jQuery('#gallery .img-left').click(function () { slide(-1); });
    jQuery('#gallery .img-right').click(function () { slide(1); });

    // Défilement automatique des images toutes les $timer ms
    image_timer = setInterval("slide(1)", $timer);
    jQuery('.gal-num li.link, #gallery .img-left, #gallery .img-right').click(function () {
      clearInterval(image_timer);
      jQuery('.map-img').fadeOut('slow', function () { jQuery('.map-img').removeClass('current'); });
      image_timer = setInterval("slide(1)", $timer);
    });

    // Affichage de la map (arrêt du défilement automatique)
    jQuery('.gal-num li.map').click(function () {
      clearInterval(image_timer);
      jQuery('.gal-num .current').removeClass('current');
      jQuery('.gal-num li.map').addClass('current');
      jQuery('#map.map-img').removeClass('init');

      jQuery('.gal-img li.current').fadeOut('slow', function () { jQuery('.gal-img li.current').removeClass('current'); });
      jQuery('.map-img').fadeIn('slow', function () { jQuery('.map-img').addClass('current'); });
    });

    // GMap
    if (jQuery('#map.map-img').length > 0) {
      window.onunload = function () { GUnload(); };
      loadcoord($('#lat').val(), $('#lng').val());
    }
  }
});

// Fonction qui charge la map avec les cooronnées données
function loadcoord($lat, $lng) {
  if (GBrowserIsCompatible()) {
    var map = new GMap2(document.getElementById("map"));
    map.setCenter(new GLatLng($lat, $lng), 16);
    var marker = new GMarker(new GLatLng($lat, $lng), { draggable: false });
    map.addOverlay(marker);
    map.setMapType(G_SATELLITE_MAP);
    map.setUIToDefault();
  }
}

// Affiche une image par rapport à son index
function slide_num($index) {
  var current_slide = jQuery('.gal-img li.current');
  var next_slide = jQuery('.gal-img li').eq($index);
  var current_num = jQuery('.gal-num .current');
  var next_num = jQuery('.gal-num li.link').eq($index);

  next_num.siblings().removeClass('current');
  next_num.addClass('current');
  current_slide.fadeOut('slow', function () { jQuery('.gal-img li.current').removeClass('current'); });
  next_slide.fadeIn('slow', function () { next_slide.addClass('current'); });
}

// Affiche l'image précédente ou suivante (nombre négatif pour la précédente, positif pour la suivante)
function slide($direction) {
  var current_slide = jQuery('.gal-img li.current');
  var current_num = jQuery('.gal-num .current');
  var next_slide;
  var next_num;
  if ($direction > 0) {
    next_slide = current_slide.next('li');
    next_num = current_num.next('li.link');
    if (next_slide.length <= 0) {
      next_slide = jQuery('.gal-img li:first');
      next_num = jQuery('.gal-num li.link:first');
    }
  }
  else {
    next_slide = current_slide.prev('li');
    next_num = current_num.prev('li.link');
    if (next_slide.length <= 0) {
      next_slide = jQuery('.gal-img li:last');
      next_num = jQuery('.gal-num li.link:last');
    }
  }

  current_slide.fadeOut('slow', function () {
    jQuery('.gal-img li.current').removeClass('current');
    next_num.siblings().removeClass('current');
  });

  next_slide.fadeIn('slow', function () {
    next_slide.addClass('current');
    next_num.addClass('current');
  });
}
