$(document).ready(function(){
  var currentPosition = 0;
  var slideWidth = 600;
  var slides = $('.slide');
  var numberOfSlides = slides.length;
  var grabbaTimer = 5000;
  var timer;
  timer = clearTimeout(timer);
  


  // 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 * numberOfSlides);

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

  // Hide left arrow control on first load
  manageControls(currentPosition);
  
//-------------------------
//-------------------------  
  
  // Play slider with timer

  timer = setTimeout(grabba_timer_fct, grabbaTimer);
 function grabba_timer_fct (){
	  grabbaTimer = 5000;
	  if(numberOfSlides == currentPosition+1){
		  currentPosition = 0;  
	  }
	  else{
		  currentPosition = currentPosition+1;
	  }		  
	  manageControls(currentPosition);
	  $('#slideInner').animate({
      'marginLeft' : slideWidth*(-currentPosition)
    });
   timer = setTimeout(grabba_timer_fct, grabbaTimer);  

	  //alert(numberOfSlides);
  }
//-------------------------    
//-------------------------    
  
  // Create event listeners for .controls clicks
  $('.control')
    .bind('click', function(){
    // Determine new position
	timer = clearTimeout(timer);
	
	currentPosition = ($(this).attr('id')=='rightControl') ? currentPosition+1 : currentPosition-1;
    grabbaTimer = 15000;
	timer = setTimeout(grabba_timer_fct, grabbaTimer);
	
	// Hide / show controls
    manageControls(currentPosition);
    // Move slideInner using margin-left
    $('#slideInner').animate({
      'marginLeft' : slideWidth*(-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() }
  }	
});


$(document).ready(function(){
  var currentPosition2 = 0;
  var slideWidth2 = 600;
  var slides2 = $('.slide2');
  var numberOfSlides2 = slides2.length;
  var grabbaTimer2 = 5000;
  var timer2;
  timer2 = clearTimeout(timer2);
  


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

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

  // Set #slideInner width equal to total width of all slides
  $('#slideInner2').css('width', slideWidth2 * numberOfSlides2);

  // Insert controls in the DOM
  $('#slideshow2')
    .prepend('<span class="control2" id="leftControl2">Clicking moves left</span>')
    .append('<span class="control2" id="rightControl2">Clicking moves right</span>');

  // Hide left arrow control on first load
  manageControls2(currentPosition2);
  
//-------------------------
//-------------------------  
  
  // Play slider with timer

  timer2 = setTimeout(grabba_timer_fct2, grabbaTimer2);
 function grabba_timer_fct2 (){
	  grabbaTimer2 = 5000;
	  if(numberOfSlides2 == currentPosition2+1){
		  currentPosition2 = 0;  
	  }
	  else{
		  currentPosition2 = currentPosition2+1;
	  }		  
	  manageControls2(currentPosition2);
	  $('#slideInner2').animate({
      'marginLeft' : slideWidth2*(-currentPosition2)
    });
   timer2 = setTimeout(grabba_timer_fct2, grabbaTimer2);  

	  //alert(numberOfSlides);
  }
//-------------------------    
//-------------------------    
  
  // Create event listeners for .controls clicks
  $('.control2')
    .bind('click', function(){
    // Determine new position
	timer2 = clearTimeout(timer2);
	
	currentPosition2 = ($(this).attr('id')=='rightControl2') ? currentPosition2+1 : currentPosition2-1;
    grabbaTimer2 = 15000;
	timer2 = setTimeout(grabba_timer_fct2, grabbaTimer2);
	
	// Hide / show controls
    manageControls2(currentPosition2);
    // Move slideInner using margin-left
    $('#slideInner2').animate({
      'marginLeft' : slideWidth2*(-currentPosition2)
    });
  });

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls2(position2){
    // Hide left arrow if position is first slide
	if(position2==0){ $('#leftControl2').hide() } else{ $('#leftControl2').show() }
	// Hide right arrow if position is last slide
    if(position2==numberOfSlides2-1){ $('#rightControl2').hide() } else{ $('#rightControl2').show() }
  }	
});

