window.addEvent('domready', function() { 

	// Let's define some variables first
	var wrapper = $('wrap'); // The outer wrapper
	var carousel = $('carousel'); // The inner wrapper
	var items = $$('#carousel li'); // The different elements, this is an array
	var item_width = 500; // The full width of a single item (incl. borders, padding, etc ... if there is any)
	var max_margin = items.length * item_width - item_width;
	var frame_delay = 5000; // The delay in microseconds between each auto-scrolled frame
	
	// Set up the animation
	var animation = new Fx.Tween(carousel, {duration: 500});
	
	// The function to browse forward
	function next_item(pos){
		if(pos == -max_margin){
			animation.start('left', 0);
		} else { 
			var newposition = pos - item_width;
			animation.start('left', newposition);
		}
	}
	
	// The function to browse backward
	function previous_item(pos){
		if(pos == 0){
			animation.start('left', -max_margin);
		} else { 
			var newposition = pos + item_width;
			animation.start('left', newposition);
		}
	}
	
	// Set up the 'next' and 'previous' buttons
	$('next').addEvent('click', function(){
		var position = parseInt(carousel.getStyle('left'));
		next_item(position);
	});
	
	$('previous').addEvent('click', function(){
		var position = parseInt(carousel.getStyle('left'));
		previous_item(position);
	});

	// Automatically flip through the featured stories every five seconds.
	// The downside to this is that it just keeps flipping, even if the viewer has already
	// interacted with it, or is currently interacted it. This can make it frustrating to use.
	// Determine how to disable repeat.periodical after the user manually invokes next_item() or
	// previous_item().
	var repeat = function() {
		var position = parseInt(carousel.getStyle('left'));
		next_item(position);
	};
	//repeat.periodical(frame_delay);

});