﻿

var pedidosScroller = new Class({

    Implements: [Options],
    options: {
        back: null,
        forward: null,
        page: null, // span que muestra la página actual
        width: 115,
        totalImages: 11,
        visibleImages: 5,
        restartLink: null, // link que vuelve a la primer página
        currentFirst: 0
    },

    clearAll: function() {
        this.options.back.removeEvent('click');
        this.options.forward.removeEvent('click');
    },

    initialize: function(containerId, options) {
        this.setOptions(options);
        this.container = $(containerId); //ul container                    
        if (this.container.getElements('div.offer_item')) {
            this.options.totalImages = this.container.getElements('div.offer_item').length;
        }
        this.addEvents();
        if (this.options.currentFirst < 0 || this.options.totalImages - this.options.currentFirst < this.options.visibleImages) {
            this.options.currentFirst = 0;
        }
        if (this.options.restartLink) {
            if (this.options.totalImages <= this.options.visibleImages) {
                var parent = this.options.restartLink.getParent();
                parent.setStyle('display', 'none');
            }
        }
        this.moveFirstTime();
    },
    addEvents: function() {
        this.options.back.addEvent('click', function() { this.move(-3) } .bind(this));
        this.options.forward.addEvent('click', function() { this.move(3) } .bind(this));
        if (this.options.restartLink) {
            this.options.restartLink = $(this.options.restartLink);
            this.options.restartLink.addEvent('click', function() {
                this.options.currentFirst = 0;
                this.moveFirstTime();
            } .bind(this));
            this.options.restartLink.set('href', 'javascript:void(0);');
        }
        this.options.back.set('href', 'javascript:void(0);');
        this.options.forward.set('href', 'javascript:void(0);');
    },
    move: function(way) {
        this.options.currentFirst += way;
        this.container.tween('margin-left', (this.options.currentFirst * this.options.width * (-1)));
        this.checkBounds();

        if (this.options.page != null) {
            var total = (this.options.totalImages / 3).toInt();
            if ((total * 3) < this.options.totalImages) {
                total = total + 1;
            }
            $(this.options.page).set('html', ((this.options.currentFirst / 3) + 1).toString() + ' de ' + total.toString() + ' ');
        }
    },
    moveFirstTime: function() {
        this.container.tween('margin-left', (this.options.currentFirst * this.options.width * (-1)));
        if (this.options.page != null) {
            var total = (this.options.totalImages / 3).toInt();
            if ((total * 3) < this.options.totalImages) {
                total = total + 1;
            }
            $(this.options.page).set('html', ((this.options.currentFirst / 3) + 1).toString() + ' de ' + total.toString() + ' ');
        }
        this.checkBounds();
    },
    checkBounds: function() {
        if (this.options.currentFirst == 0) {
            this.options.back.setStyle('display', 'none');
            this.options.back.getParent().addClass('brand_arrow_l_dis');
            this.options.back.getParent().removeClass('brand_arrow_l');
        } else {
            this.options.back.setStyle('display', 'block');
            this.options.back.getParent().removeClass('brand_arrow_l_dis');
            this.options.back.getParent().addClass('brand_arrow_l');
        }
        if (this.options.totalImages - this.options.currentFirst <= this.options.visibleImages) {
            this.options.forward.setStyle('display', 'none');
            this.options.forward.getParent().addClass('brand_arrow_r_dis');
            this.options.forward.getParent().removeClass('brand_arrow_r');
        } else {
            this.options.forward.setStyle('display', 'block');
            this.options.forward.getParent().removeClass('brand_arrow_r_dis');
            this.options.forward.getParent().addClass('brand_arrow_r');
        }
    }
});



