if (typeof EasyCMS == 'undefined') EasyCMS={};
if (!EasyCMS.hasOwnProperty('eyewireScroll')) EasyCMS.eyewireScroll={};

EasyCMS.eyewireScroll.scroll = function (step) {
    var obj = $('eyewireProductScroll');
    var products = $$('#eyewireProductScroll .product');
    var productWidth = products[0].getSize().x;
    var left = obj.getStyle('left').toInt();
    var productsNo = products.length;

    if (productsNo > 0) {
        var newOffset = left + step;

        if (newOffset <= 0 && newOffset >= productWidth * 4 - productsNo * productWidth) {

            $$('.eyewireSection .arrow').each(function(arrow, arrowIndex) {
                arrow.removeEvents('click');
            });

            var tweenObject = new Fx.Tween(obj, {
                fps: 35,
                duration: 150,
                onComplete: function() {
                    EasyCMS.eyewireScroll.attachScroller();
                    if (newOffset < 0) {
                        $$('.eyewireSection .arrow.left a').each(function(arrow, arrowIndex) {
                            if (arrow.hasClass('inactive')) {
                                arrow.setStyle('display', 'none');
                            } else {
                                arrow.setStyle('display', 'block');
                            }
                        });
                    } else {
                        $$('.eyewireSection .arrow.left a').each(function(arrow, arrowIndex) {
                            if (arrow.hasClass('inactive')) {
                                arrow.setStyle('display', 'block');
                            } else {
                                arrow.setStyle('display', 'none');
                            }
                        });
                    }

                    if (newOffset > productWidth * 4 - productsNo * productWidth) {
                        $$('.eyewireSection .arrow.right a').each(function(arrow, arrowIndex) {
                            if (arrow.hasClass('inactive')) {
                                arrow.setStyle('display', 'none');
                            } else {
                                arrow.setStyle('display', 'block');
                            }
                        });
                    } else {
                        $$('.eyewireSection .arrow.right a').each(function(arrow, arrowIndex) {
                            if (arrow.hasClass('inactive')) {
                                arrow.setStyle('display', 'block');
                            } else {
                                arrow.setStyle('display', 'none');
                            }
                        });
                    }
                }
            });
            tweenObject.start('left', obj.getStyle('left').toInt() + step);
        }
    }
    return false;
}

EasyCMS.eyewireScroll.attachScroller = function () {
    if ($$('#eyewireProductScroll .product')) {
        var step = $$('#eyewireProductScroll .product')[0].getSize().x;
        $('eyewireProductScroll').setStyle('width', $$('#eyewireProductScroll .product').length * $$('#eyewireProductScroll .product')[0].getSize().x);

        $$('.eyewireSection .arrow').each(function(arrow, arrowIndex) {
            if (arrow.hasClass('left')) {
                arrow.addEvent('click', function() {
                    return EasyCMS.eyewireScroll.scroll(step);
                });
            } else if (arrow.hasClass('right')) {
                arrow.addEvent('click', function() {
                    return EasyCMS.eyewireScroll.scroll(-step);
                });
            }
        });
    }
}

window.addEvent('domready', function() {
    EasyCMS.eyewireScroll.attachScroller();
});
