/*
 * jQuery horizonal scroller
*/


(function($) {
  $.fn.extend({
    horizScroller: function(options) {
      
      return this.each(function(i) {
        
        var width = 177 + 7;
        var x = $(this).children(options.jq_prev).outerWidth() + 5;
        
        $(this).css('position', 'relative');
          
        $(this).children(options.jq_item)
          .css('position', 'absolute')
          .css('top', '0px')
          .each(function(){
            $(this).css('left', x);
            x += width;
          })
          ;
        
        var total_width = x - width;
          
        $(this).children(options.jq_next)
          .css('position', 'absolute')
          .css('top', '0px')
          .css('right', '0px')
          .click(function() {
            //if ($(this).parent().children().is(':animated').length > 0) return false;
            $(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;
          })
          ;
          
        $(this).children(options.jq_prev)
          .css('position', 'absolute')
          .css('top', '0px')
          .css('left', '0px')
          .click(function(){
            //if ($(this).parent().children().is(':animated').length > 0) return false;
            $(this).parent().children(options.jq_item).stop(false, true);
            
            var first = $(this).parent().children(options.jq_item).eq(0);
            
            if (first.position().left > 0) {
              $(this).parent().children(options.jq_item + ':last')
                .insertBefore(first)
                .css('left', '-' + (177 - 7 - 7) + 'px')
                ;
            }
            
            $(this).parent().children(options.jq_item).animate({
              left: '+=' + width + 'px'
            });
            
            return false;
          })
          ;
          
      });
    
    }
  });
})(jQuery);