/*
  Space To Take Place
  Copyright (C) 2008 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.
*/



var initialHeight = 0;

function resized() {
	var jWindow = $(window);
	var width = Math.max(
		jWindow.width(),
		1000
	);
	var height = Math.max(
		jWindow.height(),
		initialHeight
	);
	$("#background")
		.width(width)
		.height(height);
	$(".bar").height(height);
	$("#lines")
		.width(width)
		.height(height);
	$(".rule.wide").width(width);
	if (jWindow.width()>1000) {
		// Centered content
		$("#main")
			.css("left", "50%")
			.css("marginLeft", "-490px");
	} else {
		// Left-attached content
		$("#main")
			.css("left", "")
			.css("marginLeft", "");
	}
}

$(window).resize(resized);

var sttpTime;

$(function () {
	// Make columns
	$("#content.columns").columnize({columns: 3, balance: false, height: $("#content").height()});

	// Make external links functional
	$("#thumbnails a")
		.add("a.external")
		.add("#content.text a:not(.internal)")
		.add(".bottom a")
		.click(function () {
			this.blur();
			var hrefStart = $(this).attr("href").substr(0, 1);
			if (this.className!="external" && (hrefStart=="/" || hrefStart==".")) {
				// Internal link
				location.href = this.href;
			} else {
				// External link
				window.open(this.href);
			}
			return false;
		});
	
	// Open PDF in new window
	$("#owners a").click(function() {
		this.blur();
		window.open(this.href);
		return false;
	});

	// Add labels
	$("#content form div")
		.find("label")
			.overlabel();

	// Add AJAX
	$("form.reaction").submit(function() {
		var jForm = $(this);
		$.ajax({
			type: $(this).attr("method"),
			url: $(this).attr("action"),
			data: $(this).serialize(),
			dataType: "json",
			success: function(msg) {
				if (msg.ok) {
					// Comment was posted
					location.reload();
				} else {
					// Something went wrong
					if (msg.spam) {
						// Msg is spam
						$("div.column", jForm).replaceWith("Thanks for your interest in our site. Your reaction was detected to be spam. If this is not the case (although we doubt it), please contact the <a href=\"/contact\">website administrator</a>.")
					} else {
						// Fields are missing
						$("div.reaction", this).removeClass("required");
						for (var i=0; i<msg.missing.length; i++) {
							jFieldContainer = $("#reaction_"+msg.missing[i]).parent()
							jFieldContainer.addClass("required");
							if (msg.label && i<msg.label.length) {
								jFieldContainer.find("label").text(msg.label[i]);
							}
						}
					}
				}
			}
		});
		return false;
	});
	$("form.reaction :input[type=submit]").throbber({image: "/elements/throbber.gif"});

	// Add text counting
	function limit(el) {
		if (el.value.length>900) {
			el.value = el.value.substr(0, 900);
			$("#"+el.id+"Down").text("0");
		}
	}
	$(":input[type=textarea]")
		.textCounting({maxLengthSource: "list", maxLengthList: "900"})
		.change(function() {
			limit(this);
		})
		.keyup(function() {
			limit(this);
		});

	// Add image hovers
	$('.bottom.partners img').imghover({
		suffix: 'hover'
	});

	// Add clock
	var start = (new Date()).getTime()-sttpTime;
	function timeFormat(v) {
		return (v<10 ? "0" : "")+v;
	}
	window.setInterval(function() {
		var now = new Date((new Date()).getTime()-start);
		$("#dateYear").text(now.getFullYear());
		$("#dateMonth").text(timeFormat(now.getMonth()+1));
		$("#dateDay").text(timeFormat(now.getDate()));
		$("#timeHours").text(timeFormat(now.getHours()));
		$("#timeMinutes").text(timeFormat(now.getMinutes()));
		$("#timeSeconds").text(timeFormat(now.getSeconds()));
	}, 500);

	// Add fixes for Internet Explorer 6
	if ($.browser.msie && $.browser.version<"7.0") {
		$("#logo").html('<div style="width:980px;height:69px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'/elements/logo.png\', sizingMethod=\'scale\');"></div>');
		$(".thumbnail").hover(
			function() {
				$(".caption", this).show();
				$("img", this).css("opacity", 1);
			},
			function() {
				$(".caption", this).hide();
				$("img", this).css("opacity", .6);
			}
		);
	}

	// Adjust positions
	initialHeight = $("#background").height();
	resized();
});

window.onload = function() {
	preloadImages();
}
