/*
  KCAP
  Copyright (C) 2011 by Systemantics, Bureau for Informatics

  Systemantics GmbH
  Am Lavenstein 3
  52064 Aachen
  GERMANY

  Web:    www.systemantics.net
  Email:  hello@systemantics.net

  Permission granted to use the files associated with this
  website only on your webserver.

  Changes to these files are PROHIBITED due to license restrictions.
*/



var fadeDuration = 250;
var fadeOpacity = .5;

$(function() {
	$("#welcomecn").each(function() {
		$(this).appendTo("body");
		var img = $("#welcomecn-image");

		img.hide();
		var newImage = new Image();
		$(newImage).load(function() {
			img.data("width", newImage.width);
			img.data("height", newImage.height);
			$(window)
				.resize(function() {
					var w = $(this).width();
					var h = $(this).height();
					var iw = img.data("width");
					var ih = img.data("height");
					var scale = Math.max(w/iw, h/ih);
					var newWidth = iw*scale;
					var newHeight = ih*scale;
					img
						.width(newWidth)
						.height(newHeight)
						.css({
							marginLeft: (-(newWidth-w)/2)+"px",
							marginTop: (-(newHeight-h)/2)+"px"
						})
						.show();
				})
				.resize();
		});
		newImage.src = img.attr("src");
	});

	(function() {
		var h = 0;
		$(".bottom-aligned").each(function() {
			h = Math.max(h, $(this).outerHeight());
		});
		if (!h) {
			return;
		}
		var origY = $(".bottom-aligned").position().top;
		$(window)
			.resize(function() {
				$(".bottom-aligned").css("top", Math.min(origY, $(this).height()-h-9)+"px");
			})
			.resize();
	})();

	var f2 = false;
	$(".item")
		.hover(
			function() {
				if (f2) {
					$(".item img").stop(true).not($("img", this)).fadeTo(fadeDuration, 1);
					$("img", this).stop(true).fadeTo(fadeDuration, fadeOpacity);
					return true;
				}
				$(".item img").stop(true).not($("img", this)).fadeTo(fadeDuration, fadeOpacity);
				$("img", this).stop(true).fadeTo(fadeDuration, 1);
			},
			function() {
				if (f2) {
					$(".item img").delay(1500).queue(function() {
						$(this).stop(true).fadeTo(fadeDuration, 1);
					});
				}
				$(".item img").delay(1500).queue(function() {
					$(this).stop(true).fadeTo(fadeDuration*10, 1);
				});
			}
		);
	
	$("#project-image").wrapInner("<div></div>");
	$("#project .images img").css("opacity", .5);
	$("#project .images img.selected").css("opacity", 1);
	$("#project .images a")
		.hover(
			function() {
				$("#project .images img").stop().not($("img", this)).fadeTo(fadeDuration, fadeOpacity);
				$("img", this).stop().fadeTo(fadeDuration, 1);
			},
			function() {
				$("#project .images img:not(.selected)").stop().fadeTo(fadeDuration, fadeOpacity);
				$("#project .images img.selected").stop().fadeTo(fadeDuration, 1);
			}
		)
		.click(function() {
			this.blur();
			if ($("img", this).is(".selected")) {
				return false;
			}
			var clicked = $(this);
			$(".images .selected").removeClass("selected");
			$("img", clicked).addClass("selected");
			$("html,body").animate({scrollTop: 0}, fadeDuration);
			$("#project-image div").fadeOut(fadeDuration, function() {
				var img = new Image();
				img.onload = function() {
					$("#project-image img").attr("src", img.src);
					$("#project-image div").fadeIn(fadeDuration);
					$("#project-image a").attr("href", clicked.attr("href")+"f");
				}
				img.src = $("img", clicked).attr("src").replace("/s_", "/p_");
			});
			return false;
		});
	$("#project-image a").click(function() {
		$('<div id="fullscreen"></div>')
			.appendTo("body")
			.load(this.href+" #fullscreen > *", function() {
				initFullscreen();
			});
		return false;
	});
	var allImages = $("#project .images .image");
	$("#image-prev").click(function() {
		var index = allImages.index($("img.selected").closest(".image"));
		allImages.eq(index==0 ? allImages.length-1 : index-1).find("a").click();
	});
	$("#image-next").click(function() {
		var index = allImages.index($("img.selected").closest(".image"));
		allImages.eq(index<allImages.length-1 ? index+1 : 0).find("a").click();
	});

	function initFullscreen() {
		$("#fullscreen .image").each(function() {
			$("img", this).hide();
			var img = $(new Image);
			img.load(function() {
				$("#fullscreen .image img").remove();
				$("#fullscreen .image").append(img);
				var w = img.width();
				var h = img.height();
				$(window).resize(function() {
					var scale = Math.min($(window).width()/w, $(window).height()/h);
					img
						.width(w*scale)
						.height(h*scale)
						.css("margin-top", ($(window).height()-img.height())/2);
				}).resize();
				$("#fullscreen-control")
					.css("opacity", .3)
					.delay(3000)
					.fadeTo(fadeDuration, 0);
			})
			img.attr("src", $("img", this).attr("src"));
			$("#fullscreen-control").hover(
					function() {
						$("#fullscreen-control").fadeTo(fadeDuration, .3);
					},
					function() {
						$("#fullscreen-control").stop(true).fadeTo(fadeDuration, 0);
					}
				);
			function fullscreenClose() {
				if ($("#container-fixed").length==0) {
					location.href = $("#fullscreen-close a").attr("href");
				} else {
					$("#fullscreen").remove();
				}
				return false;
			}
			function fullscreenNav(href) {
				if ($("#container-fixed").length==0) {
					location.href = href;
				} else {
					$("#fullscreen").load(href+" #fullscreen > *", function() {
						initFullscreen();
					});
				}
			}
			function fullscreenNext() {
				fullscreenNav($("#fullscreen-next a").attr("href"));
				return false;
			}
			function fullscreenPrev() {
				fullscreenNav($("#fullscreen-prev a").attr("href"));
				return false;
			}
			$("#fullscreen-close a").click(fullscreenClose);
			$("#fullscreen-next a").click(fullscreenNext);
			$("#fullscreen-prev a").click(fullscreenPrev);
			$(document).keyup(function(e) {
				switch (e.which) {
					case 27:
						fullscreenClose();
						break;
					case 32:
					case 39:
						fullscreenNext();
						break;
					case 37:
						fullscreenPrev();
						break;
				}
			});
		});
	}
	initFullscreen();

	$("#search-q,#subscribe-mail").each(function() {
		$(this)
			.focus(function() {
				$("label[for="+this.id+"]").hide();
			})
			.blur(function() {
				if ($.trim($(this).val())=="") {
					$("label[for="+this.id+"]").show();
				}
			});
		if ($.trim($(this).val())!="") {
			$("label[for="+this.id+"]").hide();
		}
	});

	$("#project-menu li ul").hover(
		function() {
			$(this).prev("span").addClass("here");
		},
		function() {
			$(this).prev("span").removeClass("here");
		}
	);
	
	$(".text li").prepend("– ");
	
	$("#submenu a").click(function() {
		this.blur();
		var newHash = this.hash;
		var target = $(this.hash).offset().top;
		var oldLocation = window.location.href.replace(window.location.hash, "");
		var newLocation = this;
		if (oldLocation+newHash==newLocation) {
			$($.browser.safari ? "body:not(:animated)" : "html:not(:animated)").animate({ scrollTop: target-123 }, 500);
		}
		return false;
	});

	$("a[href^=http://],a._blank").click(function() {
		this.blur();
		window.open(this.href);
		return false;
	});
});

