/*
  Wettbewerb Kunststudentinnen und Kunststudenten stellen aus 2011
  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 () {
	if ($("#container").length) {
		$(window).resize(function () {
			var deltaX = $("#date").position().left;
			$("#container").width(Math.max(1000+deltaX, $(window).width()+deltaX));
			$("#intro").css("top", Math.max(30, ($(window).height()-$("#intro").height())/2));
		}).resize();
	
		var scrollLeft = $.cookie("l");
		scrollLeft = scrollLeft==null ? $("#date").position().left : parseInt(scrollLeft);
		$(document)
			.scroll(function () {
				$.cookie("l", $(document).scrollLeft(), {path: "/", expires: 0});
			})
			.scrollLeft(scrollLeft);
	
		$("#text-archive td").click(function () {
			window.open($(this).closest("tr").find("a").attr("href"));
			return false;
		});

		$(".here a").each(function () {
			var homeUrl = this.href;
			$("#container").click(function () {
				location.href = homeUrl;
			})
		});
		
		$("#container").click(function () {
			$("#academies").removeClass("open");
		});
	}

	$(".menu h3").click(function () {
		$(this).closest(".menu").toggleClass("open");
	});

	if ($("#gallery").length) {
		var imgCount = $("#gallery .images img").length;
		$("#gallery .images img").each(function () {
			var image = $(this),
				img = new Image();

			image.css("visibility", "hidden")
			$(img).load(function () {
				image
					.data("loaded", true)
					.data("width", img.width)
					.data("height", img.height);
				imgCount--;
			});
			img.src = this.src;
		});
		
		var resizing = false;
		$(window).resize(function () {
			if (resizing) {
				return;
			}
			resizing = true;
			scrolling = true;
			$("#overlay").remove();
			var bbSize = Math.min(($(window).width() - 6 * 30) / 3, $(window).height() - 200);
			$("#gallery .artist").width((bbSize + 30) * 3).height($(window).height()-20);
			$("#gallery .images img").each(function () {
				var image = $(this);
				if (image.data("loaded")) {
					var ratio = image.data("width")/image.data("height"),
						h = 0;
					if (image.data("width")>image.data("height")) {
						image
							.css("width", bbSize)
							.css("height", "");
						h = bbSize/ratio;
					} else {
						image
							.css("width", "")
							.css("height", bbSize);
						h = bbSize;
					}
					image.closest("div").css("padding-top", bbSize-h);
					image.css("visibility", "");
				}
				image.closest("div").width(bbSize);
			});
			$("#gallery").css("width", ($(window).width() - 30) * $(".artist").length - 30);
			scrolling = false;
			resizing = false;
			$(window).scroll();
		});
		$(window).load(function() {
			$(window).resize();
		});
		
		$("#gallery .name").each(function () {
			$(this).css("bottom", (11-$(this).height())+"px");
		});
		$("#gallery .name span").click(function () {
			var name= $(this).closest(".name");
			var artist = $(this).closest(".artist");
			var infos = $(this).closest(".infos");
			var bio = artist.find(".bio");
			$('<div id="overlay"></div>')
				.css({
					height: $(window).height()-11,
					width: $("#gallery").width()
				})
				.append(bio.clone().css({
					left: bio.position().left+artist.position().left+parseInt(artist.css("margin-left")),
					bottom: infos.height()+parseInt(infos.css("bottom"))+9
				}))
				.append(name.clone().css({
					left: name.position().left+artist.position().left+parseInt(artist.css("margin-left")),
					top: name.position().top+infos.position().top
				}))
				.click(function () {
					$("#overlay").remove();
				})
				.appendTo("body");
		});
		var oldHash = "";
		var scrolling = false;
		var scrollTimeout = false;
		window.setInterval(function () {
			if (imgCount) {
				return;
			}
			if (location.hash!=oldHash) {
				window.clearTimeout(scrollTimeout);
				scrolling = true;
				var duration = 500;
				oldHash = location.hash;
				var nextArtist = $("#"+location.hash.substr(2));
				$(".infos", nextArtist.siblings()).fadeOut(duration);
				$(".infos", nextArtist).fadeIn(duration);
				$("html, body")
					.stop()
					.animate({scrollLeft: nextArtist.offset().left-30}, duration, function () {
						scrolling = false;
					});
			}
		}, 500);
		$(window).scroll(function () {
			if (scrolling) {
				return;
			}
			window.clearTimeout(scrollTimeout);
			scrollTimeout = window.setTimeout(function () {
				$(".artist").each(function () {
					if ($(window).scrollLeft() < $(this).position().left + $(this).width() / 2) {
						oldHash = "";
						location.hash = "#/"+$(this).attr("id");
						return false;
					}
				});
			}, 1000);
		});
		$("#academies,#participants").click(function () {
			if ($(this).hasClass("open") && !$("#overlay").length) {
				$('<div id="overlay"></div>')
					.css({
						height: $(window).height(),
						width: $("#gallery").width()
					})
					.click(function () {
						$("#overlay").remove();
						$("#academies,#participants").removeClass("open");
					})
					.appendTo("body");
			}
		});
	}

	$("#academies h3").click(function () {
		if ($(this).closest(".menu").hasClass("open")) {
			$("#participants").addClass("open");
			$("#menu").removeClass("open");
			$(".text,#overlay-text").remove();
			if ($("#container").length) {
				return false;
			}
		} else {
			$(".here").removeClass("here");
			$("#participants").removeClass("open");
			$("#overlay").remove();
		}
	});
	
	$("#participants h3").click(function () {
		if ($(this).closest(".menu").hasClass("open")) {
		} else {
			$(".here").removeClass("here");
			$("#academies").removeClass("open");
			$("#overlay").remove();
		}
	});

	$("#overlay-text").height($(window).height());

	$("#academies li")
		.hover(
			function () {
				$("."+$(this).attr("id")).addClass("hover");
			},
			function () {
				$("."+$(this).attr("id")).removeClass("hover");
			}
		)
		.click(function () {
			$(this).siblings(".here").each(function () {
				$("."+$(this).attr("id")).removeClass("here");
				$(this).removeClass("here");
			});
			$(this).toggleClass("here");
			$("."+$(this).attr("id")).toggleClass("here");
			return false;
		});

	$("#participants li")
		.each(function () {
			$(this).data("academy", $(this).attr("class"));
		})
		.hover(
			function () {
				$("#"+$(this).data("academy")).addClass("hover");
			},
			function () {
				$("#"+$(this).data("academy")).removeClass("hover");
			}
		);

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

