$(document).ready(function(){
  var currentPosition = 0;
  var slideWidth = 600;
  var slides = $('.slide');
  var numberOfSlides = 4;

  // Remove scrollbar in JS
  $('#filmSlider').css('overflow', 'hidden');

 

  // Set #slideInner width equal to total width of all slides
  $('#slider').css('width', slideWidth * numberOfSlides);

  // Insert controls in the DOM
  $('#filmSlider')
    .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);
  buttonAnimation();
  
  // Create event listeners for .controls clicks
  $('.control')
    .bind('click', buttonAnimation);
  
  function buttonAnimation(){
	    // Determine new position
		 
	  	
		if($(this).attr('id')=='leftControl'){
			currentPosition = currentPosition-2;
			speed = 500;
			 $('#slider').stop(1);
		}else if($(this).attr('id')=='rightControl'){
			currentPosition;
			speed =500;
			 $('#slider').stop(1);
		}else{
			currentPosition = currentPosition+1;
			speed = 20000;
			
		}
		  
		if(currentPosition >= numberOfSlides){
			
	    	currentPosition = 0;
	    	speed = 500*numberOfSlides;
	    	
	    }else if(currentPosition < 0){
	    	currentPosition = 0;
	    }
		
	
		// Hide / show controls
	    manageControls(currentPosition);
	    
	   
	    // Move slideInner using margin-left
	    $('#slider').animate(
	    		{'marginLeft' : slideWidth*(-currentPosition)},
	    		speed,
	    		'linear',
	    		buttonAnimation
	    		
	    );
	    
	 };

  // 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() }
  }	
});
