var mmUpdates = new Class({
	options: {
		showDuration: 3000
	},
	Implements: [Options,Events],
	initialize: function(container,handles,options) {	
		this.update = $('updatesBox');
		if (!this.update) return; 
		var that = this;
		$$(handles+' a').each(function(a, i){			
			var title = a.get('title');
			var href = a.get('href');
			var nimg = a.get('rev');
			var desc = a.getParent('li').getElement('span').get('text');
			var upd = {'title':title, 'href':href, 'img':nimg, 'desc':desc};
			a.addEvent('click', function(e){
				if(!a.getParent('li').hasClass('active')){
					e.stop();
				} 
				that.stop();
				that.show(i);
			});			
			if(!$('upd_a_'+i)){
				var upd_a = new Element('a', {id: 'upd_a_'+i, href:upd.href, title:upd.title});
				var img = new Element('img', {id: 'upd_img_'+i, src:upd.img, alt:upd.title});
				var span = new Element('span', {id: 'upd_span_'+i, text:upd.desc, opacity: 0.75});
				upd_a.adopt(img,span);
				$(container).adopt(upd_a);
				upd_a.addClass('pr');
				upd_a.fade('hide');
				span.fade('hide');
			}else{
				$('upd_span_'+i).fade(0.7);
			}
		}); 
		this.handles = handles;
		this.container = $(container);
		this.elements = $$('#'+container+' a');
		this.currentIndex = 0;
		this.interval = '';
		this.container.addEvents({
			mouseenter: function() { this.stop(); }.bind(this),
			mouseleave: function() { this.start(); }.bind(this)
		});
	},
	show: function(to) {
		this.elements[this.currentIndex].fade('out');
		$('upd'+this.currentIndex).removeClass('active');
		$('upd_span_'+this.currentIndex).fade('hide');
		this.currentIndex = ($defined(to) ? to : (this.currentIndex < this.elements.length - 1 ? this.currentIndex+1 : 0));
		this.elements[this.currentIndex].fade('in');
		$('upd'+this.currentIndex).addClass('active');
		$('upd_span_'+this.currentIndex).fade(0.7);
	},
	start: function() {
		this.stop();
		this.interval = this.show.bind(this).periodical(this.options.showDuration);
	},
	stop: function() {
		$clear(this.interval);
	}
});
window.addEvent('domready',function() {	
	var updates = new mmUpdates('updateImg','#updateDesc');
	updates.start();	
});
