// requires "jquery.js"


$(document).ready(function(){
	$(".faq-show>.entry").each(function(){
		var id = $(this).attr("id");
		if (id) {
			var e = $(this);
			var q = $(this).children(".question");
			var a = $(this).children(".answer");
			var t = q.children("h3").wrapInner("<a href=\"#" + id + "\" class=\"toggle\"></a>");

			e.addClass("dynamicborder");

			t.click(function(){
				e.toggleClass("visible");
				a.slideToggle(100);
				return false;
			});
			t.hover(
				function(){ e.addClass("hover"); },
				function(){ e.removeClass("hover"); }
			);

			if (!location.hash || location.hash != "#" + id) {
				a.hide();
			}
			else {
				e.addClass("visible");
			}
		}
	});
	$(".faq-show-internal_link").each(function(){
		$(this).click(function(){
			var href = $(this).attr("href");
			var i = href.indexOf("#");
			if (0 <= i && href.indexOf("#", i + 1) < 0) {
				var e = $(".faq-show>.entry" + href.substr(i));
				e.addClass("visible");
				e.children(".answer").slideDown(100);
			}
			return true;
		});
	});
	var note = $("<div></div>")
		.append($("<p></p>").text("質問を選択すると、回答が表示されます。"))
		.append($("<p></p>").text("[すべての回答を表示する] チェックボックスで、すべての質問への回答を表示することができます。"))
		.css("line-height", "1.2")
		.css("margin", "1em 0")
		.insertAfter(".faq-show>h1:first");

	var checkbox = $("<input></input>")
		.attr("id", "faq-show-all")
		.attr("type", "checkbox");
	checkbox.click(function(){
		if ($(this).attr("checked")) {
			var e = $(".faq-show>.entry");
			e.addClass("visible");
			e.children(".answer").show();
		}
		else {
			var e = $(".faq-show>.entry");
			e.removeClass("visible");
			e.children(".answer").hide();
		}
	});
	$("<div></div>")
		.html("<label for=\"faq-show-all\">すべての回答を表示する</label>")
		.prepend(checkbox)
		.css("text-align", "right")
		.appendTo(note);
});

