
/*
 * jQuery iSlider plugin 1.0
 *
 * This is the infinite jQuery slider. After a thourough search of the web
 * for an infinite sliding list, i decided to createone myself.
 *
 * Usage: 
 *  http://www.frankbr.nl/projects/slider/demo.html
 *
 * Copyright (c) 2009 Frank Broersen
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 */
(function($){

  $.fn.Slider = function() {

    /*
     * internal settings
     */
    var internal = {
      holder: null,
      remainder: 0
    }

    return this.each(function() {

      init( $(this) );

      /*
       * init
       * setup the slider and play it
       */
      function init( el ) {

        var width = 10000; 

        if( el.attr('id') == '' ) {
          el.attr('id','slider' + Math.floor( Math.random() * 99 ));
        }

        el.css({
          overflow: 'hidden',
          position: 'relative'
        });

        el.html('<div id="innerSlider" style="position: absolute;">' + el.html() + '</div>');

        internal.holder = $('#innerSlider');

        internal.holder.css({
          left: '0',
          width: width + 'px'
        });

        // start the slider
        play();

        // pause on hover, start on stophover
        el.hover(function () {
          pause();
        },function () {
          play();
        });

      }

      /*
       * loop
       */
      function loop() {
        play();
      }

      /*
       * Pause the slider
       */
      function pause() {
        internal.remainder = $('#innerSlider').find('a:first').outerWidth( true ) - parseInt( $('#innerSlider').css('left').replace('-','').replace('px','') );
        internal.holder.stop();
      }

      /*
       * Play the slider
       */
      function play() {
        var size = $('#innerSlider').find('a:first').outerWidth( true );
        var duration = Math.round( ( size * 1000 ) / 100 );
        if( internal.remainder > 0 ) {
          duration = Math.round( ( internal.remainder * duration ) / size );
          internal.remainder = 0;
        }
        internal.holder.animate({
          left: '-' + size + 'px'
        },duration,'linear',function() {
          internal.holder.css('left',0);
          $('#innerSlider a:last').after( $('#innerSlider a:first') );
          loop();
        });
      }

    });
    
  }
})(jQuery);