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');
	_thumbs.removeClass('webmin');
	$('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;
	
	$('button_show_gallery').addEvent('click',function(){
		showPhoto(0,sbox);
		return false;
	});
	$('button_show_gallery_small').addEvent('click',function(){
		showPhoto(0,sbox);
		return false;
	});
	
	$('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 region_id=0,city_id=0,fb_shown='';
var filter_cities_data='';
var filterbox_init=function(){
	$$('div.filterbox_options a').addEvent('click',function(){
		val=this.get('html');
		prnt=this.getParent();
		prnt.getNext().set('value',val);
		prnt.getPrevious().getPrevious().set('html',val);
		prnt.setStyle('display','none');
		fb_shown='';			
		filter_type=prnt.get('id');
		if(filter_type=='filter_theme'){
			return true;
		}
		if(filter_type=='filter_region'){
			region_id=this.id.substr(13);
			new Request.HTML({url:absPath+urlSelectedFile,method:'get',onSuccess:function(tree,elements,html,js){
				$('module_catalog_list').set('html',html);
				tit=$('filter_city_title');
				tit.set('html',tit.get('title'));
				$('filter_city').set('html',filter_cities_data);
				filterbox_init();
			}}).send('webmin_ajax=1&catalog_action_type=filter_region&region_id='+region_id);
		}
		if(filter_type=='filter_city'){
			loc=location.href;
			loc=loc.split('?');
			loc=loc[0];
			city_id=this.id.substr(11);
			new Request.HTML({url:loc,method:'get',onSuccess:function(tree,elements,html,js){
				$('module_catalog_list').set('html',html);
			}}).send('webmin_ajax=1&catalog_action_type=filter_region&region_id='+region_id+'&city_id='+city_id);
		}
		if(filter_type=='filter_region2'){
			return true;
		}
		if(filter_type=='filter_theme2'){
			theme_id=this.id.substr(12);
			new Request.HTML({url:absPath+urlSelectedFile,method:'get',onSuccess:function(tree,elements,html,js){
				$('module_catalog_list').set('html',html);
			}}).send('webmin_ajax=1&catalog_action_type=filter_region&region_id='+region_id+(this.get('class')=='subcat'?'&catalog_subcat='+theme_id:'&theme_id='+theme_id)+(city_id!=0?'&city_id='+city_id:''));
		}
		return false;
	});		
}
window.addEvent('domready',function(){
	if($('gallery'))
		gallery_init();
	if($('module_filter')){
		$$('div.filterbox_button').addEvent('click',function(){
			if(fb_shown!='')
				fb_shown.setStyle('display','none');
			pos=this.getPosition();
			box=this.getNext();
			if(box.get('html')=='')
				return false;
			box.setStyles({'top':(pos.y+29),'left':(pos.x-178),'display':'block'});
			fb_shown=box;
		});
		filterbox_init();
		$$('html').addEvent('click',function(event){
			if($(event.target).hasClass('filterbox_button'))
				return;
			if(fb_shown!='')
				fb_shown.setStyle('display','none');
		});
	}
});
