var pe = null;
var currentOverlay = null;
var carousel = null;
var firstTime = true;

function start() {
	carousel = new UI.Carousel("team_carousel");

	pe = new PeriodicalExecuter(function(pe) {
				 $('team_carousel_next_button').simulate('click');
			}, 5);	

	showOverlay();
}

function showOverlay() {
	if (!carousel.animating) {
		var overlay = $('co_' + carousel.currentIndex());

		if (overlay) {
			$('co_' + carousel.currentIndex()).fade({ duration: 1.0, from: 0, to: 0.99 });
			currentOverlay = overlay;
		}
	}

	if (!firstTime && carousel.currentIndex() == 0) {
		pe.stop();
	}
	firstTime = false;
}

Event.observe(window, "load", start);
Event.observe($('team_carousel_previous_button'), 'click', function() { pe.stop(); });
Event.observe($('team_carousel_user_next_button'), 'click', function() { pe.stop(); });
/* Event.observe($('team_carousel_next_button'), 'click', function() { currentOverlay.morph("opacity: 0", { duration: 0.5 }); } ); */
Event.observe($('team_carousel_next_button'), 'click', function() { currentOverlay.fade({ duration: 0.5, from: 0.99, to: 0.1 }); } );
document.observe('carousel:scroll:ended', showOverlay);
