﻿window.addEvent('domready', function() { Category.setup(); });

var Category = {
    CurrentThumb: 0,
    setup: function() {

        var p = Category.ThumbList = $$('#categoryiteminner div');

        $('categoryiteminner').setStyle('width', (241 * p.length) + 'px');

        $$('#prevpage,#prevscreen').addEvent('click', function(y) { Category.MoveThumb(y, -1); });
        $$('#nextpage,#nextscreen').addEvent('click', function(y) { Category.MoveThumb(y, 1); });
        Category.SetText();
    },
    MoveThumb: function(y, direction) {
        y.stop();
        Category.CurrentThumb += direction;

        if (Category.CurrentThumb < 0)
            Category.CurrentThumb = 0;

        if (Category.CurrentThumb > Math.floor(Category.ThumbList.length / 4))
            Category.CurrentThumb = Math.floor(Category.ThumbList.length / 4);

        var firstPage = Category.CurrentThumb * 4;
        if (firstPage < Category.ThumbList.length) {
            var left = -Category.ThumbList[firstPage].getPosition('categoryiteminner').x;

            $('categoryiteminner').tween('left', left + 'px');

            Category.SetText();
        }
    },
    SetText: function() {
        var total = Category.ThumbList.length;
        var start = (Category.CurrentThumb * 4) + 1;
        var end = start + 3;
        if (end > total)
            end = total;
        $('position').set('text', 'Products ' + start + ' to ' + end + ' of ' + total);

    }
}