/*
  TOEVAL GEZOCHT
  Copyright (C) 2011 by Systemantics, Bureau for Informatics

  Systemantics GmbH
  Am Lavenstein 3
  52064 Aachen
  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.
*/



if (typeof(stLight)!="undefined") {
	stLight.options({
		publisher:'12345'
	});
}

$(function() {
	var cookieOptions = {
			path: "/",
			expires: 0
		};

	var popupZIndex = 0;
	
	var images = $("#articles .image, #news .image");
	images.css("visibility", "hidden");

	// Create frame layout
	$(".frame")
		.wrapInner('<div class="framed-content"></div>')
		.append('<div class="framed-bottom"></div>');

	// Make non-active frames clickable
	$(".frame:not(#frame-home):not(.active)")
		.hover(
			function () {
				$(".frame-title a", this).addClass("hover");
			},
			function () {
				$("	.frame-title a", this).removeClass("hover");
			}
		)
		.click(function (e) {
			location.href = $(".frame-title a", this).attr("href");
			e.stopPropagation();
		});

	// Show frame titles in all four corners of a frame
	$(".frame:not(.active) .frame-title").each(function () {
		var title = $(this),
			parent = title.parent(),
			i;

		for (i = 2; i <= 4; i++) {
			title.clone().addClass("frame-title-copy" + i).appendTo(parent);
		}
		title.addClass("frame-title-copy1");
	});

	// Replace ‘toeval gezocht’ by word mark
	$(".text,.intro,.placeholder,.submenu").highlight("toeval gezocht");

	$("#projects .project").each(function () {
		$(this).find("h1,h2,.date").wrapAll('<div class="head-outer"><div class="head-inner"></div></div>');
	});

	$(".image-preview .title-image,.image-preview .media-preview").each(function () {
		$(this).wrapInner($('<div class="image-contents"></div>'));
		var contents = $(this).find(".image-contents");
		contents.css("margin-top", -contents.height()/2);
	});

	$("#search").closest("form").submit(function () {
		return $("#search-q").val().trim()!="";
	});
	$("#search-q").each(function() {
		$(this)
			.focus(function () {
				$("label[for="+this.id+"]").hide();
			})
			.blur(function () {
				if ($.trim($(this).val())=="") {
					$("label[for="+this.id+"]").show();
				}
			});
		if ($.trim($(this).val())!="") {
			$("label[for="+this.id+"]").hide();
		}
	});

	if ($("#popups").length) {
		$(".popup-contents").each(function () {
			var pc = $(this);
			pc.wrapInner("<div></div>");
			var pcHeight = pc.height();
			var pt = Math.ceil((pcHeight - pc.find("div").height()) / 2);
			pc
				.css("padding-top", pt)
				.height(pcHeight - pt);
		});
		$(".popup")
			.each(function (i) {
				var p = $(this);
				var pos = $.cookie(p.attr("id")+"_p");
				var x;
				switch (i%3) {
					case 0:
						x = 25+Math.random()*60;
						break;
					case 1:
						x = 25+Math.random()*600;
						break;
					case 2:
						x = 630+Math.random()*60;
						break;
				}
				pos = pos ? pos.split(",") : [x, i%3==1 ? 350 : 25+Math.random()*255];
				if (pos.length==2) {
					p.css({
						left: pos[0] + "px",
						top: pos[1] + "px"
					});
				}
			})
			.click(function (event, ui) {
				$(this).css("z-index", 2000+popupZIndex);
				popupZIndex++;
			})
			.draggable({
				handle: ".popup-contents",
				start: function (event, ui) {
					$(this).css("z-index", 2000+popupZIndex);
					popupZIndex++;
				},
				stop: function (event, ui) {
					$.cookie(ui.helper.attr("id")+"_p", ui.position.left+","+ui.position.top, cookieOptions);
				}
			})
			.append($('<div class="popup-close"></div>').click(function () {
				var p = $(this).closest(".popup");
				$.cookie(p.attr("id")+"_hidden", 1, cookieOptions);
				p.closest(".popup").remove();
			}))
			.hover(
				function () {
					$(".popup-contents", this).css("visibility", "visible");
					window.clearTimeout($(this).data("timeout"));
				},
				function () {
					var p = this;
					p.data("timeout", window.setTimeout(function () {
						$(".popup-contents", p).css("visibility", "hidden");
					}, 100));
				}
			);
	}

	$(window).load(function () {
		var h = 0;
		$(".frame").each(function () {
			var frame = $(this);
			h = Math.max(h, frame.position().top + frame.height());
		});
		$("#container").height(h);

		// Center images vertically
		images.each(function () {
			var image = $(this);
			image.closest("div").andSelf().css("position", "relative");
			image.css("overflow", "hidden");
			$("img", this).css("margin-top", Math.round((image.parent("div").height() - image.position().top + image.parent("div").position().top - image.height()) / 2) + "px");
			image.css("visibility", "");
		});
	});

	$("#report").each(function () {
		var report = $(this);
		if (report.closest("#edit-form").length) {
			return;
		}
		var shrink = false;
		report.find(".column").each(function () {
			var col = $(this);
			if (col.height()>150) {
				col.wrapInner("<div></div>");
				shrink = true;
				$('<div class="column-enlarger">meer</div>')
					.click(function () {
						if (report.hasClass("shrinked")) {
							report.removeClass("shrinked");
							$(".column-enlarger").text("minder");
						} else {
							report.addClass("shrinked");
							$(".column-enlarger").text("meer");
						}
					})
					.appendTo(col);
			}
		});
		if (shrink) {
			report.addClass("shrinked");
		}
	});

	$(".image-preview.enlargable a").click(function (e) {
		e.preventDefault();
		var el = $(this).closest(".image-preview");
		var origImage = el.find("img");
		var id = "popup-"+el.attr("id");
		if ($("#"+id).length) {
			$("#"+id).css("z-index", 2000+popupZIndex);
			popupZIndex++;
			return;
		}
		var img = new Image();
		img.onload = function () {
			var pos = $.cookie(id+"_p");
			var p = el.offset();
			pos = pos ? pos.split(",") : [p.left-(img.width-origImage.width())/2, p.top-(img.height-origImage.height())/2];
			$('<div id="'+id+'" class="popup projects with-image" style="width:'+img.width+'px;height:'+img.height+'px;'+(pos.length==2 ? 'left:'+pos[0]+'px;top:'+pos[1]+'px;' : '')+'background-image:url('+img.src+');z-index:1002;"></div>')
				.draggable({
					handle: ".popup-contents",
					start: function (event, ui) {
						$(this).css("z-index", 2000+popupZIndex);
						popupZIndex++;
					},
					stop: function (event, ui) {
						$.cookie(ui.helper.attr("id")+"_p", ui.position.left+","+ui.position.top, cookieOptions);
					}
				})
				.append($('<div class="popup-close"></div>').click(function () {
					$(this).closest(".popup").remove();
				}))
				.css("z-index", 2000+popupZIndex)
				.appendTo("body");
			popupZIndex++;
		}
		img.src = this.href
	});

	$("a.external,a.download").click(function (e) {
		this.blur();
		window.open(this.href);
		e.preventDefault();
	});
});

