﻿$(document).ready(function(){
    var currentPosition = 0;
    var slideWidth = 869;
    var slides = $('.featuredslide');
    var numberOfSlides = slides.length;
    $('#featuredcontainer').css('overflow', 'hidden');
    slides
    .css('overflow', 'hidden')
    .wrapAll('<div id="slideFeathureInner"></div>')
  // Float left to display horizontally, readjust .slides width
      .css({
        'float' : 'left',
        'width' : slideWidth
      });
    $('#slideFeathureInner').css('width', slideWidth * numberOfSlides);
    $('#feauredshow')
    .prepend('<span class="FeatureControl" id="FeatureLeftControl">Moves left</span>')
    .append('<span class="FeatureControl" id="FeatureRightControl">Moves right</span>');
    function  manageControls(position){
      // position==0 is first slide
        if (position == 0) { $('#FeatureLeftControl').hide(); }
        else { $('#FeatureLeftControl').show(); }
        // numberOfSlides-1 is last slides
        if (position == numberOfSlides - 1) { $('#FeatureRightControl').hide(); }
        else { $('#FeatureRightControl').show(); }
}      
    // Hide left arrow control on first load
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
  $('.FeatureControl')
    .bind('click', function(){
    // Determine new position
        currentPosition = ($(this).attr('id') == 'FeatureRightControl')
    ? currentPosition+1 : currentPosition-1;

      // Hide / show controls
      manageControls(currentPosition);
      // Move slideInner using margin-left
      $('#slideFeathureInner').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) { $('#FeatureLeftControl').hide() }
    else { $('#FeatureLeftControl').show() }
    // Hide right arrow if position is last slide
    if (position == numberOfSlides - 1) { $('#FeatureRightControl').hide() }
    else { $('#FeatureRightControl').show() }
    }
  });

