/*
 * jQuery horizonal scroller
*/


(function($) {
  $.fn.extend({
    horizScroller: function(options) {
      
      return this.each(function(i) {
        
        var $container = $(this);
        
        var width = 170;
        var x = $(this).children(options.jq_prev).outerWidth();
        
        $container.css('position', 'relative');
          
        $container.children(options.jq_item)
          .css('position', 'absolute')
          .css('top', '0px')
          .each(function(){
            $(this).css('left', x);
            x += width;
          })
          ;
        
        var total_width = x - width;
          
        $container.parent().children(options.jq_next)
          .css('position', 'absolute')
          .css('top', '0px')
          .css('right', '0px')
          .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;
          })
          ;
          
        $container.parent().children(options.jq_prev)
          .css('position', 'absolute')
          .css('top', '0px')
          .css('left', '0px')
          .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', '-' + (170) + 'px')
                ;
            }
            
            $container.children(options.jq_item).animate({
              left: '+=' + width + 'px'
            });
            
            return false;
          })
          ;
          
      });
    
    }
  });
})(jQuery);

