/*
	jquery.imageRotator.js
*/
/**
 * jQuery.imageRotator plugin
 *
 * jQuery.imageRotator displaying a set of images, one at a time. It
 * is implemented using the guidelines for implementing a jQuery
 * plugin as outlined in the following document:
 * http://docs.jquery.com/Plugins/Authoring
 *
 *   Usage: $(<selector>).imageRotator('method', args, ...);
 *
 *  jQuery.imageSlider contains the following methods:
 *    - 'init'
 *    when calling init, the 2nd argument is an object literal with options that
 *    customize the behavior of the rotator. There are no workable default options.
 *    See the example for details on the options.
 *
 *   - 'pause'
 *   Stops the rotator on the current image.
 *
 *   - 'unpause'
 *   If the rotator has been paused, calling unpause will
 *   resume play from the current image.
 *
 *   - 'play'
 *   Causes the rotator to begin cycling images from the current index.
 *
 * @fileOverview This file contains the jQuery.imageRotator plugin
 * @author <a href="mailto:kevin.m@otwmg.com">William Kevin Manire</a>
 * @version 1
 */

/**
 * @example
 * // first initialize an image with the init method
 * var rotatorImg = $('#rotator').imageRotator(
 *   'init',
 *   {
 *     // The images array is actually populated with object literals.
 *     // Each object must have a URL property. Any other data may be added
 *     // and that data will be made available as an argument to the
 *     // imageRotator.imagePlay event.
 *     images: [
 *       {URL: 'img1.jpeg', caption: 'first image'},
 *       {URL: 'img2.jpeg', caption: 'second image'},
 *       {URL: 'img3.jpeg', caption: 'third image'},
 *       {URL: 'img4.jpeg', caption: 'fourth image'}
 *     ],
 *
 *     // Show each image for 5 full seconds
 *     delay: 5000
 *   }
 * ) // start up the rotator
 * .imageRotator('play');
 *
 * // When element image4 is clicked, the rotator will immediately display the image at index 4
 * $('#image4').click(function () { rotatorImg.imageRotator('gotoIndex', 4); });
 */
(

function ($) {
  var
  methods = {
    'init': function (options) {
      return this.each(function (i, n) {
        var data = {
          images: options.images,
          index: -1,
          timerId: 0,
          paused: false,
          animating: true,
          delay: options.delay,
          fadeInDelay: options.fadeInDelay,
          fadeOutDelay: options.fadeOutDelay
        };

        $(n).data('imageRotator', data);
      });
    },

    'fadeInDelay': function(delay) {
      var data = this.data('imageRotator');
      if (typeof delay !== 'undefined') {
        data.fadeInDelay = delay;
        return this;
      }
      return data.fadeInDelay;
    },

    'fadeOutDelay': function(delay) {
      var data = this.data('imageRotator');
      if (typeof delay !== 'undefined') {
        data.fadeOutDelay = delay;
        return this;
      }
      return data.fadeOutDelay;
    },

    'pause': function () {
      return this.each(function (i, n) {
        var data = $(n).data('imageRotator');
        data.paused = true;
        clearTimeout(data.timerId);
        $(n).trigger('imageRotator.pause', data.images[data.index]);
      });
    },

    'isAnimating': function () {
      return $(this).data('imageRotator').animating;
    },

    'isPaused': function () {
      return $(this).data('imageRotator').paused;
    },

    'unpause': function () {
      return this.each(function (i, n) {
        var data = $(n).data('imageRotator');
        data.paused = false;
        $(n).trigger('imageRotator.unpause', data.images[data.index]);

        clearTimeout(data.timerId);
        data.timerId = setTimeout(function () {
          $(n).imageRotator('play');
        }, data.delay * 0.3);
      });
    },

    'gotoIndex': function (idx) {
      return this.each(function (i, n) {
        var data = $(n).data('imageRotator');
        if (idx < data.images.length && idx >= 0) {
          data.paused = true;
          data.index = idx - 1;
          clearTimeout(data.timerId);
          data.paused = false;
          $(n).imageRotator('play');
        }
      });
    },

    'play': function () {
      return $(this).each(function (i, n) {
        var data = $(n).data('imageRotator');
        clearTimeout(data.timerId);
        if (data.paused === false) {
          data.animating = true;

          data.index += 1;
          if (data.index === data.images.length) {
            data.index = 0;
          }
          $(n).fadeOut(data.fadeOutDelay);
          $.when($(n)).done(function () {
            $(n).trigger('imageRotator.transitioning', data.images[data.index]);
            $(n).attr('src', data.images[data.index].URL);
            $(n).attr('alt', data.images[data.index].URL);

            $(n).fadeIn(data.fadeInDelay);

            $.when($(n)).done(function () {
              $(n).trigger('imageRotator.play', data.images[data.index]);

              data.timerId = setTimeout(function () {
                methods.play.call(n);
              }, data.delay);

              $(this).css('opacity', 1);
              data.animating = false;
            });
          });
        }
      });
    }
  };

  $.fn.imageRotator = function (method, options) {
    if (methods[method]) {
      return methods[method].apply(
      this, Array.prototype.slice.call(arguments, 1));
    } else {
      $.error('Method ' + method + ' does not exist on jQuery.imageRotator');
      return undefined;
    }
  };

})(jQuery);


