window.addEvent('domready', function() {
	var content1 = new Fx.Slide('content1').show();
	var content2 = new Fx.Slide('content2').hide();
	var content3 = new Fx.Slide('content3').hide();
	var content4 = new Fx.Slide('content4').hide();
	var currentIndex = 1;
	var imageCount = 5;
	
	function fade(from, to)
	{
		if(to == from) return;
		if($(from).fx){$(from).fx.stop();}
		if($(to).fx){$(to).fx.stop();}
		
		$(from).fx = $(from).effect('opacity', {duration: 2000}).start(0);
		$(to).fx = $(to).effect('opacity', {duration: 2000}).start(1);
	}
	
	var refresh = (function () {
		var nextIndex = (currentIndex % imageCount) + 1;
		fade('content1_image' + currentIndex, 'content1_image' + nextIndex);
		currentIndex = nextIndex;
	});
	
	var periodical = refresh.periodical(8000, this);
	
	$('button1').addEvent('click', function(e){
		e = new Event(e);
		content1.slideIn();
		content2.slideOut();
		content3.slideOut();
		content4.slideOut();
		e.stop();
	});
	$('button2').addEvent('click', function(e){
		e = new Event(e);
		content2.slideIn();
		content1.slideOut();
		content3.slideOut();
		content4.slideOut();
		e.stop();
	});
	$('button3').addEvent('click', function(e){
		e = new Event(e);
		content3.slideIn();
		content2.slideOut();
		content1.slideOut();
		content4.slideOut();
		e.stop();
	});
	$('button4').addEvent('click', function(e){
		e = new Event(e);
		content4.slideIn();
		content2.slideOut();
		content3.slideOut();
		content1.slideOut();
		e.stop();
	});
});
