/*!
 * @copyright 2010-Present Advanced Care Solutions, Inc.
 * @author Christopher Rahauiser <crahauiser@acs-web.com>
 */
(function($) {
  $.fn.customizableSlideShow = function(options) {
    var opts = $.extend({}, $.fn.customizableSlideShow.defaults, options);

    return this.each(function() {
      var o = $.meta ? $.extend({}, opts, $(this).data()) : opts;

      var children = $(this).children();
      if (children.length < 2) {
        return;
      }

      opts.childrenSetup(children);

      var current = 0,
          next,
          timer = null,
          animating = false,
          playPause;

      function animateNext() {
        if (animating) {
          return;
        }
        animating = true;

        next = current + 1;
        if (next == children.length) {
          next = 0;
        }

        o.beforeNextAnimation(children.eq(next), children.eq(current));

        children
          .eq(next)
            .animate(
              o.animateInNextProperties,
              o.animateInNextDuration,
              o.animateInNextEasing,
              o.animateInNextCallback
            )
          .end()
          .eq(current)
            .animate(
              o.animateOutNextProperties,
              o.animateOutNextDuration,
              o.animateOutNextEasing,
              function() {
                o.animateOutNextCallback.call(this); // set this to dom element
                animating = false;
              }
            );
        current = next;
      }

      function animatePrevious() {
        if (animating) {
          return;
        }
        animating = true;

        next = current - 1;
        if (next < 0) {
          next = children.length - 1;
        }

        o.beforePreviousAnimation(children.eq(next), children.eq(current));

        children
          .eq(next)
            .animate(
              o.animateInPreviousProperties,
              o.animateInPreviousDuration,
              o.animateInPreviousEasing,
              o.animateInPreviousCallback
            )
          .end()
          .eq(current)
            .animate(
              o.animateOutPreviousProperties,
              o.animateOutPreviousDuration,
              o.animateOutPreviousEasing,
              function() {
                o.animateOutPreviousCallback.call(this); // set this to dom element
                animating = false;
              }
            );
        current = next;
      }

      function play() {
        if (playPause.length > 0) {
          playPause
            .addClass(o.pauseClass)
            .removeClass(o.playClass);
        }
        timer = setInterval(animateNext, o.triggerDuration);
      }

      function pause() {
        clearInterval(timer);
        timer = null;
      }

      $(o.nextSelector)
        .show()
        .click(function(event) {
          event.preventDefault();
          pause();
          animateNext();
          play();
        });
      $(o.previousSelector)
        .show()
        .click(function(event) {
          event.preventDefault();
          pause();
          animatePrevious();
          play();
        });

      playPause =
          $(o.playPauseSelector)
            .show()
            .click(function() {
              if (playPause.hasClass(o.pauseClass)) {
                pause();
                playPause
                  .addClass(o.playClass)
                  .removeClass(o.pauseClass);
              } else {
                play();
              }
            });

      play();
    });
  };

  $.fn.customizableSlideShow.defaults = {
    triggerDuration: 6000,
    beforeNextAnimation: function(animateInObj, animateOutObj) { },
    beforePreviousAnimation: function(animateInObj, animateOutObj) { },
    animateInNextEasing: 'swing',
    animateInNextDuration: 1200,
    animateInNextProperties: { opacity: 'toggle' },
    animateInNextCallback: function() {
      $(this).css('z-index', 2);
    },
    animateOutNextEasing: 'swing',
    animateOutNextDuration: 1200,
    animateOutNextProperties: { opacity: 'toggle' },
    animateOutNextCallback: function() {
      $(this).css('z-index', 1);
    },
    animateInPreviousEasing: 'swing',
    animateInPreviousDuration: 1200,
    animateInPreviousProperties: { opacity: 'toggle' },
    animateInPreviousCallback: function() {
      $(this).css('z-index', 2);
    },
    animateOutPreviousEasing: 'swing',
    animateOutPreviousDuration: 1200,
    animateOutPreviousProperties: { opacity: 'toggle' },
    animateOutPeviousCallback: function() {
      $(this).css('z-index', 1);
    },
    nextSelector: '.customizable-slideshow-next',
    previousSelector: '.customizable-slideshow-previous',
    playPauseSelector: '.customizable-slideshow-play-pause',
    playClass: 'customizable-slideshow-play',
    pauseClass: 'customizable-slideshow-pause',
    childrenSetup: function(children) { }
  };
})(jQuery);
