$(document).ready(function() {
	$("div.faq:first h4").each(function() {
		$(this).addClass("closed");
		$(this).nextUntil("h4").hide();
	});
	$("div.faq:first h4").each(function() {
		$(this).click(function() {
			var $this		= $(this);
			var isClosed	= $this.hasClass("closed");

			if ( $("div.faq:first h4:not(.closed)").length ) {
				$("div.faq:first h4:not(.closed)").addClass("closed");
				var nrOpened = $("div.faq:first h4:first").nextAll("p, ul").length;
				$("div.faq:first h4:first").nextAll("p, ul").slideUp(400, function() {
					if (--nrOpened == 0 && isClosed) {
						$this.nextUntil("h4").slideDown();
						$this.removeClass("closed");
					}
				});
			}
			else {
				$this.nextUntil("h4").slideDown();
				$this.removeClass("closed");
			}
		});
	});

	if ( $("#portfolio").length && $("#portfolio li").length ) {
		$("#portfolio li a.more-info").click(function() {
			var $part1 = $(this).parent();
			var $part2 = $part1.nextAll('.part2:first');
			$(this).fadeOut(300, function() {
				$part2.slideDown(400);
			});
			return false;
		});

		$("#portfolio li a.less-info").click(function() {
			var $part2 = $(this).parent();
			var $part1 = $part2.prevAll('.part1:first');
			$part2.slideUp(400, function() {
				$part1.find("a.more-info:first").fadeIn(300);
			});
			return false;
		});
	}

	$("dl.wedding dt").click(function() {
		var $this = $(this);
		// if the clicked item is opened, close it
		if ( $this.nextAll("dd:first").is(":visible") ) {
			$this.nextAll("dd:first").slideUp(400);
		}
		// if the clicked item is not opened, close every other opened item and open the clicked one
		else {
			$this.parent().children("dd:visible").slideUp(400, function() {	});
			$this.nextAll("dd:first").slideDown(400);
		}
	});
});
