var photo,
	photos,
	photos_cnt=0,
	last_photo=0,
	shown=0,
	sfx;

function showPhoto(i,sbox){	
	if(!photo)
		photo=new Element('img').inject($('gallery_photo'));		
	photos[last_photo].removeClass('active');
	photos[i].set('class','active');
	
	last_photo=i;
	$('gallery_description').set('html',(photo_data[i][0]!=''?photo_data[i][0]:'&nbsp;'));	
	photo.src=photos[i].src.replace('/photos/','/photos/orig/');
	if(sbox)
		sbox.show();	
	s=112*last_photo;
	if(!sfx)
		sfx=new Fx.Scroll($('gallery_thumbs_box'));
	sfx.set(s,0);
}
function cyclePhotos(dir){
	dir=='-'&&last_photo>0?showPhoto(last_photo-1):((dir=='+'&&last_photo<(photos_cnt-1))?showPhoto(last_photo.toInt()+1):'');
}

function gallery_init(){
	var sbox=new ShaddowBox('shaddowbox_content');
	var _gal=$('gallery');
	_gal.getElements('img').each(function(o,i){
		o.addEvents({
			'click':function(event){
				showPhoto(i,sbox);	 	
			}.pass([i,sbox]),
			'mouseover':function(event){
				this.tween('opacity',.7,1);						 	
			}
		});
	});
	
	var _thumbs=_gal.clone();

	_thumbs.setProperty('id','gallery_thumbs');
	$('gallery_thumbs_box').setStyle('overflow-x','scroll');
	$('gallery_thumbs_box').grab(_thumbs);
	
	photos=$('gallery_thumbs').getElements('img');	
	photos.each(function(o,i){
		o.addEvents({
			'click':function(event){
				showPhoto(i);						 	
			}.pass([i,sbox]),
			'mouseover':function(event){
				this.tween('opacity',.7,1);						 	
			}
		});
	});
	photos_cnt=photos.length;
	
	$('gallery_previous').addEvent('click',function(){cyclePhotos('-');});
	$('gallery_next').addEvent('click',function(){cyclePhotos('+');});
	$('gallery_close').addEvent('click',function(){sbox.hide();});
	
	$$('html').addEvent('keydown',function(event){
		param=event.key=='left'?'-':event.key=='right'?'+':'';
		if(param!=''){
			cyclePhotos(param);
			event.preventDefault();
		}
		if(event.key=='esc'&&sbox.shown==1)
			sbox.hide();
	});
}
window.addEvent('domready',function(){
	if($('gallery'))
		gallery_init();
	else if($('module_gallery'))
		$('module_gallery').getElements('img').addEvent('mouseover',function(event){
				this.tween('opacity',.7,1);						 	
			}
		);
	if($('news_back'))
		$('news_back').addEvent('click',function(){
			history.go(-1);
			return false;
		});
});
