$(document).ready(function() {
	$("a#1").click(function() { show(0); return false; });
	$("a#2").click(function() { show(1); return false; });
	$("a#3").click(function() { show(2); return false; });
	$("a#4").click(function() { show(3); return false; });
	$("a#5").click(function() { show(4); return false; });
	$("a#6").click(function() { show(5); return false; });
	var images = $("#gallery img");
	var thumbs = $("#thumbs img");
	var thumblinks = $("#thumbs a");
	var index = thumbs.length-1;
	var thumbsHeight = 146;
	thumblinks.slice(thumblinks.length-3,thumblinks.length).clone().attr('id', function() { var id = $(this).id; return 'b' + this.id}).prependTo("#thumbs");
	$("a#b4").click(function() { show(3); return false; });
	$("a#b5").click(function() { show(4); return false; });
	$("a#b6").click(function() { show(5); return false; });
	for (i=0; i<thumbs.length; i++) {
		$(thumbs[i]).addClass("thumb-"+i);
		$(images[i]).addClass("image-"+i);
	}
	show(index);
	setInterval(sift, 10000);
	function sift() {
		if ( index < thumbs.length && index > 0) {index -= 1 ; }
		else { index = thumbs.length-1 }
		show ( index );
	}
	function show(num) {
		images.fadeOut(600);
		$(".image-"+num).stop().fadeIn(600);
		var scrollPos = ( (num-1) * thumbsHeight ) + 146;
		$("#thumbs").stop().animate({scrollTop: scrollPos}, 600);
	}
});
