// JavaScript Document

window.addEvent('domready', function () {

	// __________________________________________________________ PHOTOGALLERY
	var imgsarr = new Array();
	
	var dx = $$('#dx-photogallery');
	var sx = $$('#sx-photogallery');
	var images = $$('#photo-photogallery img');
	images.set('morph', {duration : 1200});
	dx.set('morph', {duration : 250});
	sx.set('morph', {duration : 250});
	dx.setStyle('opacity','.9');
	sx.setStyle('opacity','.9');
	
	var current = 1;
	
	var Nimages = images.length;
	
	// setto lo z-index
	for(i=1;i<=images.length;i++) {
		images[i-1].setStyle('z-index', Nimages - (i-1) );
		imgsarr[i-1] = "../gallery/small/"+i+".jpg"
	}
	
	images[current-1].set('src',imgsarr[current-1]);
	
	// tasto destro
	dx.addEvents({
		'click' : function(){
			images.morph({'opacity':'0'});
			current++;
			if(current > Nimages) { current = 1; }
			images[current-1].set('src', imgsarr[current-1]);
			images[current-1].addEvent('load', function () {
				images[current-1].morph({'opacity':'1'});
			});
			Site.counter = current;
		},
		'mouseover' : function() {
			dx.morph({'opacity':'.4'});
		},
		'mouseleave' : function() {
			dx.morph({'opacity':'.9'});
		}
	});

	// tasto sinistro
	sx.addEvents({
		'click' : function(){
			images.morph({'opacity':'0'});
			current--;
			if(current < 1) { current = Nimages; }
			images[current-1].set('src', imgsarr[current-1]);
			images[current-1].addEvent('load', function () {
				images[current-1].morph({'opacity':'1'});
			});
			Site.counter = current;
		},
		'mouseover' : function() {
			sx.morph({'opacity':'.4'});
		},
		'mouseleave' : function() {
			sx.morph({'opacity':'.9'});
		}
	});

	var Site = { counter: 1 };
	var addCount = function(){ 
		images.morph({'opacity':'0'});
		this.counter++;
		if(this.counter > Nimages) { this.counter = 1;	}
		current = this.counter;
		images[this.counter-1].set('src', imgsarr[this.counter-1]);
		images[this.counter-1].addEvent('load',function() {
			images.morph({'opacity':'0'});
			this.morph({'opacity':'1'})	
		})
	};
	addCount.periodical(7000, Site); 

});
