window.addEvent('domready', function() {
		
	var galleryFx = new Fx.Tween($('gallery'),{ duration: 500 });
	var count = 1;
	
	$('arrowRight').addEvent('click',function(e){
		e.stop();
		galleryFx.start('opacity',0).chain(function(){
			if (count < ($('gallery').getElements('img').length/3)) {
				$('gallery').getFirst().setStyle('top', (0-(count*296)));
				count++;
			} else {
				$('gallery').getFirst().setStyle('top', 0);
				count = 1;
			}
			galleryFx.start('opacity',1)
		})
	})
	
	$('arrowLeft').addEvent('click',function(e){
		e.stop();
		galleryFx.start('opacity',0).chain(function(){
			if (count == 1) {
				$('gallery').getFirst().setStyle('top', (0-((($('gallery').getElements('img').length/3)-1)*296)));
				count = $('gallery').getElements('img').length/3;
			} else {
				count--;
				$('gallery').getFirst().setStyle('top', (0-((count-1)*296)));
			}
			galleryFx.start('opacity',1);
		})
	})

	var shadow = new Element('div',{ 'id':'shadow', 'styles':{
		'position':'absolute',
		'top':0,
		'left':0,
		'width': '1000px',
		'height': '100%',
		'background': '#000',
		'opacity': 0
	}})

	var galleryBox = new Element('div',{ 'id':'galleryBox', 'styles':{
		'position':'absolute',
		'top':0,
		'text-align':'center',
		'width':'100%',
		'padding-top': '50px',
		'opacity': 0
	}})

	var galleryImg = new Element('img',{ 'src':$('gallery').getFirst().get('href') });

	shadow.inject($('main'));
	galleryBox.inject($('main'));
	galleryImg.inject(galleryBox);
	var shadowFx = new Fx.Tween(shadow,{ duration: 500 });
	var galleryBoxFx = new Fx.Tween(galleryBox,{ duration: 500 });

	
	/* preloading */
	var preload = [];
	
	$('gallery').getElements('a').each(function(images){
		preload.include(images.get('href'));
		
		images.addEvent('click',function(e){
			e.stop();
			galleryImg.set('src',this.get('href'));
			shadowFx.start('opacity',.7).chain(function(){
				galleryBoxFx.start('opacity',1);
			})
		})
	})
	
	galleryImg.addEvent('click', function(close){
		galleryBoxFx.start('opacity',0).chain(function(){
			shadowFx.start('opacity',0);
		})
	})
	var loader = new Asset.images(preload)
	
});