window.addEvent('domready',function(){
	
	Gallery = new Class({
	
		initialize:function(container){	
			//console.log("Gallery.initialize");
			
			this.currentPage = 1;
			this.container = $(container);
			this.nav = $('vids-nav');
			this.slider = this.container.getElement('.slider');
			this.items = this.container.getElements('.vid');		
			this.buttonBack = this.nav.getElement('a[href$=back]');
			this.buttonNext = this.nav.getElement('a[href$=next]');
			
			this.viewable = 12;
			this.viewable_height = this.container.getStyle('height').toInt();
			this.pageCount = Math.ceil(this.items.length/this.viewable);
			
			this.slider.set('tween', {duration: 'short'});
			
			this.addEvents();
			this.show(this.currentPage);
			
	
		},
		
		addEvents:function(){
			//console.log("Gallery.addEvents");
			this.buttonBack.addEvent('click', function(e) {
				e.stop();
				this.showPrev();
			}.bind(this));	
			
			this.buttonNext.addEvent('click', function(e) {
				e.stop();
				this.showNext();
			}.bind(this));	
			
		},
		
		show:function(page){
	
			//console.log("Gallery.show(",page,")");
			this.currentPage = page;
			this.slider.tween('margin-top', - ((this.currentPage-1)*this.viewable_height));
		
			if(this.currentPage >= this.pageCount)
			{
				this.buttonNext.setOpacity(0.2);
				this.buttonNext.setStyle('cursor','default');
			}
			else
			{
				this.buttonNext.setOpacity(1);
				this.buttonNext.setStyle('cursor','pointer');
			}
			if(this.currentPage <=1 )
			{
				this.buttonBack.setOpacity(0.2);
				this.buttonBack.setStyle('cursor','default');
			}
			else
			{
				this.buttonBack.setOpacity(1);
				this.buttonBack.setStyle('cursor','pointer');
			}
		},
		
		showNext:function(){
			//console.log("Gallery.showNext");
			if(this.currentPage < this.pageCount )
			{
				this.show(this.currentPage+1);
			}
		},
		
		showPrev:function(){
			//console.log("Gallery.showPrev");
			if(this.currentPage > 1 )
			{
				this.show(this.currentPage-1);
			}
		}
	});	
});

