/*
  Half Full
  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() {
	var largeAd = $("#ads .large");
	if ($("#ads .ad").index(largeAd)>0) {
		// Move the large ad to the front and right-align it
		largeAd.css("float", "right").prepend("#ads");
	}

	$("#ads .ad").each(function() {
		if ($("a", this).length>0) {
			$(this).clone()
				.insertAfter(this)
				.css({
					left: $(this).offset().left,
					position: "absolute",
					top: $(this).offset().top
				})
				.hover(
					function() {
						$(this).fadeTo(250, .01);
					},
					function() {
						$(this).fadeTo(250, 1);
					}
				);
			$(this).addClass("hover");
		}
	});

	$("#moreinfo h2").click(function() {
		var c = $("#moreinfo");
		if (c.height()>24) {
			// Current status is open, so close
			c.animate({height: "24px"}, 250);
		} else {
			// Current status is closed, so open
			var h = 0;
			c.find(".content").each(function() {
				h = Math.max(h, $(this).height());
			});
			h += 36;
			c.animate({height: h+"px"}, 250);
		}
	});

	$("#print-button").click(function() {
		window.print();
	});

	$(".narrow").each(function() {
		$("li:last-child:not(:last)", this).addClass("continued");
	});

	if ($(".photo").length>0) {
		$(".photo a").lightBox({
			imageLoading: "/elements/lightbox-ico-loading.gif",
			imageBtnClose: "/elements/lightbox-btn-close.gif",
			imageBtnPrev: "/elements/lightbox-btn-prev.gif",
			imageBtnNext: "/elements/lightbox-btn-next.gif",
			imageBlank: "/elements/lightbox-blank.gif",
		});
	}

	$("#email-button").hover(
		function() {
			this.src = this.src.replace(/email\.gif/, "email_on.gif"); 
		},
		function() {
			this.src = this.src.replace(/email_on\.gif/, "email.gif"); 
		}
	);

	var chunkHeights = new Array();
	var totalHeight = 0;
	$("#menu-categories .category").each(function() {
		var h = $(this).height();
		totalHeight += h;
		chunkHeights.push(h);
	});
	var colHeight = totalHeight/3;
	var bestSplits = false;
	var bestValue = 10000;
	for (var i1=1; i1<chunkHeights.length; i1++) {
		for (var i2=i1+1; i2<chunkHeights.length; i2++) {
			var splits = new Array(i1, i2);
			var value = 0;
			var lastSplit = 0;
			for (var j=0; j<splits.length; j++) {
				var sum = 0;
				for (var i=lastSplit; i<splits[j]; i++) {
					sum += chunkHeights[i];
				}
				value = Math.max(Math.abs(sum-colHeight), value);
				if (value>bestValue) {
					// Cut off if we're bigger than the best value
					break;
				}
				lastSplit = splits[j];
			}
			if (value<bestValue) {
				// Store new best value
				bestValue = value;
				bestSplits = splits;
			}
		}
	}
	var lastSplit = 0;
	for (var i=0; i<bestSplits.length; i++) {
		$("#menu-categories > .category:lt("+(bestSplits[i]-lastSplit)+")").wrapAll('<div class="narrow"></div>');
		lastSplit = bestSplits[i];
	}
	$("#menu-categories > .category").wrapAll('<div class="narrow"></div>');

	$("#container > .narrow:last, #menu-categories > .narrow:last").addClass("last");

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

	try {
		if (GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("map"));
			map.setCenter(new GLatLng(51.615139, -0.246377), 15);
			map.setUIToDefault();
			
			var hlfIcon = new GIcon(G_DEFAULT_ICON);
			hlfIcon.image = "/elements/marker.png";
			hlfIcon.iconSize = new GSize(73, 36);
			hlfIcon.iconAnchor = new GPoint(36, 36);
			hlfIcon.shadow = "/elements/lightbox-blank.gif";
			
			map.addOverlay(new GMarker(new GLatLng(51.615139, -0.246377), { icon:hlfIcon }));
		}
	} catch (e) {}

	$(window)
		.resize(function() {
			$("#container").css("left", Math.max(0, ($(window).width()-$("#container").width())/2-12));
		})
		.resize();
});
