function gallery() {
	
	if(images == 0) return false;
	
	var pages = ((images-1) / 4).toInt() + 1;
	var page = 1;
	var thumbs = $$('#gallery-thumbs li a');
	var image = $('gallery-image').getFirst();
	var right = $('gallery-right');
	var left = $('gallery-left');
	var gallery_thumbs = $('gallery-thumbs');
	var position = $('gallery-position');
	var fx = new Fx.Morph(gallery_thumbs, {'link': 'cancel', transition: Fx.Transitions.Quad.easeOut});
	var fx_running = false;
	var index = 0;
	
	gallery_thumbs.setStyle('width', images*102);
	
	thumbs[0].getParent().setProperty('id','selected-thumb');
	
	thumbs.each(function(thumb, i) {
		thumb.addEvent('click', function(e) {
			e.stop();
			index = i;
			$('selected-thumb').removeProperty('id');
			this.getParent().setProperty('id', 'selected-thumb');
			image.setProperty('src', this.getFirst().getProperty('src').replace('th/', ''));
		});
	});
	
	left.addEvent('click', function(e) {
		e.stop();
		
		index == 0 ? index = thumbs.length - 1 : index--;
		$('selected-thumb').removeProperty('id');
		thumbs[index].getParent().setProperty('id', 'selected-thumb');
		image.setProperty('src', thumbs[index].getFirst().getProperty('src').replace('th/', ''));
		position.set('text', (index+1) + ' (' + images + ')');
		
		if(index == thumbs.length - 1 || (index + 1) % 4 == 0)
			fx.start({'left': -408 * (index / 4).toInt()});
	});
	
	right.addEvent('click', function(e) {
		e.stop();
		
		index == thumbs.length - 1 ? index = 0 : index++;
		$('selected-thumb').removeProperty('id');
		thumbs[index].getParent().setProperty('id', 'selected-thumb');
		image.setProperty('src', thumbs[index].getFirst().getProperty('src').replace('th/', ''));
		position.set('text', (index+1) + ' (' + images + ')');
		
		if(index % 4 == 0) {
			fx_running = true;
			fx.start({'left': -408 * (index / 4).toInt()}).chain(function(){fx_running = false;});
		}
	});
}

addDOMLoadEvent(gallery);
