/* ----------------------------------------------------------------------------
 * jQuery shuffle
 * Copyright (c) 2008 Ca-Phun Ung <caphun at yelotofu dot com>
 * Licensed under a MIT license
 * http://yelotofu.com/labs/jquery/snippets/shuffle/
 */
(function(a){a.fn.shuffle=function(){return this.each(function(){var b=a(this).children().clone(true);return(b.length)?a(this).html(a.shuffle(b)):this})};a.shuffle=function(c){for(var d,b,e=c.length;e;d=parseInt(Math.random()*e),b=c[--e],c[e]=c[d],c[d]=b){}return c}})(jQuery);
/* ----------------------------------------------------------------------------
*/

/* ----------------------------------------------------------------------------
 * Album interactions & diaporama
 * Copyright (C) 2012 Raphaël Bois /contact//raphael-bois_dot_com/
 * for warriors MIT license
 */
var isdesc_show_delayed = null;
var last_over_tm = null;
var diaporama_tm = null;
var keep_desc = false;
var diap_seq = [];

function show_el() {
  last_over_tm = null;
  var el = diaporama_tm ? $('.diap-next') : $('.last-enter');
  if (!el.hasClass('isdesc') && !keep_desc) {
    var desc = $('.isdesc');
    if (desc.css('visibility') == 'visible' && desc.css('display') == 'block') {
      desc.fadeOut(100, function () { $(this).css('display', 'none'); });
    }
  }
  if (el.css('visibility') == 'visible' && el.css('display') == 'block') {
    return;
  }
  var tm = keep_desc ? 1000 : 150;
  if (!el.hasClass('isdesc')) {
    var lshow = $('.last-show');
    lshow.css('z-index', 1);
    lshow.addClass('to-hide');
    lshow.removeClass('last-show');
    el.addClass('last-show');
    if (!el.length) {
      $('.to-hide').fadeOut(tm, function() {
        $(this).removeClass('to-hide');
        $(this).hide();
      });
    }
  }
  el.css({'display': 'none', 'visibility': 'visible', 'z-index': el.hasClass('isdesc')?20:10 });
  el.fadeIn(tm, function () {
    var e = $(this);
    if (!e.hasClass('isdesc')) {
      var h = $('.to-hide');
      h.removeClass('to-hide');
      h.hide();
    }
  });
}

function diaporama_step(init) {
  var diap_tm = diaporama_tm;
  var dnc = "diap-next";
  if ($('last-enter').length) {
    if (diap_tm) clearTimeout(diap_tm);
    diap_tm = null;
  } else {
    var dseq = diap_seq;
    if (init) {
      keep_desc = true;
      if (diap_tm) clearTimeout(diap_tm);
      dseq = [];
    } else {
      show_el();
    }
    $('.'+dnc).removeClass(dnc);
    if (dseq.length == 0) {
      var lshow = $('.last-show');
      var imgs = $('.image_projet');
      for (var i = 0; i < imgs.length; i++) {
        if (!lshow || lshow[0] != imgs[i] && !$(imgs[i]).hasClass('isdesc')) {
          dseq.push(imgs[i]);
        }
      }
      if (lshow) {
        dseq.push('.nomatch');
      }
      $.shuffle(dseq);
    }
    if (dseq.length) {
      var n = dseq.pop();
      $(n).addClass(dnc);
    }
    diap_seq = dseq;
    diap_tm = setTimeout(diaporama_step, 5000);
  }
  diaporama_tm = diap_tm;
}

function enter_el(_el) {
  var el = $(_el);
  var lec = "last-enter";
  if (isdesc_show_delayed) {
    clearTimeout(isdesc_show_delayed);
    isdesc_show_delayed = null;
  }
  keep_desc = false;
  if (diaporama_tm) {
    clearTimeout(diaporama_tm);
    diaporama_tm = null;
  }
  if (el.hasClass(lec)) {
    return;
  }
  el.addClass(lec);
  if (last_over_tm) {
    clearTimeout(last_over_tm);
  }
  last_over_tm = setTimeout(show_el, 150);
}

