var marges = 0;
var currentPosition = 0;
var slideWidth = 980;
var slides = $('.slide');
var numberOfSlides = slides.length;
$(document).ready(function (){
  currentPosition = 0;
  slideWidth = 980;
  //alert(document.body.clientWidth);
  marges = (document.body.clientWidth)-slideWidth;
  //alert(marges);
  slides = $('.slide');
  numberOfSlides = slides.length;
  
  $('#slidesContainer').css('width', document.body.clientWidth);
  
  // Redefinition des marges en fonction de la taille de l ecran
  $('.slide').css('margin-left', (marges)/2);
  $('.slide').css('margin-right', (marges)/2);
  $('.slide').css('margin-top', (document.body.clientHeight-550)/2);

  // Remove scrollbar in JS
  $('#slidesContainer').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  slides
  .wrapAll('<div id="slideInner"></div>')
  // Float left to display horizontally, readjust .slides width
  .css({
    'float' : 'left',
    'width' : slideWidth
  });

  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', ((slideWidth+marges) * numberOfSlides)+500);

  // Insert left and right arrow controls in the DOM
  $('#slideshow')
    .prepend('<span class="control" id="leftControl">Move left</span>')
    .append('<span class="control" id="rightControl">Move right</span>');

  // Hide left arrow control on first load
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
  $('.control')
    .bind('click', function(){
    // Determine new position
      currentPosition = ($(this).attr('id')=='rightControl')
    ? currentPosition+1 : currentPosition-1;

      // Hide / show controls
      manageControls(currentPosition);
      // Move slideInner using margin-left
      $('#slideInner').animate({
        'marginLeft' : (slideWidth+marges)*(-currentPosition)
      });
      //alert(slideWidth+marges)*(-currentPosition);
    });

  // manageControls: Hides and shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
    if(position==0){ $('#leftControl').hide() }
    else{ $('#leftControl').show() }
    // Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ $('#rightControl').hide() }
    else{ $('#rightControl').show() }
    }
  });
  

function rez()
{
 window.onresize = resizeWin;
}

function resizeWin()
{
marges = (document.body.clientWidth)-980;
//alert(marges);
  $('#slidesContainer').css('width', document.body.clientWidth);
  
  // Redefinition des marges en fonction de la taille de l ecran
  $('.slide').css('margin-left', Math.round((marges)/2));
  $('.slide').css('margin-right', Math.round((marges)/2));
  $('.slide').css('margin-top', (document.body.clientHeight-550)/2);
  
  slides = $('.slide');
  numberOfSlides = slides.length;
  // Set #slideInner width equal to total width of all slides
  $('#slideInner').css('width', ((slideWidth+marges) * numberOfSlides)+500);

  
 //alert('taille gen : '+document.body.clientWidth+' / marge g : '+(marges)/2+' / marge d : '+(marges)/2+' / total : '+(710+marges)+' / nbre slide : '+numberOfSlides+' / taille slideinner : '+((slideWidth+marges) * numberOfSlides));
  //alert((marges)/2);
  
}
