$(document).ready(function() {

	var active = 0; // starts at zero
	var list = $('ul.imagelist');
	
	list.children('li').eq('0').show().siblings().hide();
	
	// add last and next buttons dynamically so they only appear if javascript is enabled
	
	if (list.children().length > 1) {
		var controls = list.after("<div id='controls'/>");
		$('#controls').append("<a class='prev'>Previous</a><a class='next'>Next</a>")
	}
	
	

	$('.next').bind('click', function() {
		active = active == list.children('li').length-1 ? 0 : active + 1;
	});

	$('.prev').bind('click', function() {
		active = active == 0 ? list.children('li').length-1 : active - 1;
	});


	var getActive = function() {
		return list.children('li').eq(active);
	};

	$('.prev,.next').bind('click', function() {
		getActive().show().siblings().hide();
	});


});
