/*
  Studio Piet Paris
  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.
*/



var leftScrollableWidth = 0;
var max = 0;

function resize() {
	var pages = $("#left > div");
	var w = $("#left").width();
	for (var i=0; i<pages.length; i++) {
		var index = parseInt(pages[i].className.substr(1));
		$(pages[i]).css({left: index*w, width: w});
	}
	leftScrollableWidth = (pages.length)*w;
	$("#container")
		.css("height", $(window).height()-20-60)
		.css("padding-right", $("#right").width()-155-270);
	max = Math.max(
		Math.min(
			$(window).height()-20-60,
			$(window).width()/2-40
		),
		460
	);
	$("#left img, #left object, #left embed").each(function() {
		var img = $(this);
		var ratio = img.attr("width")/img.attr("height");
		img.css({
			width: ratio>1 ? max : max*ratio,
			height: ratio>1 ? max/ratio : max
		});
		img.closest("div").css("paddingTop", (max-parseInt(img.css("height")))/2);
	});
}

var currentPath = [];
var transitions = [
	[	// Level 0
	 	function(newId) {
			// NOOP
	 	},
	 	function(newId) {
	 		$("#news").hide();
			$("#c-"+newId).show(150);
	 	},
	 	function(newId) {
	 		$("#news").hide();
			var path = newId.split("-");
			$("#c-"+path[0]).show(150);
			$(".level0 h3, .level1 h3").animate({fontSize: "12px", lineHeight: "16px"}, 150, function() {$(this).parent().addClass("small");});
			$("#c-"+newId).show(150);
	 	},
	 	function(newId) {
	 		$("#news").hide();
			var path = newId.split("-");
			$("#c-"+path[0]).show(150);
			$(".level0 h3, .level1 h3").animate({fontSize: "12px", lineHeight: "16px"}, 150, function() {$(this).parent().addClass("small");});
			$("#c-"+newId).show(150, showed);
	 	}
	],
	[	// Level 1
	 	function(newId) {
			$("#news").show();
			$(".level1").hide(150);
	 	},
	 	function(newId) {
	 		$(".level1").hide(150);
	 		$("#c-"+newId).show(150);
	 	},
	 	function(newId) {
			var path = newId.split("-");
			$("#c-"+path[0]).show(150);
			$(".level0 h3, .level1 h3").animate({fontSize: "12px", lineHeight: "16px"}, 150, function() {$(this).parent().addClass("small");});
			$("#c-"+newId).show(150);
	 	},
	 	function(newId) {
	 		$("#c-"+newId).show(150, showed);
			$(".level0 h3, .level1 h3").animate({fontSize: "12px", lineHeight: "16px"}, 150, function() {$(this).parent().addClass("small");});
	 	}
	],
	[	// Level 2
	 	function(newId) {
			$("#news").show();
			$(".level1, .level2").hide(150);
			$(".level0, .level1, #news").removeClass("small")
			$(".level0 h3, .level1 h3")
				.animate({fontSize: "40px", lineHeight: "40px"}, 150);
	 	},
	 	function(newId) {
			$(".level1, .level2").hide(150);
			$("#c-"+newId).show(150);
			$(".level0, .level1, #news").removeClass("small")
			$(".level0 h3, .level1 h3")
				.animate({fontSize: "40px", lineHeight: "40px"}, 150);
	 	},
	 	function(newId) {
	 		$(".level2").hide(150);
	 		$("#c-"+newId).show(150);
	 	},
	 	function(newId) {
	 		$(".level2").hide(150);
	 		$("#c-"+newId).show(150, showed);
	 	}
	],
	[	// Level 3
	 	function(newId) {
			$("#news").show();
			$(".level1, .level3").hide(150);
			$(".level0, .level1, #news").removeClass("small")
			$(".level0 h3, .level1 h3")
				.animate({fontSize: "40px", lineHeight: "40px"}, 150);
	 	},
	 	function(newId) {
			$(".level3").hide(150);
			$(".level1, .level2").hide(150);
			$("#c-"+newId).show(150);
			$(".level0, .level1, #news").removeClass("small")
			$(".level0 h3, .level1 h3")
				.animate({fontSize: "40px", lineHeight: "40px"}, 150);
	 	},
	 	function(newId) {
	 		$(".level3").hide(150);
	 		$("#c-"+newId).show(150);
	 	},
	 	function(newId) {
	 		$(".level3").hide(150);
	 		$("#c-"+newId).show(150, showed);
	 	}
	]
];

