$(document).ready(
	function(){
		$(".referenceItem").find(".referenceImg img:last",this).css({
			"display":"none",
			"opacity":"0.0",
			"z-index":"80"
		}).find(".referenceImg img:first",this).css({
			"opacity":"1.0",
			"z-index":"70"
		});
		$(".referenceImg").append('<div class="referenceImgButtons">');
		$(".referenceImgButtons").append('<a href="" title="Ansicht 1" class="iconCircle1active"></a>');
		$(".referenceImgButtons").append('<a href="" title="Ansicht 2" class="iconCircle2inactive"></a>');
		$(".referenceItem").find(".referenceImgButtons a:first",this).click(
			function(e){
				e.preventDefault();
				$(this).blur();
				$(this).parents(".referenceImg").find("img:last").stop().animate({"opacity":"0.0"},500, function(){
					$(this).css({"display":"none"});
				});
				$(this).next("a").removeClass("iconCircle2active").addClass("iconCircle2inactive");
				$(this).removeClass("iconCircle1inactive").addClass("iconCircle1active");
			}
		);
		$(".referenceItem").find(".referenceImgButtons a:last",this).click(
			function(e){
				e.preventDefault();
				$(this).blur();
				$(this).parents(".referenceImg").find("img:last").stop().show().animate({"opacity":"1.0"},500);
				$(this).prev("a").removeClass("iconCircle1active").addClass("iconCircle1inactive");
				$(this).removeClass("iconCircle2inactive").addClass("iconCircle2active");
			}
		);
		
		/* ==================================== */
		$(".referenceImgBig").click(
			function(e){
				e.preventDefault();
				var vWinX = $(window).width();
				var vWinY = $(window).height();
				var vDocY = $(document).height();
				var vHref = $(this).attr("href");
				var vTitle = $(this).attr("title");
				var vRel = $(this).attr("rel");
				var vPosition = $(window).scrollTop() + ($(window).height()/2);
				$("body").append('<div id="referenceShaddow"></div>');
				$("#referenceShaddow").animate({"opacity":"0.4"},1000).height(vDocY);
				$("body").append('<div id="referenceGalleryBox"><a id="referenceImgGalleryReady" href="'+vRel+'" title="'+vTitle+'" target="_blank"><img id="referenceImgGallery" src="'+vHref+'" title="'+vTitle+'" /></a></div>');
				$("#referenceGalleryBox").css({"width":vWinX+"px","height":vWinY+"px"});
				$("#referenceImgGallery").load(
					function(){
						var vImgX = $("#referenceImgGallery").width()/2;
						var vImgY = $("#referenceImgGallery").height()/2;
						$(this).css({
							"margin-left":"-"+vImgX+"px",
							"margin-top":"-"+vImgY+"px"
						}).css({"top":vPosition+"px"}).animate({"opacity":"1.0"},1500);
					}
				);
				$(window).scroll(
					function(){
						var vPosition = $(this).scrollTop() + ($(window).height()/2);
						$("#referenceImgGallery").animate({"top":vPosition+"px"},500);
					}
				);
				$("#referenceShaddow, #referenceGalleryBox").click(
					function(){
						$("#referenceImgGallery").animate({"opacity":"0.0"},500, function(){
							$(this).remove();
						});
						$("#referenceShaddow").animate({"opacity":"0.0"},1000, function(){
							$(this).remove();
						});
						$("#referenceGalleryBox").remove();
						$("#referenceImgGallery").die();
					}
				);
			}
		);
	}
);
