// main navigation code for claudiopesci.com
//
// (unobtrusive) javascript coded in october 2007 by pinit - http://me.pinit.it
//
// it uses 'a bit' the mootools js framework (http://mootools.net) (well, was the first time using it, so let's experiment!)

var navi = {
	slide : new Array,
	last: false,
	fire : function() {
		$$('.gotop').empty().setStyle('display','none');
		$$('#menu a').each(function(item){
			mydiv = item.getProperty('id').slice(2);
			navi.slide[item.getProperty('id').slice(2)] = new Fx.Slide(item.getProperty('id').slice(2), {onComplete: function(){
																if (this.wrapper.offsetHeight != 0)	this.wrapper.setStyle('height', 'auto');
																}}).hide();
			item.addEvent('click', function(e){new Event(e).stop();navi.show(this.getProperty('id').slice(2));});
		});	
		$('top').addEvent('click', function(e){new Event(e).stop();navi.show(false);});
		navi.slide['liospiti']= new Fx.Slide('liospiti').hide();
	},
	show : function(div) {
		if (!(div==navi.last)) {
			if(navi.last) {
				navi.slide[navi.last].slideOut();
				$('b_'+navi.last).removeClass('attivo');
			} 
			if(div) {
				navi.slide[div].slideIn();
				$('b_'+div).addClass('attivo');
				navi.last=div;
				if (!(navi.slide['liospiti'].open)) navi.slide['liospiti'].slideIn();	
			} else {navi.last=false;navi.slide['liospiti'].slideOut();}
		}
	}
};

var galleria = {
	div : new Array,
	last : 1,
	fire : function() {
		galbuttons = $$('.galbtn');
		galbuttons.each(function(item, ind){
			item.onclick = function () {galleria.show(ind+1);return false;};
			galleria.div[ind+1] = new Fx.Slide('t_'+(ind+1));
			if(ind>0) galleria.div[ind+1].hide(); else $('b_1').addClass('attivo');
		});
	},
	show : function(x) {
		if(!(x==galleria.last)) {
			galleria.div[galleria.last].slideOut();
			galleria.div[x].slideIn();
			$('b_'+galleria.last).removeClass('attivo');
			$('b_'+x).addClass('attivo');
			galleria.last = x;
		}
	}
};

var eventi = {
	fire: function() {
		if ($('recent')) {
			ev_recent = new Fx.Slide('recent').hide();
			new Element('a', {'id': 'mostra_recenti', 'href': '#', 'class':'eventibtn'}).setText('\u00bb altri eventi recenti').injectAfter('last').onclick = function(){eventi.show('recent');return false;};
			if ($('archive')) new Element('a', {'id': 'mostra_archivio', 'href': '#', 'class':'eventibtn'}).setText('\u00bb mostra tutti gli eventi').injectInside('recent').onclick = function(){eventi.show('archive');return false;};
		}
	},
	show: function(what){
		switch (what) {
			case 'recent':
				if(!($('ultimo1'))) {new Element('a', {'id': 'ultimo1', 'href': '#', 'class':'eventibtn'}).setText('\u00ab chiudi').injectInside('recent').onclick = function(){eventi.show('last');return false;};if ($('mostra_archivio')) $('ultimo1').injectBefore('mostra_archivio');}
				ev_recent.slideIn();
				$('mostra_recenti').setStyle('display','none');
				break;
			case 'archive':
				$('mostra_archivio').setStyle('display','none');
				$('ultimo1').setStyle('display','none');
				ev_recent.slideIn();
				if(!($('ultimo2'))) {
        			new Element('script',{'src':'jsdata/eventi.js.php?out=archive','type':'text/javascript'}).injectInside(document.body);
				} else ev_archive.slideIn();
				break;
			case 'last':
				if($('ultimo2')) {ev_archive.slideOut();$('mostra_archivio').setStyle('display','inline');}
				$('mostra_recenti').setStyle('display','inline');
				$('ultimo1').setStyle('display','inline');
				ev_recent.slideOut();
				break;
		}
	}
};

var ospiti = {
	num : 4,
	fire : function (){
		$('guestform').empty();
		new Element('div', {'id':'formdiv'}).injectInside('guestform');
		new Element('a', {'id':'signlink','href':'#'}).setText('\u00bb firma il libro degli ospiti').injectInside('guestform').onclick = function(){ospiti.sign();return false;};
		if ($('others')) {
			$('othersbtn').setText('\u00bb mostra altri').onclick = function() {ospiti.more(ospiti.num);return false;};
		}
	},
	sign : function() {
		if ($('fnome')) {
			$('signlink').setStyle('display','none');
			guest_form.slideIn();
		} else {
	        new Element('script',{'src':'jsdata/form.js','type':'text/javascript'}).injectInside(document.body);
		};
	},
	more : function(from) {
			$('others').addClass('loading');
			new Element('script',{'src':'jsdata/moresigns.js.php?from='+from,'type':'text/javascript'}).injectInside(document.body);
	}
};

window.addEvent('domready', function(){
	//fire! fire! :D
	navi.fire();     //this drives main navigation menu
	galleria.fire(); //this drives the gallery (galleria)	
	eventi.fire();	 //this drives the news (eventi)
	ospiti.fire();   //this drives the guestbook (libro degli ospiti)
});	
