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