var PhotoGlance = new Class({
    
    initialize: function() {
        
        this.thbs = $$('#pgContainer a');
        
        if(this.thbs.length > 0) {
            this.thumbsize = this.thbs[0].getSize();
            this.scrolling = false;
            this.container = $('pgContainer');
            this.anil = 1100;
            
            this.curr = 0;
            this.rEl = this.thbs[6];
            this.lEl = this.thbs[0];
            
            this.lcon = $('pgl');
            this.rcon = $('pgr');
            
            this.scrlr = new Fx.Scroll(this.container, { duration: this.anil, onComplete: function(obj) {
                if(obj.scrlr.from[0] == obj.scrlr.to[0]) {
                    obj.scrolling = false;
                }
            }.pass( this ) });
            
            this.lcon.addEvent('mouseenter', function(){ this.scrolling = true; this.scroll('left'); }.bind(this));
            this.lcon.addEvent('mouseleave', function(){ this.scrolling = false; }.bind(this));
            this.lcon.addEvent('click', function(e){ e.stop(); this.scrolling = false; this.scrlr.toLeft(); }.bind(this));
            this.rcon.addEvent('mouseenter', function(){ this.scrolling = true; this.scroll('right'); }.bind(this));
            this.rcon.addEvent('mouseleave', function(){ this.scrolling = false; }.bind(this));
            this.rcon.addEvent('click', function(e){ e.stop(); this.scrolling = false; this.scrlr.toRight(); }.bind(this));
            
            this.thumbs = $$('#pgContainer a');
            this.thumbs.each(function(thumb) {
                thumb.set('opacity', .65);
                thumb.addEvent('mouseenter', function(item){ item.tween('opacity', 1)  }.pass(thumb) );
                thumb.addEvent('mouseleave', function(item){ item.tween('opacity',.65) }.pass(thumb) );
            });
        }
    },
    
    scroll: function( direction ) {
        if(this.scrolling) {
            
            if(direction == 'left') {
                --this.curr;
            } else {
                ++this.curr;
            }
            
            this.lEl = this.thbs[this.curr - 1];
            this.rEl = this.thbs[this.curr + 1];
            
            var toElement;
            if(direction == 'left') {
                toElement = this.lEl;
            } else {
                toElement = this.rEl;
            }
            
            //console.log(this.curr, this.rEl, this.lEl);
            
            if(toElement && this.scrolling) {
                this.scrlr.toElement( toElement );
                this.scroll.delay(this.anil + 100, this, direction);
            } else {
                this.scrolling = false;
            }
            
        }
    }
    
});

window.addEvent('domready', function() {
    new PhotoGlance();
} );
