// JavaScript Document
$(document).ready(function(){
  var currentPosition = 0;
  var testslideWidth = 280;
  var testslides = $('.testslide');
  var numberOftestslides = testslides.length;

  // Remove scrollbar in JS
  $('#testmonialContainer').css('overflow', 'hidden');

  // Wrap all .testslides with #testslideInner div
  testslides
    .wrapAll('<div id="testslideInner"></div>')
    // Float left to display horizontally, readjust .testslides width
	.css({
      'float' : 'left',
      'width' : testslideWidth
    });

  // Set #testslideInner width equal to total width of all testslides
  $('#testslideInner').css('width', testslideWidth * numberOftestslides);

  // Insert controls in the DOM
  $('#testmonial')
    .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);

  // 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 testslideInner using margin-left
    $('#testslideInner').animate({
      'marginLeft' : testslideWidth*(-currentPosition)
    });
  });

  // manageControls: Hides and Shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first testslide
	if(position==0){ $('#leftControl').hide() } else{ $('#leftControl').show() }
	// Hide right arrow if position is last testslide
    if(position==numberOftestslides-1){ $('#rightControl').hide() } else{ $('#rightControl').show() }
  }	
});

