﻿window.addEvent('domready', function() { Press.setup(); });

var Press = {
    Current: 0,
    setup: function() {

        var p = Press.prod = $$('#presslistinner .pressitem');
        var thumbTop = 0;
        var thumbLeft = 0;
        var rowNum = 0;

        p.setStyle('visibility', 'hidden');

        p.each(function(y) {
            y.setStyles({ 'top': thumbTop + 'px', 'left': thumbLeft + 'px' });
            rowNum++;

            var l = thumbLeft;
            var t = thumbTop;

            if (rowNum % 4 == 0) {
                rowNum = 0;
                thumbLeft = 0;
                thumbTop += y.getSize().y;
            }
            else {
                thumbLeft += y.getSize().x + 5;
            }

            var img = y.getElement('img');

            img.set('morph', { 'duration': 300 });
            y.set('morph', { 'duration': 300 });

            img.addEvent('mouseenter', function(z) {
                $$('#presslistinner .pressitem img').setStyle('z-index', 1);
                y.setStyle('z-index', 100);
                y.morph({ 'top': t - 25, 'left': l - 15 });
                img.morph({ 'width': 210, 'height': 275 });
            });
            img.addEvent('mouseleave', function(z) {
                y.morph({ 'top': t, 'left': l });
                img.morph({ 'width': 180, 'height': 225 });
            });
        });

        $('presslist').setStyle('height', thumbTop + 200);

        p.setOpacity(0);
        p.setStyle('visibility', '');
        p.fade(1);


        /*
        $('presslistinner').setStyle('width', (240 * p.length) + 'px');

        Press.SetText();
        $$('#prevscreen,#largeprevscreen').addEvent('click', function(y) { Press.Move(y, -1); });
        $$('#nextscreen,#largenextscreen').addEvent('click', function(y) { Press.Move(y, 1); });
        }
        */
    }

    ,
    Move: function(y, direction) {
        y.stop();
        Press.Current += direction;

        if (Press.Current < 0)
            Press.Current = 0;

        if (Press.Current > Math.floor(Press.prod.length / 4))
            Press.Current = Math.floor(Press.prod.length / 4);

        var firstPage = Press.Current * 4;

        var left = -Press.prod[firstPage].getPosition('presslistinner').x;

        $('presslistinner').tween('left', left + 'px');
        Press.SetText();
    },
    SetText: function() {
        var total = Press.prod.length;
        var start = (Press.Current * 4) + 1;
        var end = start + 3;
        if (end > total)
            end = total;
        $('position').set('text', 'Article ' + start + ' to ' + end + ' of ' + total);

    }
}
