$(document).ready(function(){
var currentPosition = 0;
var slideWidth = 300;   // swatch width
var tds = $('#slide td').length;
var numberOfSlides = (tds) -1;    

//window.alert(willy); //use this to find out how the math is working

  $("#slide").width(tds *300);
 
 $('#slidesContainer').css('overflow', 'hidden');
  

  $('#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);

  // 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
    $('#slide').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() }
  }	
});


