var GalleryCarousel = new Class({
    ctr: 0,
    max: 0,
    maxh: 0,
    height: 0,
    xpos: 0,
    ypos: 0,
    delay: 0,
    scroll: null,
    panel: null,
    tray: null,
    item: [],
    options: {
        group: 1,
        delay: 50,
        duration: 500,
        transition: Fx.Transitions.Cubic.easeOut,
        linkBack: null,
        linkNext: null,
        linkCtrl: 'linkCtrl'
    },

    initialize: function(win, options) {
        if (!$(win)) {
            return false;
        }
        
        var win = $(win);
        this.setOptions(options);
        this.panel = $E('.thumbpanel', win);
        this.tray = $E('.thumbtray', win);
        this.ctr = 0;
        this.maxh = 0;

        $ES('.thumbnail', this.tray).each(function(item) {
            this.ctr++;
            this.item[this.ctr] = {top: this.maxh,
                                   width: item.getCoordinates().width,
                                   height: item.getCoordinates().height};
            this.maxh += this.item[this.ctr].height;
        }.bind(this));

        this.ctr = 0;
        this.height = this.panel.getCoordinates().height;
        this.max = Math.floor(this.maxh / this.height);

        this.scroll = new Fx.Scroll(this.panel, {wait: true,
                                                 duration: this.options.duration,
                                                 transition: this.options.transition});

        this.up = new Element('p').addEvent('mouseover', this.hoverOn)
                        .addEvent('mouseout', this.hoverOff)
                        .addEvent('click', this.rewind.bind(this))
                        .addClass(this.options.linkBack)
                        .setOpacity(0.5)
                        .injectInside(win)
                        .setHTML('Back');
        this.dn = new Element('p').addEvent('mouseover', this.hoverOn)
                        .addEvent('mouseout', this.hoverOff)
                        .addEvent('click', this.advance.bind(this))
                        .addClass(this.options.linkNext)
                        .setOpacity(0.5)
                        .injectInside(win)
                        .setHTML('Next');

        this.render();
    },
    
    advance: function() {
        if (this.ctr == this.max) {
            return false;
        }
        
        this.ctr++;
        
        this.render();
    },
    
    rewind: function() {
        if (this.ctr == 0) {
            return false;
        }
        
        this.ctr--;
        
        this.render();
    },
    
    render: function() {
        var tgt = (this.ctr * this.height > this.maxh - this.height) ? this.maxh - this.height : this.ctr * this.height;
        this.scroll.scrollTo(0, this.ctr * this.height);
        this.up.setStyles({display: (this.ctr > 0) ? 'block' : 'none'});
        this.dn.setStyles({display: (this.ctr < this.max) ? 'block' : 'none'});
    },
    
    hoverOn: function() {
        if (!this.fx) {
            this.fx = new Fx.Style(this, 'opacity', {wait: false});
        }
        this.fx.start(1);
    },
    
    hoverOff: function() {
        this.fx.start(0.5);
    }
});

GalleryCarousel.implement(new Options);
