/* 
jquery.nc.veloScroll.js
($'veloElements').jquery.nc.veloScroll()
Point veloScroll to a series of images and enable a scroller animation
that is activated by a slider
*/
(function ($) {
   var methods = {
      animEngine : function (animVal) { // speed
         var $this = $(this);
         if (animVal === 0) {
            $this.data('isRunning', false);
            return;
         }
         $this.data('speed', Math.floor(Math.abs(1 / animVal) * 1000));
         $this.data('increment', 4 * ((animVal < 0) ? 1 : -1));
         if ($this.data('isRunning') === false) {
            $this.data('isRunning', true);
            engine();
         }
         function engine() {
            $.each($this.data('elements'), function (i, v) {

               var edgeOffset = $(this).outerWidth(true);

               $(this).css("left",  parseInt($(this).css("left"), 10) + $this.data('increment'));
               
               if (parseInt($(this).css("left"), 10) >= $this.data('length') - edgeOffset) { // right
                  $(this).css("left", -edgeOffset);
               } else if (parseInt($(this).css("left"), 10) <= (edgeOffset * -1)) { //left
                  $(this).css("left", $this.data('length') - edgeOffset); 
               }
            });
            if ($this.data('isRunning') === true) {
               setTimeout(engine, $this.data('speed'));
            }
         }
      },
      initSetup : function () {
         var $this, currentLeft;
         $this = $(this);
         currentLeft = 0;
         $.each($this.data('elements'), function (i, v) {
            $(v).css({
               "left" : function () {
                  return currentLeft;
               },
               "position": "absolute" 
            });
            currentLeft +=  $(v).outerWidth(true);
         });
         $this.data('length', currentLeft);
      },
      init : function (options) {
         return this.each(function () {
            var $this = $(this);

            $this.data('elements', $this.find("li.carousel-item"));
            $this.data('isRunning', false);
            $this.data('speed', 50);
            $this.data('carouselHolder', $this);
            $this.data('carousel',   $this.find('#carousel'));

            $(window).load(function () {
               methods.initSetup.call($this);
            });

         });
      }
   };

   $.fn.veloScroll = function (method) {
      if (methods[method]) {
         return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
      } else if (typeof method === 'object' || !method) {
         return methods.init.apply(this, arguments);
      } else {
         $.error('Method ' +  method + ' does not exist on veloScroll');
      }
   };
})(jQuery);

