/*
  carlier | gebauer
  Copyright (C) 2011 by Systemantics, Bureau for Informatics

  Systemantics GmbH
  Alte Poststr. 38
  47877 Willich
  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.
*/



$(function() {
	// Detect scrollbar presence
	function hasVerticalScrollbar() {
		return document.documentElement.scrollHeight>document.documentElement.clientHeight;
	}

	// Detect scrollbar width
	function getScrollbarWidth() {
		var probe = $('<div style="margin:0;padding:0;overflow:auto;height:100px;width:100px;"><div style="margin:0;padding:0;width:100%;height:101px;"></div></div>');
		probe.appendTo("body");
		var w = probe.width()-probe.children("div").width();
		probe.remove();
		return w;
	}

	$("#ticker")
		.append($("#ticker>*").clone())
		.webTicker({
			travelocity: .05*.7
		})
		.css("visibility", "visible");

	$("#images > div")
		.each(function () {
			var origImage = $("img", this);
			var imageDiv = $(this);
			var src = origImage.get(0).src;
			imageDiv
				.data("gray", src)
				.data("color", src.replace("/g_", "/s_"))
				.data("caption", origImage.attr("alt"))
		})
		.bind("showColor", function () {
			$("img", this).get(0).src = $(this).data("color");
		})
		.bind("showGray", function () {
			$("img", this).get(0).src = $(this).data("gray");
		})
		.hover(
			function () {
				$("#item_"+this.id.substr(6)+" a").addClass("hover");
				$(this).trigger("showColor");
			},
			function () {
				$(".hover").removeClass("hover");
				$(this).trigger("showGray");
			}
		);

	$(".item a").hover(
		function () {
			$("#image_"+$(this).closest(".item").attr("id").substr(5)).trigger("showColor");
		},
		function () {
			$("#image_"+$(this).closest(".item").attr("id").substr(5)).trigger("showGray");
		}
	);

	var oldHash = "";
	var watchHashChange = function() {
		if (location.hash!=oldHash) {
			oldHash = location.hash;
			var oldScrollTop = $(window).scrollTop();
			if (location.hash.substr(1, 3)=="img") {
				// Show enlarged image
				var imageDiv = $("#image_"+location.hash.substr(4));
				var img = new Image();
				img.onload = function () {
					// Hide enlarged image
					$(".enlarged").each(function () {
						$(this)
							.removeClass("enlarged")
							.children("div").remove();
					});
					var ratio = Math.min(
						($("#images").width()-(hasVerticalScrollbar() ? 0 : getScrollbarWidth()))/img.width,
						($(window).height()-(134+10)*2)/img.height
					);
					imageDiv.append($('<div><img src="'+img.src+'" width="'+(img.width*ratio)+'" height="'+(img.height*ratio)+'" alt="" /><div class="caption">'+imageDiv.data("caption")+'</div></div>')).addClass("enlarged");
					$(window).scrollTop(oldScrollTop);
				};
				img.src = imageDiv.data("color").replace("/s_", "/");
			}
		}
		window.setTimeout(watchHashChange, 100);
	};
	watchHashChange();

	$("a[href^='http://']").click(function (e) {
		this.blur();
		window.open(this.href);
		e.preventDefault();
	});
});

