/*
  Website MKK
  Copyright (C) 2007-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.
*/



$(function() {
	var f;

	if ($("#deliverysame").length>0) {
		$("#deliverysame")
			.click(function() {
				var fs = $(this).parents("fieldset");
				fs.find("input[type=text], input[type=radio], select")
					.attr("disabled", this.checked);
				if (this.checked) {
					fs.find("input[type=text]").val("");
				}
			})
			.parents("fieldset").find("input[type=text], input[type=radio], select")
				.attr("disabled", $("#deliverysame")[0].checked);
	}

	f = function() {
		$("#customamount-text").attr("disabled", !$("#customamount-radio").attr("checked"));
		if ($("#customamount-radio").attr("checked")) {
			$("#customamount-text").focus();
		}
	}
	$("input[name=amount]")
		.change(f)
		.click(f);
	$("input[name=amount]").change();

	f = function() {
		$("#donate-period").css(
			"display",
			$("#donate-mode input[value=once]").attr("checked")
				? "none"
				: "block"
			);
	};
	$("#donate-mode input[type=radio]")
		.change(f)
		.click(f);
	$("#donate-mode input[type=radio]").change();

	$.datepicker.setDefaults($.extend({
		changeMonth: true,
		changeYear: true,
		dateFormat: "dd.mm.yy",
		minDate: "0",
		showAnim: false
	}, $.datepicker.regional[$("meta[name='dc.language']").attr("content")]));
	$("#donate-period input[type=text]").datepicker();

	$.datepicker.setDefaults($.extend({
		changeMonth: true,
		changeYear: true,
		dateFormat: "dd.mm.yy",
		minDate: null,
		showAnim: false,
		yearRange: "1900:"+((new Date()).getYear()+1900)
	}, $.datepicker.regional[$("meta[name='dc.language']").attr("content")]));
	$("#dob,#partner_dob1,#partner_dob2,#partner_dob3,#partner_dob4,#partner_dob5,#partner_dob6,#partner_dob7,#partner_dob8,#partner_dob9").datepicker();

	f = function() {
		$("#beuys-invitation-details").css(
			"display",
			$("#beuys").attr("checked")
				? "block"
				: "none"
			);
	};
	$("input[name=purpose]")
		.change(f)
		.click(f);
	$("input[name=purpose]").change();

	if ($("#beuys-invitation").length>0) {
		$("#beuys-invitation")
			.click(function() {
				var fs = $("#personal-data");
				fs.find("input[type=text], input[type=radio], select")
					.attr("disabled", !this.checked);
			});
		$("#personal-data").find("input[type=text], input[type=radio], select")
			.attr("disabled", !$("#beuys-invitation")[0].checked);
	}

	if ($("#donate-receipt").length>0) {
		$("#donate-receipt")
			.click(function() {
				var fs = $("#personal-data");
				fs.find("input[type=text], input[type=radio], select")
					.attr("disabled", !this.checked);
				if (!this.checked) {
					fs.find("input[type=text]").val("");
				}
			});
		$("#personal-data").find("input[type=text], input[type=radio], select")
			.attr("disabled", !$("#donate-receipt")[0].checked);
	}

	f = function() {
		$("#donate-payment-info-account").css(
			"display",
			$("#donate-payment-method input[value=DebitDE]").attr("checked")
				|| $("#donate-payment-method input[value=DebitNL]").attr("checked")
				? "block"
				: "none"
		);
		$("#donate-payment-info-sik").css(
			"display",
			$("#donate-payment-info-account").css("display")=="block"
				? "none"
				: "block"
		);
		$("#donate-payment-rtn").css(
			"display",
			$("#donate-payment-method input[value=DebitDE]").attr("checked")
				? "block"
				: "none"
			);
	};
	$("#donate-payment-method input[type=radio]")
		.change(f)
		.click(f);
	$("#donate-payment-method input[type=radio]").change();

	$("a.external, a.external[href^=http://]").click(function() {
		this.blur();
		window.open(this.href);
		return false;
	});

	$(".row").each(function() {
		var h = 0;
		$("img", this).each(function() {
			h = Math.max(h, $(this).height());
		});
		$("img", this).each(function() {
			$(this).parent().parent().css("padding-top", h-$(this).height());
		});
	});

	$(".sponsors tr").each(function() {
		var h = 0;
		$(".image", this)
			.each(function() {
				h = Math.max(h, $(this).height());
			})
			.height(h);
	});

	$(".person-add").click(function() {
		var number = $(this).closest("form").find(".person").length+1;
		$(this).parent().before(
			$(this).closest("form")
				.find(".person:eq(0)").clone().find(":input")
					.each(function() {
						if ($(this).is(":text")) {
							$(this).val("");
						} else {
							$(this).attr("checked", false);
						}
						var name = $(this).attr("name");
						newName = name.substr(0, name.length-1)+number;
						$(this)
							.attr("name", newName)
							.attr("id", newName)
							.closest(".person").find("label[for="+name+"]").attr("for", newName);
					})
				.end()
				.find("p:first-child").html("<br />").end()
		);
		$(this).closest("form").find("input[name=persons]").val(number);
		if (number==9) {
			$(this).remove();
		}
	});

	$(document).keydown(function(e) {
		var url = false;
		switch (e.which) {
			case 37:
				url = $(".nav-prev a").attr("href");
				break;
			case 39:
				url = $(".nav-next a").attr("href");
				break;
			default:
				return;
		}
		if (url) {
			location.href = url;
			return false;
		}
	});
});

