var Chisiamo_a = Class.create({
	initialize: function() {
		this.duration = 0.5;

		this.observeMenu();
		
		this.goTo(1);
	},
	
	observeMenu: function() {
		$$('a[id^="menu_sezione_"]').each(function(el) {
			var section = el.id.substring("menu_sezione_".length, el.id.length);
			Event.observe(el, 'click', this.goTo.bind(this, section));
		}.bind(this));
	},
	
	goTo: function(section) {
		var finalTop = 0;
		$$('div.sezione').each(function(el, i) {
			i++;
			
			if (i < section) {
				finalTop -= el.getHeight();
			}
			
			if (i == section) {
				this.activateSection(i);
			} else {
				this.deactivateSection(i);
			}
		}.bind(this));

		var minHeight = $('menu_sezioni').getHeight();
		var finalHeight = $('sezione_' + section).getHeight() - 30;
		if (finalHeight < minHeight) finalHeight = minHeight;
		
		new Effect.Morph('interno', {
			style: {
				height: finalHeight + "px"
			},
			duration: this.duration
		});
		new Effect.Morph('sezioni', {
			style: {
				top: finalTop + "px"
			},
			duration: this.duration
		});
	},
	
	activateSection: function(section) {
		var menu = $('menu_sezione_' + section);
		if (!menu.hasClassName('selezionato')) menu.addClassName('selezionato');
		
		new Effect.Opacity('sezione_' + section, {
			to: 1.0,
			duration: this.duration
		});
		if ($$('#sezione_' + section + ' b')[0].getStyle('display') != 'none') {
			setTimeout(function() {
				$$('#sezione_' + section + ' b').each(function(el) {
					el.setStyle({
						opacity: 1.0
					});
				});
			}, this.duration * 1000 / 2);
		}
	},
	
	deactivateSection: function(section) {
		var menu = $('menu_sezione_' + section);
		if (menu.hasClassName('selezionato')) menu.removeClassName('selezionato');
		
		new Effect.Opacity('sezione_' + section, {
			to: 0.0,
			duration: this.duration
		});
		if ($$('#sezione_' + section + ' b')[0].getStyle('display') != 'none') {
			setTimeout(function() {
				$$('#sezione_' + section + ' b').each(function(el) {
					el.setStyle({
						opacity: 0.0
					});
				});
			}, this.duration * 1000 / 2);
		}
	}
});