function leave_el(_el) {
  var lec = "last-enter";
  var el = $(_el);
  el.removeClass(lec);
  if (last_over_tm && $('.'+lec).length == 0) {
    clearTimeout(last_over_tm);
  }
  if (!$('.'+lec).length) {
    diaporama_step(true);
  }
}

var _laniere_over = null;
var _laniere_tm = null;
var _laniere_hidding = null;
var _lanieres_list = [];
var _lanieres_unused = [];
var _lanieres_replace = [];
function lanieres_refresh_unused() {
  _lanieres_unused = [];
  var els = $('.laniere a');
  var ret, i, j, fa;
  for (i = 0; i < _lanieres_list.length; i++) {
    fa = $('<a/>', { href: _lanieres_list[i].fiche });
    ret = true;
    for (j = 0; j < els.length; j++) {
      if ($(els[j]).prop('href') == fa.prop('href')) {
        ret = false;
        break;
      }
    }
    fa.remove();
    if (ret) _lanieres_unused.push(_lanieres_list[i]);
  }
}

function laniere_next() {
  var a, img, obj;
  $.shuffle(_lanieres_unused);
  obj = _lanieres_unused.pop();
  a = $('<a/>', { title: obj.title, href: obj.fiche });
  img = $('<img/>', { src: obj.image, alt: obj.title });
  img.appendTo(a);
  a.appendTo($('#laniere-next'));
  _laniere_tm = setTimeout(function () {
    var i, el;
    do {
      if (_lanieres_replace.length == 0) {
        $('.laniere').each(function () {
          _lanieres_replace.push(this);
        });
        $.shuffle(_lanieres_replace);
      }
      el = _lanieres_replace.pop();
    } while (_laniere_over == el);
    var fad = $('a', el);
    $('#laniere-next a').prependTo(el);
    _laniere_hidding = fad;
    fad.fadeOut(500, function () {
      _lanieres_unused.push({
        fiche: fad.prop('href'),
        title: fad.prop('title'),
        image: $('img', fad).prop('src')
      });
      _laniere_hidding.remove();
      _laniere_hidding = null;
      laniere_next();
    });
    _laniere_tm = null;
  }, 2500);
}

/* Trick for offline use to avoid Origin issue with file protocol. */
function lanieres_setup(data, dataonly) {
  _lanieres_list = data;
  if (!dataonly) {
    lanieres_refresh_unused();
    if (_lanieres_unused.length) {
      laniere_next();
    }
  }
}

function init_lanieres(list_url) {
  $('.laniere').mouseenter(function () {
    _laniere_over = this;
  }).mouseleave(function () {
    if (_laniere_over == this) {
      _laniere_over = null;
    }
  });
  $('#contenu').prepend($('<div/>', { id: 'laniere-next' }));
  if (window.location.protocol == 'file:') {
    setTimeout(function(){lanieres_setup(_lanieres_list);}, 10);
  } else {
    $.getJSON(list_url, function(data){lanieres_setup(data);});
  }
}

jQuery(function () {
  if (!jQuery) return;
  var vignettes = $('.vignette');
  if (!vignettes.length) return;
  desc = $('.isdesc');
  jQuery.fx.interval = 40;
  vignettes.css('z-index', 1);
  vignettes.mouseenter(function () {
    var m = this.id.match(/^vig([0-9]+)$/);
    var id = m ? "#img"+m[1] : '.nomatch';
    enter_el(id);
  }).mouseleave(function () {
    var m = this.id.match(/^vig([0-9]+)$/);
    var id = m ? "#img"+m[1] : '.nomatch';
    leave_el(id);
  });
  desc.css('z-index', 20);
  /* isdesc_show_delayed = setTimeout(function () {
    var el = desc;
    el.css('display', 'none');
    el.css('visibility', 'visible');
    el.fadeIn(3000);
    isdesc_show_delayed = null;
  }, 3000); */
  diaporama_step(true);  
});

