/*
 * jQuery horizonal scroller
*/


(function($) {
  $.fn.extend({
    horizScroller: function(options) {
      
      return this.each(function(i) {
        
        var width = $(this).children(options.jq_item).outerWidth();
        var x = 0;
          
        $(this).children(options.jq_item)
          .css('position', 'absolute')
          .css('top', '0px')
          .each(function(){
            $(this).css('left', x);
            x += width;
          })
          ;
        
        var $container = $(this);
        
        var total_width = x - width;
        
        // If there isn't enough sliders, nuke the buttons
        if (typeof(options.num) != 'undefined') {
          if ($container.children().length <= options.num) {
            $(options.jq_next).css('visibility', 'hidden');
            $(options.jq_prev).css('visibility', 'hidden');
            return true;
          }
        }
        
        // Next arrow
        $(options.jq_next)
          .click(function() {
            $container.children(options.jq_item).stop(false, true);
            
            var last = $container.children(options.jq_item + ':last');
            
            if (last.position().left < total_width) {
              $container.children(options.jq_item).eq(0)
                .insertAfter(last)
                .css('left', (total_width) + 'px')
                ;
            }
            
            $container.children(options.jq_item).animate({
              left: '-=' + width + 'px'
            });
            
            return false;
          })
          ;
        
        // Previous arrow
        $(options.jq_prev)
          .click(function(){
            $container.children(options.jq_item).stop(false, true);
            
            var first = $container.children(options.jq_item).eq(0);
            
            if (first.position().left >= 0) {
              $container.children(options.jq_item + ':last')
                .insertBefore(first)
                .css('left', '-' + (width) + 'px')
                ;
            }
            
            $container.children(options.jq_item).animate({
              left: '+=' + width + 'px'
            });
            
            return false;
          })
          ;
          
      });
    
    }
  });
})(jQuery);

