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();
	});
	if($('catalog_map')){
		var mapbox=new ShaddowBox('shaddowbox_map');
		$('catalog_map').addEvent('click',function(){
			mapbox.show();
		});
		$('map_close').addEvent('click',function(){mapbox.hide();});
	}
}
window.addEvent('domready',function(){
	if($('gallery'))
		gallery_init();
	var wm_search=$('wm_search');
	if(wm_search){
		wm_search.addEvents({
			'focus':function(){
				if(this.value==this.title)
					this.value='';
			},
			'blur':function(){
				if(this.value=='')
				this.value=this.title;
			}
		});
		$('search_filter').addEvent('submit',function(){
			if($('search_period').value==''&&$('search_region').value==''&&$('search_company').value==''){
				return false;
			}
			location.href=absPath+webLang+'/vyhladavanie/?search_period='+$('search_period').value+'&search_region='+$('search_region').value+'&search_company='+$('search_company').value+'&page=0';
			return false;
		});
		$('search_form').addEvent('submit',function(){
			if(wm_search.value==''||wm_search.value==wm_search.title)
				return false;
		});
		/* $('search_period').addEvent('change',function(){
			if(this.value!='')
				location.href=absPath+webLang+'/vyhladavanie/?search_period='+this.value+'&page=0';
		});
		$('search_region').addEvent('change',function(){
			if(this.value!='')
				location.href=absPath+webLang+'/vyhladavanie/?search_region='+this.value+'&page=0';
		});
		$('search_company').addEvent('change',function(){
			if(this.value!='')
				location.href=absPath+webLang+'/vyhladavanie/?search_company='+this.value+'&page=0';
		}); */
	}
});
