﻿window.addEvent('domready', function() { ProductList.setup(); });

var ProductList = {
    Current: 0,
    setup: function() {
        var p = ProductList.prod = $$('#productlistinner .product');
        $('productlistinner').setStyle('width', (188 * p.length) + 'px');

        ProductList.SetText();
        $$('#prevscreen,#largeprevscreen').addEvent('click', function(y) { ProductList.Move(y, -1); });
        $$('#nextscreen,#largenextscreen').addEvent('click', function(y) { ProductList.Move(y, 1); });

        p.each(function(x) {
            var y = x;
            y.addEvent('mouseenter', function() { y.getElements('.links,.info').removeClass('hidden'); });
            y.addEvent('mouseleave', function() { y.getElements('.links,.info').addClass('hidden'); });
        });

    },
    Move: function(y, direction) {
        y.stop();
        ProductList.Current += direction;

        if (ProductList.Current < 0)
            ProductList.Current = 0;

        if (ProductList.Current > Math.floor(ProductList.prod.length / 4))
            ProductList.Current = Math.floor(ProductList.prod.length / 4);

        var firstPage = ProductList.Current * 5;

        var left = -ProductList.prod[firstPage].getPosition('productlistinner').x;

        $('productlistinner').tween('left', left + 'px');
        ProductList.SetText();
    },
    SetText: function() {
        var total = ProductList.prod.length;
        var start = (ProductList.Current * 5) + 1;
        var end = start + 4;
        if (end > total)
            end = total;
        $('position').set('text', 'Products ' + start + ' to ' + end + ' of ' + total);

    }
}
