var swap_speed = 'fast';

function build_slideshow(){
	var screenshots = $('div#screenshots ul/li');
	if(screenshots.length > 1){
		// number nav
		var nav = $('div#screenshots').append('<ol></ol>').find('ol');
		screenshots.each(function(i){
			var item = nav.append('<li>' + (i + 1) + '</li>');
		});
		var current_index = 0;
		$('div#screenshots ol/li').each(function(i){
			$(this).bind('click', function(){
				var t = this;
				$('div#screenshots ul/li.current').fadeOut(swap_speed, function(){
					$('div#screenshots li.current').removeClass('current');
					$($(screenshots).eq(i)).fadeIn(swap_speed);
					$($(screenshots).eq(i)).addClass('current');
					$(t).addClass('current');
				});
				current_index = i;
			});
			if(i == 0){
				$(this).addClass('current');
			}
		});
		// prev/next nav
		$('div#screenshots').append('<span class="previous"></span><span class="next"></span>').find('span').each(function(i){
			$(this).bind('click', function(){
				$(this).attr("class") == 'next' ? current_index++ : current_index--;
				current_index = current_index < 0 ? $(screenshots).length - 1 : current_index >= $(screenshots).length ? 0 : current_index;
				var t = this;
				$('div#screenshots ul/li.current').fadeOut(swap_speed, function(){
					$('div#screenshots li.current').removeClass('current');
					$($(screenshots).eq(current_index)).fadeIn(swap_speed);
					$($(screenshots).eq(current_index)).addClass('current');
					$($('div#screenshots ol/li').eq(current_index)).addClass('current');
				});
			});
		});
	}
}

$(build_slideshow);

