/*
 * This is a basic image rotate script.  It simply switches the images in a given pane.
 *
 * options: the options to use:
 *    {
 *       duration: the amount of time in milliseconds to show an image before showing the next image.
 *       fadeTime: the amount of time in milliseconds to perform the fade; if 0 no fade is used.
 *       maxOpacity: the amount to fade to, between 0 and 1; defaults to 1.
 *       minOpacity: the amount to fade to, between 0 and 1; defaults to 0.
 *       easing: the easing to use; defaults to "swing".
 *    }
 */
(function($) {
  $.extend($.fn, {
    slideshow : function(options) {
      options = $.extend({
        duration      : 10000,
        fadeTime      : 0,
        maxOpacity    : 1,
        minOpacity    : 0,
        random        : false,
        easing        : null
      }, options || {});
      return this.each(function() {
        var div = $(this);
        var images = $('img', div);
        var numImages = images.length;
        var prevIndex = -1;
        var index = 0;
        if (options.random) {
          index = randomIndex(-1, -1);
        }
        var first = $(images.get(index));
        images.css('opacity',options.minOpacity);
        images.hide();

        images.each(function() {
          var image = $(this);
          image.bind('contextmenu', function() {return false;});
          var imgSize = resizeImage(image, div);
          
          image.width(imgSize.width);
          image.height(imgSize.height);
          
          animation = {
            animate : false,
            start   : {top: '0px', left: '0px'},
            stop    : {}
          };
          var height = div.height();
          var width = div.width();

          if (imgSize.height > height && imgSize.width > width) {
            animation.animate = true;
            if (image.is('.nozoom')) {
              animation.start.top = '0px';
              animation.start.left = '0px';
              animation.stop.top = (height - imgSize.height) + 'px';
              animation.stop.left = (width - imgSize.width) + 'px';
            } else {
              animation.start.top = ((height - imgSize.height) / 2) + 'px';
              animation.start.left = ((width - imgSize.width) / 2) + 'px';
              animation.start.width = imgSize.width;
              animation.start.height = imgSize.height;
              var small = resizeImage(image, div, true);
              animation.stop.top = ((height - small.height) / 2) + 'px';
              animation.stop.left = ((width - small.width) / 2) + 'px';
              animation.stop.width = small.width;
              animation.stop.height = small.height;
            }
          } else if (imgSize.height > height) {
            animation.animate = true;
            animation.stop.top = (height - imgSize.height) + 'px';
          } else if (imgSize.width > width) {
            animation.animate = true;
            animation.stop.left = (width - imgSize.width) + 'px';
          }
          if (image.is('.reverse')) {
            var tmp = animation.stop;
            animation.stop = animation.start;
            animation.start = tmp;
          }
          image.data('animation', animation);
        });

        first.show();
        first.css('opacity', options.maxOpacity);
        animate(first);
        setInterval(flipImage, options.duration);

        function randomIndex(idx1, idx2) {
          var rand = Math.floor(Math.random() * numImages);
      
          while (rand == idx1 || rand == idx2) {
            rand = Math.floor(Math.random() * numImages);
          }
          return rand;
        };

        function flipImage() {
          var flipTime = options.fadeTime;
          var vis = $(images.get(index));
          if (options.random) {
            var idx = randomIndex(index, prevIndex);
            prevIndex = index;
            index = idx;
          } else {
            prevIndex = index;
            index++;
            if (index == images.size()) { index = 0; }
          }

          var next = $(images.get(index));
         
          var animation = next.data('animation');
          next.css(animation.start);
          next.show();

          vis.animate({opacity:options.minOpacity}, {queue: false, duration: flipTime, complete: function() { $(this).hide(); }});
          next.animate({opacity:options.maxOpacity}, {queue: false, duration: flipTime});
          animate(next);
        };
        
        function animate(image) {
          div.addClass('running');
          var animation = image.data('animation');
          var animationTime = options.duration + options.fadeTime;
          if (animation.animate) {
            image.animate(animation.stop, {queue: false, duration: animationTime, easing: options.easing});
          }
        };

        function resizeImage(img, panel, force) {
          var pw = panel.width();
          var ph = panel.height();
          var iw = img.width();
          var ih = img.height();
  
          if (pw > iw || ph > ih || force) {
            var nw = (ph / ih) * iw;
            var nh = ph;
            if (nw < pw) {
              nh = (pw / iw) * ih;
              nw = pw;
            }
            return {width: nw, height: nh};
          }
          return {width: iw, height: ih};
        };
      });
    }
  });
})(jQuery);

