window.addEvent('domready',function(){
	new ImgLoop('images');
	new Nav(c, l);
});

var Nav = new Class({
	Implements: [Options, Events],
	options: {
		'start': 'start',
		'pages': 'article',
		'links': '.page'
	},
	cache: {},
	initialize: function(year, lang, options){
		this.setOptions(options);
		this.y = year;
		this.l = lang;
		this._setPages();
		//this._setMenu();
		this._setArchive();
		
		this.show(this.options.start, true);
	},
	_setPages: function(){
		$$(this.options.pages).each(function(el){
			el.store('y', el.getSize().y);
			el.setStyle('display', 'none');
		});
		
		$$(this.options.links).each(function(el){
			el.removeEvents('click');
			el.addEvent('click', function(e) {
				e.stop();
				this.show(el.get('href').replace('#',''));
			}.bind(this));
		}.bind(this));
	},
	_setMenu: function(){
			$('topnav').getElements('ul li').each( function( e ){
			var list = e.getElement('ol');
			if(!list) return;

			var myFx = new Fx.Slide(list).hide();
			e.addEvents({
				'mouseenter' : function(){
					myFx.cancel();
					myFx.slideIn();
				},
				'mouseleave' : function(){
					myFx.cancel();
					myFx.slideOut();
				}
			});
		});
	},
	_setArchive: function(){
		$$('#topnav ol a').each(function(el){
			el.addEvent('click', function(){
				var year = el.get('href').replace('#','');
				if (year==this.year) return;
				
				if(this.cache[year]) return this.updatePage(this.cache[year]);
				
				new Request.JSON({url: '/archive.php',
					onSuccess: function(data){
						if (!data || data.error) {alert('Server-Fehler / Errore di sistema'); return;}
						this.cache[year] = data;
						
						if (year != this.y) {
							$(document.body).set('class', 'archive');
						} else {
							$(document.body).set('class', '');
						}
						
						this.year = year;
						
						this.updatePage(data);
				  }.bind(this),
					onError: function(msg){
						alert('Fehler / Errore');
					},
				}).get({'y': year, 'l': this.l});
			}.bind(this));
		}.bind(this));
	},
	updatePage: function(data){
		$('start').getFirst('h1').set('html', data.title);
		$$('aside').getFirst('h1').set('html', data.short);

		$$('aside li').each(function(el){el.dispose()});

		// Add new groups
		data.groups.each(function(g){
			var article = new Element('article', {'id': g.short, 'class': 'group'}).adopt(
				new Element('a', {'name': g.short}),
				new Element('img', {'src':'../img/groups/' + g.img, 'alt':g.title}),
				new Element('h1', {'html':g.title}).adopt(new Element('span', {'html': g.day + ' ' + g.date}))
			).inject('contact', 'before');
			article.set('html', article.get('html') + g.content);

			$$('aside ol').adopt(new Element('li').adopt(new Element('a', {'href': '#' + g.short, 'title': g.title, 'class': 'page'}).adopt(
				new Element('span', {'class': 'date', 'html': g.date}).grab(new Element('span', {'html': g.day}), 'top'),
				new Element('span', {'html': g.title})
			)));
		});

		this._setPages();
		this.show(data.groups[0].short);
	},
	show: function(id, nofade) {
		if (id==this.current) return;

		if (nofade==true) {
			$(id).setStyle('display', 'block');
			this.current = id;
			return;
		}

		var self = this;
		var fx = new Fx.Tween(this.current);
		fx.start('opacity', 0).chain(function() {
				this.set('display', 'none');
				this.callChain();
		}).chain(function() {
				$('container').tween('height', $(id).retrieve('y') + 20);
				this.callChain();
		}).chain(function() {
				$(id).setStyles({'opacity': 0, 'display': 'block'});
				this.callChain();
		}).chain(function() {
				$(id).tween('opacity', 1);
				self.current = id;
		});
	}
});
