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();
	});
}
var voting_id=0,new_points;
function rating(){
	var v=$('rating');
	for(c=1;c<6;c++){
		v.grab(new Element('a',{
			'html':'&nbsp;',
			'title':c,
			'events':{
				'click':function(){
					new Request.HTML({url:absPath+webLang+'/'+module_url+'/',method:'get',onSuccess:function(tree,elements,html,js){
						$('rating_box').set('html',html);
						voting_id=0;
					}}).send('webmin_ajax=1&voting_action_type=rate&voting_id='+voting_id+'&voting_points='+this.title);
					return false;
				},
				'mouseover':function(){v.setStyle('backgroundPosition',(-150+(this.title*30))+'px 0px');},
				'mouseout':function(){v.setStyle('backgroundPosition','-150px 0px');}
			}
		}));
	}
}
window.addEvent('domready',function(){
	if($('gallery'))
		gallery_init();
	if(voting_id!=0)
		rating();
/* 	else if($('module_gallery'))
		$('module_gallery').getElements('img').addEvent('mouseover',function(event){
				this.tween('opacity',.7,1);						 	
			}
		); */
});