var ItemScroller = function(container, items, limit)
{
    this.construct(container, items, limit);
};

ItemScroller.prototype =
{
    container: null
    , items: []
    , maxItems: 0
    , offset: 0
    , buttons: {}
    
    , construct: function(container, items, limit)
    {
        this.items = [];
        
        this.container = jQuery(container)
            .prepend('<a href="#previous" class="previous-item">Previous Item</a>')
            .append('<a href="#next" class="next-item">Next Item</a>')
            [0];
        
        var self = this;
        this.buttons.next = jQuery('a.next-item', this.container)
            .bind('click', function()
            {
                if (self.offset < (self.items.length - self.maxItems))
                {
                    self.offset++;
                }
                self.refresh();
                return false;
            })
            .bind('focus', function(){this.blur();})
            [0];
            
        this.buttons.previous = jQuery('a.previous-item', this.container)
            .bind('click', function()
            {
                if (self.offset > 0)
                {
                    self.offset--;
                }
                self.refresh();
                return false;
            })
            .bind('focus', function(){this.blur();})
            [0];
        
        this.maxItems = limit;
        this.add(items);
    }
   
    , add: function(items)
    {
        this.items.push.apply(this.items, items);
        this.refresh();
    }
    
    , refresh: function()
    {
        var self = this;
        var from = self.offset;
        var to = self.offset + self.maxItems - 1;
        
        jQuery(this.items).each(function(i)
        {
            if (i >= from && i <= to)
            {
                jQuery(this).show();
            }
            else
            {
                jQuery(this).hide();
            }
        });
        
        jQuery(this.buttons.previous).show();
        if (this.offset <= 0)
        {
            jQuery(this.buttons.previous).hide();
        }
        
        jQuery(this.buttons.next).show();
        if (to >= this.items.length - 1)
        {
            jQuery(this.buttons.next).hide();
        }
    }
}

