/*
  Systemantics 2.0
  Copyright (C) 2010 by Systemantics, Bureau for Informatics

  Systemantics GmbH
  Am Lavenstein 3
  52064 Aachen
  GERMANY

  Web:    www.systemantics.net
  Email:  mail@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.
*/



$(function() {
	$(".menu ul li:first-child").click(function() {
		location.href = $(this).parent("ul").find("li:eq(1) a").attr("href");
	});

	if ($.browser.msie && $.browser.version.substr(0, 1)=="7") {
		var lastMenu = false;
		$(".menu").hover(
			function() {
				lastMenu = $(this);
				$(this).addClass("hover");
			},
			function() {
			}
		);
		$('<div id="menuback"></div>')
			.mouseenter(
				function() {
					if (lastMenu) {
						lastMenu.removeClass("hover");
					}
				}
			)
			.appendTo("body");
	}

	$(".project, .example").css("height", "32px");
	$(".project h2 a, .project h2, .example h2").click(function() {
		this.blur();
		var p = $(this).parents(".project, .example");
		var img = $("img", p);
		if (p.hasClass("expanded")) {
			// Collapse
			if (img.length>0) {
				$("body").css("background-image", "none");
			}
			p.removeClass("expanded");
			p.animate({height: 32}, 250);
		} else {
			// Expand
			if (img.length>0) {
				$("body").css("background-image", "none");
			}
			$(".expanded")
				.removeClass("expanded")
				.animate({height: 32}, 500);
			p.addClass("expanded");
			p.animate({height: $(".details", p).height()+32}, 500, function() {
				if (img.length>0) {
					$("body").css({
						backgroundImage: "url("+$("img", p).attr("src")+")",
						backgroundPosition: "171px "+p.offset().top+"px"
					});
				}
			});
			if (p.is(".project")) {
				location.hash = p.attr("id").substr(1);
				pageTracker._trackEvent("Projects", "View", p.attr("id").substr(1));
			}
		}
		return false;
	});
	var p = $("#p"+location.hash.substr(1));
	var year = $("#p"+location.hash.substr(1)).parents(".year");
	p
		.css("height", "auto")
		.addClass("expanded");
	var img = $("img", p);
	if (img.length>0) {
		$("body").css({
			backgroundImage: "url("+$("img", p).attr("src")+")",
			backgroundPosition: "171px "+p.offset().top+"px"
		});
	}
	if ($(".year").index(year)>0) {
		$(window).scrollTop(year.offset().top-24);
		$("body").css({
			backgroundImage: "url("+$("img", p).attr("src")+")",
			backgroundPosition: "171px "+p.offset().top+"px"
		});
	}

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

	$(".cols").columnize({
		columns: 2,
		column: "col text",
		balance: true
	});

	$(".details p, .details dt, .details dd").wrapInner("<span></span>");

	if ($("#circle").length==1) {
		$("#circle a").wrap("<div></div>");
		$("#circle > div").css("paddingTop", (119-$("#circle > div").height())/2-5);
		$("#circle").draggable({
			stop: function(event, ui) {
				$.cookie("circle-x", ui.position.left, {path: "/", expires: 365});
				$.cookie("circle-y", ui.position.top, {path: "/", expires: 365});
			}
		});
		var x = $.cookie("circle-x");
		var y = $.cookie("circle-y");
		if (x && y) {
			$("#circle").css({
				left: x+"px",
				top: y+"px"
			})
		}
	}

	$("#newsletter input[type=text]").each(function() {
		var label = $("label[for="+this.id+"]");
		var input = $(this);
		if (!label.length) {
			return true;
		}
		$(this).wrap("<div></div>").closest("div").prepend(label);
		label.css({
			color: "#999999",
			cursor: "text",
			display: input.val() ? "none" : "block",
			position: "absolute",
			width: input.width(),
			paddingLeft: (parseInt(input.css("padding-left"))+1)+"px",
			paddingTop: input.css("padding-top"),
			paddingBottom: input.css("padding-bottom"),
			paddingRight: input.css("padding-right"),
			marginLeft: input.css("margin-left"),
			marginTop: input.css("margin-top"),
			marginBottom: input.css("margin-bottom"),
			marginRight: input.css("margin-right"),
			fontFamily: input.css("font-family"),
			fontSize: input.css("font-size"),
			fontWeight: input.css("font-weight"),
			lineHeight: input.css("line-height"),
			textTransform: input.css("text-transform"),
			height: input.height()
		});
		label.click(function() {
			input.focus();
			label.css("display", "none");
		});
		input.blur(function() {
			label.css({
				color: "#b1b3b4",
				display: input.val() ? "none" : "block"
			});
		});
	});
});
