/**
 * Wrapping Gallery jQuery Script
 *
 * This jQuery plugin lets us animate ul/li/img collections as a never-ending
 * gallery. li elements which have moved off the visible area are wrapped back
 * onto the end of the ul element.
 *
 * @version $Revision$ / $Date$
 *
 */
$(document).ready(function() {

   /**
    * First "basic" plugin - slide the li elements left, wrapping them around.
    * Uses a linear animation, so the movement doesn't stop.
    *
    * @todo Cheats a little; if an li element is less than anim.left, then
    * the animation will fall over at some point. The wrapping code should do
    * some sort of while loop to figure out how many elements need shifting.
    *
    */
   linearSlide = function(options) {

      // set the default options
      options = jQuery.extend({
         easing:'linear',
         speed:1000
      }, options);

      // only supporting left movement for the immediate future
      switch (options.direction) {
         case 'left':
            var anim = {left:'-=50px'};
            break;
      }

      // animate
      $(this).animate(anim, options.speed, options.easing, function(){

         // calculate if we need to wrap li elements around to the end
         var distanceTravelled = parseInt($(this).css('left'));
         var firstElemWidth = $(this).children('li').eq(0).width();
         if (firstElemWidth < Math.abs(distanceTravelled)) {
            $(this).append($(this).children('li').eq(0));
            // reposition the ul to compensate for the lost 0th element
            $(this).css('left',distanceTravelled+firstElemWidth);
         }
         // repeat animation
         linearSlide.apply($(this), [options]);
      });

   };


   /**
    * Swing over one element at a time, wrapping the last element over to the
    * end of the UL.
    *
    */
   swingElement = function(options) {

      options = jQuery.extend({
         pause:2500,
         easing:'swing'
      }, options);

      switch (options.direction) {
         case 'up':
            var shift = $(this).children('li').eq(1).position().top;
            var anim = {top:'-='+shift+'px'};
            var cssDimension = 'top';
            break;
         case 'left':
            var shift = $(this).children('li').eq(1).position().left;
            var anim = {left:'-='+shift+'px'};
            var cssDimension = 'left';
            break;
      }
      $(this).animate(anim,options.speed,options.easing,function(){
         $(this).append($(this).children('li').eq(0));
         $(this).css(cssDimension,0);
         var _this = $(this);
         setTimeout(function(){swingElement.apply(_this, [options])}, options.pause);
      });
   };


   jQuery.fn.wrappingGallery = function(options){

      // only do the wrapping gallery if we have more than 'n' images
      var totalWidth = 0;
      $(this).children('li').each(function(){totalWidth+=$(this).width()});
      if (totalWidth < $(this).parent().width()) {
         return;
      }

      options = jQuery.extend({
         animFunction:swingElement,
         direction: 'left',
         speed:1000
		}, options);
      options.animFunction.apply($(this), [options]);
   };

});
