/*
 * jQuery horizonal scroller
*/


(function($) {
  $.fn.extend({
    horizScroller: function(options) {
      
      return this.each(function(i) {
        $container = $(this);
        
        $container.css('position', 'relative');
        
        var width = 100;
        var x = 33;
          
        $container.find(options.jq_item).each(function() {
          $(this).css('position', 'absolute').css('top', 0).css('left', x);
          x += width;
        });
        
        var total_width = x - width;
          
        $container.find(options.jq_next)
          .css('position', 'absolute')
          .css('top', '0px')
          .css('right', '0px')
          .click(function() {
            $(this).parent().children(options.jq_item).stop(false, true);
            
            var last = $(this).parent().children(options.jq_item + ':last');
            
            if (last.position().left < total_width) {
              $(this).parent().children(options.jq_item).eq(0)
                .insertAfter(last)
                .css('left', (total_width) + 'px')
                ;
            }
            
            $(this).parent().children(options.jq_item).animate({
              left: '-=' + width + 'px'
            });
            
            return false;
          });
          
        $container.find(options.jq_prev)
          .css('position', 'absolute')
          .css('top', '0px')
          .css('left', '0px')
          .click(function(){
            $(this).parent().children(options.jq_item).stop(false, true);
            
            var first = $(this).parent().children(options.jq_item).eq(0);
            
            if (first.position().left >= 33) {
              $(this).parent().children(options.jq_item + ':last')
                .insertBefore(first)
                .css('left', 33 - 100);
            }
            
            $(this).parent().children(options.jq_item).animate({
              left: '+=' + width + 'px'
            });
            
            return false;
          });
          
      });
    
    }
  });
})(jQuery);