function showed(a) {
	if (scrollJustStopped) {
		$("#container").scrollTop($("#container").scrollTop()+$("h3", this).position().top);
		scrollJustStopped = false;
	}
}

function show(anchor) {
	var newPath = anchor=="" ? [] : anchor.split("/");
	var currentLevel = currentPath.length;
	var newLevel = newPath.length;
	if (newPath!=currentPath) {
		_gaq.push(['_trackPageview', anchor]);
		$("a.visited").removeClass("visited");
		var p = "";
		for (var i=0; i<newPath.length; i++) {
			p += (p=="" ? "" : "/")+newPath[i];
			$("a[href=#"+p+"]").addClass("visited");
		}
		var id = newPath.join("-");
		$("#content").height("").height($("#content").height());
		transitions[currentLevel][newLevel](id);
		window.setTimeout(function() {
			$("#content").height("");
		}, 200);
		currentPath = newPath;
		location.hash = anchor;
		var img = $("#img-"+id);
		if (img.length==0) {
			img = $("#img-"+id.split("_").slice(0, -1).join("_"));
		}
		if (img.length>0) {
			stopScrollDetection();
			$("#left").animate(
				{scrollLeft: img.css("left")},
				200,
				function() {
					startScrollDetection();
				}
			);
		}
	}
}

var old = 0;
var scrolling = false;
var scrollJustStopped = false;

function scrollStop(x) {
	var w = $(window).width();
	$("#left div").each(function() {
		var left = parseInt($(this).css("left"));
		if (x>left-w && x<=left+$(this).width()/2) {
			location.hash = "#"+this.id.substr(4).replace(/-/g, "/");
			scrollJustStopped = true;
			return false;
		}
	});
}

var scrollDetector;
var scrollDetectionActive = false;

function startScrollDetection() {
	old = $("#left").scrollLeft();
	scrollDetector = window.setTimeout(scrollInterval, 200);
	scrollDetectionActive = true;
}

function stopScrollDetection() {
	scrollDetectionActive = false;
	window.clearTimeout(scrollDetector);
}

function scrollInterval() {
	var x = $("#left").scrollLeft();
	if (x==old && scrolling) {
		scrolling = false;
		scrollStop(x);
	} else if (x!=old && !scrolling) {
		scrolling = true;
	}
	old = x;
	if (scrollDetectionActive) {
		scrollDetector = window.setTimeout(scrollInterval, 200);
	}
}

$(function() {
	$(".video").each(function() {
		var s1 = new SWFObject('/elements/player.swf',this.id+"-player",'1280','720','9');
		s1.addParam('allowfullscreen','false');
		s1.addParam('allowscriptaccess','always');
		s1.addVariable('file', $(this).data("src"));
		s1.addVariable('image', $(this).data("img"));
		s1.addVariable('controlbar', 'none');
		s1.write(this.id);
	});
	$("#preload").remove();
	$(window).resize(resize);
	resize();
	$("#left > div").css("visibility", "visible");
	$("#content div:first").show();
	$("a[href^=http://]").click(function() {
		window.open(this.href);
		return false;
	});
	$("a").click(function() {
		this.blur();
	});
	var oldHash = location.hash;
	scrollJustStopped = true;
	show(location.hash.substr(1));
	window.setInterval(
		function() {
			if (location.hash!=oldHash) {
				oldHash = location.hash;
				show(location.hash.substr(1));
			}
		},
		200
	);
	startScrollDetection();
});

$(window).load(function() {
	$("#left > div object").each(function() {
		if ($.isFunction(this.Play)) {
			$(this).closest("div").data("swf", this);
		}
	});
	$(".video-start").click(function() {
		var movie = $("#img-"+$(this).closest("div").attr("id").substr(2)+"-swf-player")[0];
		movie.sendEvent("PLAY");
	});
	$(".video-stop").click(function() {
		var movie = $("#img-"+$(this).closest("div").attr("id").substr(2)+"-swf-player")[0];
		movie.sendEvent("STOP");
	});
});

