/*
  Grafisch Arnhem
  Copyright (C) 2009 by Systemantics, Bureau for Informatics

  Lutz Issler
  Mauerstr. 10-12
  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.
*/



var marginRight = 70;

$(window).resize(function() {
	var left = $("#list").offset().left;

	// List width
	var width = $(window).width()-left-2-marginRight;
	$("#list, #list h3, #selection").width(width);

	// Overlay width
	var axis = Math.max(left, left+(width/2));
	$("#image").width(axis);
	$(".secondlayer, #axis").css("left", axis);

	// Overlay height
	var height = Math.max($(window).height(), $("#recent").height()+$("#recent").offset().top);
	if ($(".secondlayer").length>0) {
		height = Math.max(height, $(".secondlayer").height()+parseInt($(".secondlayer").css("paddingTop")));
		$("#image").height(height-parseInt($("#image").css("paddingTop")));
		$(".secondlayer")
			.height(height-parseInt($(".secondlayer").css("paddingTop")))
			.css("paddingRight", Math.max(0, width/2-parseInt($(".secondlayer").css("paddingLeft"))-$(".secondlayer").width()));
	}
	$("#axis").height(height);
});

$(window).load(function() {
	$(window).resize();
});

function addHoverToNames() {
	$("#content a").hover(
			function() {
				if ($(this).hasClass("selected")) {
					return;
				}
				$("#content .x").removeClass("x");
				$("#content a."+$(this).attr("class")).addClass("x");
				$("#content").addClass("xed");
			},
			function() {
				$("#content .x").removeClass("x");
				$("#content").removeClass("xed");
			}
		);
}

$(function() {
	if ($("#list table").length>0) {
		// Show content area
		$("#list table").after('<div id="content"></div>');

		// Show the first column by default
		var c = "";
		$("#list table td."+$("#list table td:first").attr("class")).each(function() {
			if (c!="") {
				c += ", ";
			}
			c += $(this).html();
		});
		$("#content")
			.html(c)
			.attr("class", $("#list table td:first").attr("class"));
		addHoverToNames();

		// Extend >recent< menu
		$("#recent dd a, #properties dd a")
			.hover(
				function() {
					// Show the column in question
					$("#list table").attr("class", $(this).parent().attr("class"));
					
					// IE7 is buggy here (see http://www.positioniseverything.net/explorer/ienondisappearcontentbugPIE/index.htm)
					// Work around by temporarily showing all TDs in question
					if ($.browser.msie && $.browser.version==7) {
						$("#list table td."+$(this).parent().attr("class")).css("display", "block").css("display", "");
					}

					var c = "";
					$("#list td."+$(this).parent().attr("class")).each(function() {
						if (c!="") {
							c += ", ";
						}
						c += $(this).html();
					});
					$("#content")
						.html(c)
						.attr("class", $(this).parent().attr("class"));
					addHoverToNames();

					if ($(this).parents("#recent").length) {
						// Highlight the hovered property
						$("#recent .selected").removeClass("selected");
						$(this).addClass("selected");
					}

					// Update the selection text
					$("#selection").text($("#list th."+$(this).closest("dd").get(0).className).text()+" – "+$(this).text());

					// Highlight other occurences of the hovered property
					$("#content a.selected").removeClass("selected");
					$("#content a:contains('"+$(this).text()+"')").addClass("selected");
				},
				function() {
					if ($(this).parents("#properties").length) {
						// Reset main list to the default selection
						$("#selection").text($("#properties a.selected").text());
						$("#list table").attr("class", $("#properties a.selected").parent().attr("class"));
					}
				}
			);

		$("#recent").addClass("clickable");

		// Add selection field
		$('<div id="selection"></div>')
			.text($("#list th."+$("#list table").get(0).className).text()+($("#properties a.selected").length>0 ? " – "+$("#properties a.selected").text() : ""))
			.insertAfter("#list");

		$("#list").hover(
			function() {
			},
			function() {
				$("#list").removeClass("xed");
			}
		);
	}

	// Extend details window
	$("#image").click(function() {
		location.href = "/";
	});
	if ($("#image img").length>1) {
		$("#image").addClass("clickable");
		$("#image img").click(function() {
			var index = $("#image img").index(this);
			index++;
			if (index==$("#image img").length) {
				index = 0;
			}
			$("#image img")
				.addClass("hidden")
				.eq(index).removeClass("hidden");
			return false;
		});
	}
	$("#image .quicktime .button").click(function() {
		return false;
	});

	// Add axis
	$("body").append('<div id="axis"></div>');
	
	// Open external links in a new window
	$("a[href^=http://], a.external").click(function() {
		window.open(this.href);
		return false;
	});
});

