(function ($) {
  var methods = {
    moveToIndex: function (index) {
      var slideshow = this,
          dots      = slideshow.find('.index-dots li'),
          slides,
          newOffset,
          settings  = slideshow.data('settings.slideshow'),
          timeoutId = slideshow.data('timeoutId.slideshow');

      homeResearchSlides = slideshow.find('.home_research_slide');

      if (slideshow.find('.home_research_slide').length > 0) {
        slides = slideshow.find('.home_research_slide');
      } else {
        slides = slideshow.find('.a-slot');
      }

      newOffset = 0;

      for (var i=0; i<index; i++) {
        newOffset += slides.eq(i).width();
      }

      dots.removeClass('selected');

      animateOptions = {};

      if (slides.length - 1 == index) {
        animateOptions.complete = function () {
          slideshow.data('currentIndex.slideshow', 0);
          slideshow.find('.a-slots').css({'margin-left': 0});
        };

        dots.eq(0).addClass('selected');

      } else {
        dots.eq(index).addClass('selected');
        slideshow.data('currentIndex.slideshow', index);
      }

      slideshow.find('.a-slots').animate({'margin-left': newOffset * -1}, animateOptions);

      if (settings.autoprogress) {
        window.clearTimeout(timeoutId);

        timeoutId = window.setTimeout(function () {
          methods.moveToNext.apply(slideshow);
        }, 5000);

        slideshow.data('timeoutId.slideshow', timeoutId);
      }
    },

    moveToNext: function () {
      var slideshow    = this,
          currentIndex = slideshow.data('currentIndex.slideshow'),
          numSlides    = slideshow.data('numSlides.slideshow'),
          newIndex     = currentIndex + 1;

      methods.moveToIndex.apply(slideshow, [newIndex]);
    },

    moveToPrevious: function () {
      var slideshow    = this,
          currentIndex = slideshow.data('currentIndex.slideshow'),
          numSlides    = slideshow.data('numSlides.slideshow'),
          newIndex     = currentIndex - 1;

      if (newIndex == -1) {
        newIndex = numSlides - 1;
      }

      methods.moveToIndex.apply(slideshow, [newIndex]);
    },

    buildButtons: function () {
      var slideshow = this,
          numSlides = slideshow.data('numSlides.slideshow'),
          nextBtn   = $('<div class="next-btn"/>'),
          prevBtn   = $('<div class="prev-btn"/>'),
          indexDots = $('<ol class="index-dots clearfix"/>'),
          i, indexDot;

      nextBtn.bind('click.slideshow', function () {
        methods.moveToNext.apply(slideshow);
      });

      prevBtn.bind('click.slideshow', function () {
        methods.moveToPrevious.apply(slideshow);
      });

      for (i=0; i<numSlides; i++) {
        indexDot = $('<li>')

        if (i === 0) {
          indexDot.addClass('selected');
        }

        indexDot.bind('click.slideshow', function () {
          var index = $(this).prevAll('li').length;
          methods.moveToIndex.apply(slideshow, [index]);
        });

        indexDots.append(indexDot);
      }

      slideshow.append(nextBtn, prevBtn, indexDots);
    }
  };

  $.fn.slideshow = function (options) {
    var settings = {
      autoprogress: false,
      buttons: true
    };

    if (typeof(options) !== 'undefined') {
      $.extend(settings, options);
    }

    return this.each(function () {
      var slideshow = $(this),
          slides,
          firstSlide,
          cloneOfFirst,
          numSlides = 0,
          timeoutId;

      if (slideshow.find('.a-area-home-slideshow').length) {
        settings.autoprogress = true;
      }

      slideshow.data('settings.slideshow', settings);
      slideshow.data('currentIndex.slideshow', 0);

      if (slideshow.find('.home_research_slide').length) {
        slides = slideshow.find('.home_research_slide');
      } else {
        slides = slideshow.find('.a-slot');
      }

      numSlides = slides.length;
      firstSlide = slides.first();
      cloneOfFirst = firstSlide.clone();

      cloneOfFirst.attr('id', '');
      cloneOfFirst.find('[id]').each(function() {
        $(this).attr('id', '');
      });

      firstSlide.parent().append(cloneOfFirst);

      slideshow.data('numSlides.slideshow', numSlides);

      methods.buildButtons.apply(slideshow);

      if (settings.autoprogress) {
        timeoutId = window.setTimeout(function () {
          methods.moveToNext.apply(slideshow);
        }, 5000);

        slideshow.data('timeoutId.slideshow', timeoutId);
      }
    });
  }
})(jQuery);
