/*
  Probe
  Copyright (C) 2010-2011 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.
*/



var flashvars = {
	autostart: "true",
	backcolor: "ffffff",
	repeat: "true"
};

var params = {
	wmode: "transparent"
};

var videoId = 0;

function getRefEl() {
	return $("#archive").length ? $("#archive") : $("#exhibition");
}

function getPageWidth() {
	var refEl = getRefEl();
	return refEl.offset().left+refEl.width();
}

function getPageHeight() {
	var refEl = getRefEl();
	return refEl.offset().top+refEl.height();
}

$(function() {
	$("body").wrapInner('<div id="container"></div>');

	$(".exhibition-images:not(:first)").addClass("spaced");
	$(".exhibition-images").each(function () {
		$(this).add(".merged-details", this).width($(".cell", this).length*215);
	});

	$(".related-row").each(function () {
		var row = $(this);
		var h = 0;
		row.find(".image").each(function () {
			h = Math.max(h, $("img", this).height());
		});
		row.find(".image > .valign-bottom").each(function () {
			$(this).css("padding-top", h-$("img", this).height());
		});
		row.find(".image > .valign-middle").each(function () {
			var p = Math.floor((h-$("img", this).height())/2);
			$(this)
				.css("padding-top", p);
		});
	});

	var poster = $("#poster");
	if (poster.length) {
		poster.after($("#menu"));
		var img = new Image();
		$(img).load(function () {
			poster
				.wrap('<div id="poster-outer"></div>')
				.data("width", img.width)
				.data("height", img.height);
			var ex = $("#exhibition");
			ex.data("height", ex.height());
			$(window).resize(function () {
				var p = $("#poster-outer").offset();
				var w = $(window).width()-22;
				var h = $(window).height()-p.top-11;
				$("#poster-outer")
					.width(w)
					.height(h);
				var factor = Math.max(
					w/poster.data("width"),
					h/poster.data("height")
				);
				poster
					.width(poster.data("width")*factor)
					.height(poster.data("height")*factor)
					.css({
						marginTop: -poster.height()/2,
						marginLeft: -poster.width()/2
					});
				ex.height(Math.max(ex.data("height"), $("#menu").position().top+$(window).height()));
			}).resize();
		});
		img.src = poster.attr("src");
		$("#exhibition-details")
			.addClass("with-poster");
		$(".exhibition-title:first").attr("id", "exhibition-title");
		$('<div id="show-scrollbar"></div>').appendTo("body");
	} else {
		$("#enter,#edition").remove();
	}
		
	$(window)
		.resize(function () {
			$("#show-scrollbar").height($(window).height()+1);
			var w = getPageWidth();
			$("#menu-content").each(function () {
				$(this).height(Math.max($(window).height, getPageHeight())-$("#menu-content").offset().top);
			});
		})
		.resize();

	$(".exhibition").hover(
		function() {
			$(this)
				.addClass("hover")
				.prevAll(".exhibition, #cameras").filter(":last").addClass("hover-pre");
			$(this)
		},
		function() {
			$(this)
				.removeClass("hover")
				.prevAll(".exhibition, #cameras").filter(":last").removeClass("hover-pre");
		}
	);

	$("a.img").colorbox({
		close: "X",
		current: "{current} /{total}",
		next: ">",
		previous: "<"
	});
	$("a.youtube").each(function() {
		url = "http://www.youtube.com/v/"+this.href.substr(31);
		$(this).colorbox({
			current: "",
			html: '<object width="640" height="385"><param name="movie" value="'+url+'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="'+url+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="640" height="385"></embed></object>'
		});
	});
	$("a.video").each(function() {
		url = this.href;
		$(this).colorbox({
			current: "",
			html: '<div id="video"></div>',
			onComplete: function() {
				flashvars.displayheight = 360;
				flashvars.height = 360;
				flashvars.file = url;
				flashvars.width = 640;
				swfobject.embedSWF("elements/mediaplayer.swf", "video", 640, 360, "8", false, flashvars, params);
			}
		});
	});

	$(".view span").click(function () {
		var slideshow = $(this).closest(".slideshow");
		if (slideshow.length==0) {
			slideshow = $(".slideshow:last");
		}
		slideshow.find("a.img:first").click();
	});
	
	$("#enter a,#poster-outer").live("click", function (e) {
		e.preventDefault();
		$("html,body").animate({scrollTop: $("#menu").offset().top}, 1000, function () {
			$("#edition,#enter,#exhibition-title,#poster-outer,#show-scrollbar").remove();
			$("html,body").scrollTop(0);
			$("#exhibition").css("height", "");
		});
	});

	$(".cell a img").hover(
		function() {
			this.src = this.src.replace("/s_", "/t_");
		},
		function() {
			this.src = this.src.replace("/t_", "/s_");
		}
	);

	$("#cameras").width($("#cameras > .cell").length*$("#cameras > .cell").outerWidth(true));

	$(".related").each(function () {
		var maxW = 0;
		var currentW = 0;
		$("> div", this).each(function () {
			if ($(this).is(".clear")) {
				maxW = Math.max(maxW, currentW);
				console.log(this);
				currentW = 0;
			}
			currentW += $(this).outerWidth(true);
		});
		$(this).width(maxW);
	});

	var w = 0;
	$(".exhibition, .texts, #cameras")
		.each(function() {
			w = Math.max(w, $(this).width());
		})
		.width(w);

	var open = false;
	function showMenu(item) {
		open = true;
		var content = $('<div id="menu-content" class="'+(item && item.hasClass("narrow") ? "narrow" : "wide")+'"></div>')
			.bind("close", function () {
				if (!open) {
					return;
				}
				open = false;
				if (item) {
					item.data("open", false);
				}
				$("#menu-content,#container")
					.animate({left: "-="+$("#menu-content").outerWidth(true)+"px"}, $("#menu-content").outerWidth(true)*3, function () {
						if ($(this).is("#menu-content")) {
							$(this).remove();
						}
					});
			})
			.click(function (e) {
				if (!$(e.target).is("a,input")) {
					$("#menu-content").trigger("close");
				}
			})
			.appendTo("body")
			.css("padding-top", 4+$("#menu").position().top);
		function equipContent() {
			$("#title").prepend($("<span>X</span>").click(function () {
				$("#menu-content").trigger("close");
			}));
			$("#menu-content input[type='text']").eq(0).select().focus();
			$("#menu-content,#container").animate({left: "+="+$("#menu-content").outerWidth(true)+"px"}, $("#menu-content").outerWidth(true)*3);
		}
		if (item) {
			content.load(item.attr("href")+" #content-inline", function () {
				equipContent();
			});
		} else {
			content.append($("#content-inline > *"));
			equipContent();
		}
	}	
	$("a.inline").click(function (e) {
		e.preventDefault();
		var item = $(this);
		if (item.data("open")) {
			return;
		}
		item.data("open", true);
		var content = $("#menu-content");
		if (content.length) {
			content
				.trigger("close")
				.add("#container").queue(function (next) {
					showMenu(item);
					next();
				});
		} else {
			showMenu(item);
		}
	});
	
	$("#content-inline").each(function () {
		showMenu();
	});
});

