// JavaScript Document
var Menu = {
	menu: '',
	menuIN: '',
	stuck: false,
	isHiding: false,
	isShowing: false,
	timing: '',
	
	init: function() {
		this.menu = $('menu');
		this.menuIN = this.menu.down();
		this.menu.observe("mouseover", Menu.stickMenu.bind(this));
		this.menu.observe("mouseout", Menu.freeMenu.bind(this));
		document.observe("mousemove", Menu.show.bind(this));
		
		Menu.show();
	},
	
	hide: function() {
		if (this.stuck || this.isHiding) return;
		
		var __this = this;
		
		this.isHiding = true;
		 
		this.menuIN.fade({
			duration: 0.2
		})
		
		new Effect.Morph(this.menu, {
			style: 'height:5px',
			transition: Effect.Transitions.sinoidal,
			duration: 0.6,
			afterFinish: function() {
				__this.isHiding = false;
			}
		});
		
	},
	
	show: function() {
		var __this = this;
		
		if (this.isShowing) return;
		if (this.isHiding) {
			setTimeout(this.show.bind(this), 200);
			return;
		}
		this.isShowing = true;

		if (this.menu.getHeight() < 41) {
			new Effect.Morph(this.menu, {
				style: 'height:41px',
				transition: Effect.Transitions.sinoidal,
				duration: 0.3,
				afterFinish: function() {
					__this.menuIN.appear({
						from: __this.menuIN.getOpacity(),
						duration: 0.2
					})
					
					__this.isShowing = false;

				}
			});
		
		} else {
			this.isShowing = false;
		}
		
		if (this.timing) clearTimeout(this.timing);
		if (!this.stuck) {
			this.timing = setTimeout(this.hide.bind(this), 2000);
		}
	},
	
	stickMenu: function() {
		this.stuck = true;
		this.show();
	},

	freeMenu: function() {
		this.stuck = false;
	}

}
var Main = {
	i: 1,
	el: '',
	el2: '',
	timeout: '',
	numero_pallini: '',
	
	init: function() {
		this.el =	$$('.supertitolo:not(.hidden)')[0];
		this.el2 = $('pallini').down('img');
		this.timeout = setTimeout(function() { Main.slide(); }, 5000);
			
		jQuery("#slider").easySlider({
			prevShow: false,
			nextShow: false,
			auto:true
		});
	},
	
		
	slide: function(j) {
		if (!j) {
			if (++this.i > this.numero_pallini ) this.i = 1;
		} else {
			if (this.i == j) return;
			this.i = j;
			clearTimeout(this.timeout);
		}
		
		var next = $$('.supertitolo')[this.i - 1];
		if (this.i > 1) {
			var next2 = $('pallini').down().next(this.i - 2).down();
		} else {
			var next2 = $('pallini').down('img');
		}
		
		
		next.setOpacity(0);	
		//el.absolutize();
		next.removeClassName('hidden');		
		
		new Effect.Opacity(this.el, {
			from: 1,
			to: 0,
			duration: 0.3,
			afterFinish: function() {
				this.el.addClassName('hidden');
				//el.relativize();
				this.el = next;
				
				this.el2.src = 'images/tondo.jpg';
				this.el2.up('a').style.cursor = '';
				
				next2.src = 'images/tondo_red.jpg';
				next2.up('a').style.cursor = 'default';
				
				this.el2 = next2;
				
				this.timeout = setTimeout(function() { Main.slide(); }, 5000);
			}.bind(this)
		})
		next.appear({ duration:1.5 });
		
		return false;
	}

}

function start() {	
	Menu.init();
	if (slider = $('slider')) {
		slider.setOpacity(0);
		slider.show();
		slider.appear({ duration: 0.8 });	
	}
	
	if (document.body.id == 'home') {
		Main.init();
	}
}

window.onload = function() {
	var table = $($$('body > table')[0]);
	var div = $('slideMe');
	
	div.style.marginLeft = '300px';
	
	table.setOpacity(0);
	table.addClassName('show');
	table.appear({
		duration: 0.6
	})
	
	new Effect.Morph(div, {
		style: 'margin-left:0px',
		duration: 0.2
	})

	start();
}
