/**
 * iTunes-Style Slider
 * 
 * @author  Kevin Thompson <http://kevinthompson.info>
 */
(function( $ ) {
  
  $.fn.slider = function( options ){
    
    // Default Settings
    var settings = {
      'gallery'     : '.gallery',
      'thumbs'      : '.thumbs',
      'duration'    : 2,
      'speed'       : 400
    };

    // Merge Options with Settings
    if (options) {
      $.extend(settings,options);
    }
        
    return this.each(function(){
      
      var $slider       = $(this);
      var $gallery      = $slider.find( settings.gallery );
      var $thumbs       = $slider.find( settings.thumbs );
      
      var cycle         = 0;
      var index         = 0;
      var images        = $gallery.find('img');
      var thumbnails    = $thumbs.find('img');
      var thumbHeight   = typeof( settings.thumbHeight ) == 'undefined' ? thumbnails.outerHeight() + parseInt(thumbnails.css('margin-top')) + parseInt(thumbnails.css('margin-bottom')) : settings.thumbHeight;
      var thumbsVisible = Math.ceil(($thumbs.outerHeight() - parseInt($thumbs.css('padding-top')) - parseInt($thumbs.css('padding-bottom'))) / thumbHeight);
      
      var initialMargin;
      var timer;
      
      // Prepare Slider
      $thumbs.html('');
      for (i = 0; i <= thumbnails.length;  i++)
      {
        $(thumbnails[i]).addClass('thumb-' + i).prependTo( $thumbs );
        $(images[i]).addClass('image-' + i);
      }
      
      // Duplicate Initial Elements for Inifinite Scroll
      for (i = 0; i <= thumbsVisible; i++)
      {
        var key = i < thumbnails.length ? i : i - thumbnails.length;
        $thumbs.prepend( $(thumbnails[key]).clone() );
      }
      
      // Define "Next" Behavior
      $slider.find('.next').click(function(event)
      { 
        event.preventDefault();
        slide();
      });
      
      // Show First Image
      show( index );
      
      // Set Slide Timer
      function setTimer()
      {
        clearTimeout( timer );
        timer = setTimeout( slide, (settings.duration * 1000) );
      }
      
      // Adjust Active Index
      function slide()
      {
        setTimer();
        if ( index < (thumbnails.length - 1) )
        {
          index++;
        }   
        else 
        { 
          cycle++;
          index = 0;
        }
        show( index );
      }
      
      // Reset Thumbnail Slide
      function reset()
      {
        $thumbs.css({
          'margin-top'  : initialMargin
        });
      }
    
      // Show Slide
      function show( index )
      {
        // Reset Slide Timer
        setTimer();
        
        // Determine Margin
        var thumbCount  = thumbnails.length + thumbsVisible + 1;
        var offset      = cycle > 0 && index == 0 ? thumbCount : index + thumbsVisible + 1;
        var margin      = ((thumbCount * thumbHeight) - (offset * thumbHeight)) * -1;
        
        if( cycle == 0 && index == 0 )
        {
          initialMargin = margin;
          reset();
        }
        
        // Fade Images
        $(images).stop().animate({
          opacity: 0
        });
        $('.image-' + index).stop().animate({
          opacity: 1
        }, settings.speed);
        
        // Slide Thumbnails
        $thumbs.stop().animate({
          'margin-top': margin
        }, settings.speed, function(){
          if( cycle > 0 && index == 0 )
          {
            reset();
          }
        });
      }
          
    });
  }
    
})( jQuery );
