var references = null;
var imgCount = 0;
var currentImgNr = 0;
$(document).ready(function(){
	$("div#references.rotate ul li a").each(function() {
			var newImage = this.rel;
			var alreadyAdded = $("#images img[src=" + newImage + "]").length > 0 ? true : false;
			$(this).attr("id", "thumb" + imgCount);
			if (!alreadyAdded) {
				$("#images").append('<img id="img' + imgCount + '" width="850" class="hidden" src="' + newImage + '" alt="" title="" />');
			}
			else {
				$("#images img[src=" + newImage + "]").attr("id", "img" + imgCount);
				currentImgNr = imgCount;
			}
			imgCount++;
	});
	$("#thumb" + currentImgNr + " span").css({
		"opacity": 0
	})
	$("div#references.redirectlinks ul li a").click(function() {
		if( interval ) window.clearInterval(interval);
		$("#images img, #references span").stop();
		var selectedImgNr = $(this).attr("id").substring(5);
			$("#images img:not(#img" + selectedImgNr + ")").css({
				"display": "none",
				"z-index": 10
			})
			$("#img" + selectedImgNr).css({
				"display": "block",
				"z-index": 20
			})
			$("#references span").css({
				"opacity": 0.7
			});
			$("#thumb" + selectedImgNr + " span").css({
				"opacity": 0
			});
		return false;
	})
	if (imgCount > 1) {
		var interval = window.setInterval(function(){
			recentImgNr = currentImgNr;
			currentImgNr++;
			if (currentImgNr >= imgCount) {
				currentImgNr = 0;
			}
			$("#img" + currentImgNr).css({
				"z-index": 100
			});
			$("#img" + currentImgNr).fadeIn(2000, function(){
				$("#images img:not(#img" + currentImgNr + ")").css({
					"display": "none",
					"z-index": 10
				})
				$("#img" + currentImgNr).css({
					"z-index": 20
				})
			});
			$("#thumb" + recentImgNr + " span").css({
				"opacity": 0
			}).animate({
				"opacity": 0.7
			}, 2000, "linear", function(){
			});
			$("#thumb" + currentImgNr + " span").css({
				"opacity": 0.7
			}).animate({
				"opacity": 0
			}, 2000, "linear", function(){
			});
		}, 5000);
	}
})
