var currentActu = 1;
//var nbActu = 0;
var tempsAffichageActu = 5000;
var timeOutActus = null;

$(document).ready(function() {
  $( '#slides .thumbnail a' ).mouseover( function() {
    clearTimeout( timeOutActus );
    var id = $( this ).attr( 'id' );
    var i = id.substring( id.length - 1 , id.length );

    var currentElement = currentActu;
    currentActu = i;

    switchFromTo( currentElement , currentActu );
  } );

  $( '#slides .thumbnail a' ).mouseout( function() {
    timeOutActus = setTimeout( animateActulites , tempsAffichageActu );
  } );

  $( '#slides .thumbnail a' ).click( function() {
    goToCurrent();
  } );

  $( '#slides .fullsize' ).click( function() {
    goToCurrent();
  } );

  timeOutActus = setTimeout( animateActulites , tempsAffichageActu );
});

function animateActulites() {
  var currentElement = currentActu;
  currentActu++;
  if( currentActu > nbActu )
    currentActu = 1;

  switchFromTo( currentElement , currentActu );

  timeOutActus = setTimeout( animateActulites , tempsAffichageActu );
}

function switchFromTo( from , to ) {
  if(from != to) {
    $( '#slides .fullsize .element-' + from ).fadeOut(700);
    $( '#slides .fullsize .element-' + to ).fadeIn(700);
    $( '#slides .title .element-' + from ).hide();
    $( '#slides .title .element-' + to ).show();
    $( '#slides .subtitle .element-' + from ).hide();
    $( '#slides .subtitle .element-' + to ).show();
    $( '#slides .thumbnail a' ).removeClass( 'active' );
    $( '#slides .thumbnail a.element-link-' + to ).addClass( 'active' );
  }
}

function goToCurrent() {
  var link = $( '#slides .title .element-' + currentActu + ' a' ).attr( 'href' );
  if (link) document.location = link;
}

