/*
  De Nieuwe Toneelbibliotheek
  Copyright (C) 2009 by Systemantics, Bureau for Informatics

  Lutz Issler
  Mauerstr. 10-12
  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 resize = function() {
	var covers = $("#covers div:not(:last)");
	var loginform = $("#loginform");
	covers.css("marginRight", "")
		.css(
			"marginRight",
			Math.floor(
					($(window).width()-70-52*(covers.length+1)-(loginform.length==0 || loginform.hasClass("hidden") ? 0 : $("#userinfo").outerWidth()))/covers.length
			)
		);
	$("#covers").css("visibility", "visible");
	$("table").width($(window).width());
};

$(window).resize(resize);

var toggle = function() {
	$(this).parent().next(".details").toggle();
	if ($(this).parent().next(".details:visible").length>0) {
		$(this).addClass("open");
		var t = $(this).hasClass("toggle") ? $(this) : $(this).find(".toggle"); 
		if (t.text()=="meer info") {
			t.text("minder info");
		}
	} else {
		$(this).removeClass("open");
		var t = $(this).hasClass("toggle") ? $(this) : $(this).find(".toggle"); 
		if (t.text()=="minder info") {
			t.text("meer info");
		}
	}
	pushAuthor();
	resize();
};

var pushAuthor = function() {
	if ($("#infotext:visible").length>0 || $("#loginform:visible").length>0) {
		$("#author").addClass("pushed");
	} else {
		$("#author").removeClass("pushed");
	}
}

$(function() {
	$("span.toggle").click(toggle);
	$("h1.toggler")
		.append(' <span class="toggle">meer info</span>')
		.wrapInner("<span></span>")
		.find("span")
		.click(toggle);

	$("tr.overview td.status a").click(function() {
		location.href = this.href;
		return false;
	});

	$("tr.overview")
		.hover(
			function() {
				$(this).removeClass("white");
			},
			function() {
				if ($(this).next(".details").css("display")=="none") {
					$(this).addClass("white");
				}
			}
		)
		.click(function() {
			var next = $(this).next(".details");
			if (next.hasClass("hidden")) {
				$(this).parents("table").find("tr.details").addClass("hidden");
				$(this).parents("table").find("tr.overview").addClass("white");
				$(this).removeClass("white");
				next.removeClass("hidden");
			} else {
				next.next(".inside").andSelf().addClass("hidden");
				next.css("border-bottom-width", "");
			}
			$("html,body").animate({scrollTop: $(this).position().top-($(window).height()-$(this).height()-next.height())/2}, 1000);
			window.resize();
		});

	$("tr.inside td img").click(function() {
		var next = $(this).next("img");
		if (next.length==0) {
			next = $(this).parent().find("img:eq(0)");
		}
		$(this).css("display", "none");
		next.css("display", "inline");
	});
	$("tr.inside td img:first-child").css("display", "inline");
	
	$("table img.cover").click(function() {
		var next = $(this).parents("tr").next(".inside");
		if (next.hasClass("hidden")) {
			next.removeClass("hidden");
			$(this).parents("tr").children("td").css("border-bottom-width", "0");
			$("html,body").animate({scrollTop: next.position().top-($(window).height()-next.height())/2}, 1000);
		} else {
			next.addClass("hidden");
			$(this).parents("tr").children("td").css("border-bottom-width", "");
			var tr = $(this).closest("tr").prev(".overview");
			$("html,body").animate({scrollTop: tr.position().top-($(window).height()-tr.height()-tr.next().height())/2}, 1000);
		}
		window.resize();
	});

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

	$(".submit-button").click(function() {
		$(this).parents("form").submit();
	});

	$("#otherdelivery-yes").click(function() {
		$('#orderform form input[type="text"], #orderform form select')
			.attr("disabled", "")
			.eq(0).focus();
	});
	$("#otherdelivery-no").click(function() {
		$('#orderform form input[type="text"], #orderform form select').attr("disabled", "disabled");
	});
	if ($("#otherdelivery-no").attr("checked")) {
		$("#otherdelivery-no").click();
	}

	// Intercept any clicks to covers in the top row if they are pointing
	// to the current page
	$("#covers a").click(function() {
		var book = $(this.href.substr(this.href.indexOf("#")));
		if (book.length==0) return true;
		if (book.next().hasClass("hidden")) {
			book.removeClass("white");
		} else {
			book.addClass("white");
		}
		book.click();
		this.blur();
		return false;
	});

	// Open a book specified in the URL
	var book = $(location.hash);
	if (book.length==1) {
		book.removeClass("white");
		book.click();
	}

	// Show the login form
	$("#showlogin").click(function() {
		if ($("#loginform form:first").hasClass("hidden")) {
			$("#loginform form").addClass("hidden");
			$("#loginform form:first").removeClass("hidden");
			return;
		}
		$("#loginform")
			.toggleClass("hidden")
			.find(":input:first").focus();
		pushAuthor();
		resize();
	});

	// Format the author's page
	$(".folded h3").each(function() {
		var body = $('<div class="details"></div>');
		var next = $(this).next(":not(h3)");
		while (next.length>0) {
			var newNext = next.next(":not(h3)");
			body.append(next);
			next = newNext;
		}
		$(this)
			.after(body)
			//.append(' &nbsp;<span class="toggle">meer info</span>')
			.wrapInner('<span></span>')
			.find("span")
			.click(toggle);
	});

	// Make dropdowns functional
	$(".dropdown").click(function() {
		var s = $(this);
		s.toggleClass("open");
		if (s.hasClass("open")) {
			$('<div class="overlay"></div>')
				.width($(document).width())
				.height($(document).height())
				.click(function() {
					s.removeClass("open");
					$(this).remove();
				})
				.appendTo("body");
		} else {
			$(".overlay").remove();
		}
	});
	$(".dropdown li").click(function() {
		location.href = $("a", this).attr("href");
	});

	// Make login/retrieve password switch functional
	$("#loginform .toggle-button").click(function() {
		$("#loginform form").toggleClass("hidden");
	});

	// Make editors functional
	$(".editor .toolbar li span").click(function() {
		var $textarea = $(this).parents(".editor").find("textarea");
		var y = $textarea.scrollTop();
		var textarea = $textarea[0];
		var s = textarea.selectionStart;
		var e = textarea.selectionEnd;
		var selection = textarea.value.substring(s, e);
		var name = $(this).attr("class").substr(14);
		switch (name) {
			case "bold":
				selection = "'''"+selection+"'''";
				break;
			case "italic":
				selection = "''"+selection+"''";
				break;
			case "bullet":
				if (selection.length==0) {
					selection = "* opsomming item 1\n* opsomming item 2\n* opsomming item 3\n"
				} else {
					selection = selection.replace(/^/gm, "* ").replace(/^\*\s+$/gm, "");
				}
				break;
			case "link":
				var url = selection;
				if (selection.substring(0, 7).toLowerCase()!="http://") {
					url = "http://"+url;
				}
				selection = "["+encodeURI(url)+" "+selection+"]";
				break;
			case "email":
				selection = "[mailto:"+selection+" "+selection+"]";
				break;
			case "h1":
				selection = "== "+selection+" ==\n";
				break;
			default:
				return;
		}
		textarea.value = 
			(textarea.value).substring(0, s)
			+ selection
			+ textarea.value.substr(e);
		$textarea.scrollTop(y);
	});

	// Open the editor specified in the URL
	var editor = $('*[name="'+location.hash.substr(1)+'"]');
	if (editor.length==1) {
		editor.parents(".details").prev("h3").find("span").click();
	}

	// Make remove image button functional
	$("#author-removeimage")
		.click(function() {
			location.href = this.href;
		})
		.confirm({
			msg: 'Bent u zeker? ',
			buttons: {
				ok: 'ja',
				cancel: 'nee',
				wrapper: '<span class="button"></span>'
			}
		});

	$(window).resize();
});

