window.addEvent('domready', function(){
	new Ag_animation();
});

var Ag_animation = new Class({					 

	Implements: [Events, Options],
	
	options:{},

	initialize: function(){
		this.current_item = 1;
		this.timer;		
				
		var lnk = $('m_index').get('href');
		var request = new Request.JSON({
			url: lnk + "?action=ajax_get_agenda",			
			onComplete: function(rJSON, response) {					
				var json = $H(JSON.decode(response, true));
				if(json["agenda"].length>1){
					json["agenda"].each(function(el){
						var tmp = new Element('li').addEvent('click', function(){window.location.href = el['url'];}).setStyles({'opacity': 0, 'top': 40, 'cursor': 'pointer'}).adopt(
													new Element('span', {'class': 'date', html:el['date']}),
													new Element('span', {'class': 'title', html:el['title']}),
													new Element('span', {'class': 'description', html:el['description']})
						);
						$('ticker_agenda').grab(tmp);												
					});
					
					this.items = $$('#ticker_agenda li');
					this.timer = this.get_next.delay(0, this);
				}
			}.bind(this)
		}).post();
				
		
		/*$('ticker_agenda').addEvents({
			mouseenter: function(){this.timer = $clear(this.timer);}.bind(this),
			mouseleave: function(){this.timer = this.get_next.delay(0, this);}.bind(this)
		});*/
	},
	
	get_next: function(){		
		var previous = this.items[this.current_item-1];
		previous.set('morph', {duration: 500, transition: Fx.Transitions.Expo.easeOut});
		previous.morph({'top': 40, 'opacity': 0}).retrieve('morph').chain(function(){
			previous.setStyle('top', 40);
			this.current_item++;
			if(this.current_item > this.items.length){ this.current_item = 1; }	
			a_item = this.items[this.current_item-1];
			a_item.set('morph', {duration: 500, transition: Fx.Transitions.Expo.easeOut});
			a_item.morph({'top': 0, 'opacity': .999}).retrieve('morph').chain(function(){
				this.timer = this.get_next.delay(4000, this);
			}.bind(this));
		}.bind(this));
	}
});
