// JavaScript Document

$(function(){
	// 各見出しをクリックした時のイベントを設定
	$("div.box > dl.q > dd > a").bind( "click", function(){
		// コンテンツの開閉
		$(this).parents("div.box").children("dl.a").toggle();
		
		// 詳細情報が全展開時・全展開解除時の処理
		if( confirmAll() == true ){
			$("#expand").attr( "checked", "checked" );	// ボックスをチェック
		} else {	
			$("#expand").attr( "checked", "" ); 			// ボックスをチェック
		}
		
		return false;
	});

	// ページ内リンクをクリックした時のイベントを設定
	$("a.faqinside").bind( "click", function(){
		// 該当コンテンツを開く
		var qid = $(this).attr("href");
		$("div" + qid).children("dl.a").show();
		
		// 詳細情報が全展開時・全展開解除時の処理
		if( confirmAll() == true ){
			$("#expand").attr( "checked", "checked" );	// ボックスをチェック
		} else {	
			$("#expand").attr( "checked", "" );			// ボックスをチェック
		}
	});

	// チェックボックスをクリックした時のイベントを設定
	var chkBox = $("div#check input")[0];
	chkBox.onclick = function chkBox(){
		if( confirmAll() ){
			ctrlAll(false);	// 全閉じ処理関数を実行
		} else {
			ctrlAll(true);		// 全展開処理関数を実行
		}
	};
	
	// 全展開時・全展開解除時の処理
	if( confirmAll() == true ){
		$("#expand").attr( "checked", "checked" );	// ボックスをチェック
	} else {	
		$("#expand").attr( "checked", "" );			// ボックスをチェック
	}

	// アンカー付でロードした時の処理（指定のQAを開く）
	if( location.hash.substr(0, 2)  == "#q" ){
		$("#q" + location.hash.substr(2) + " dl.a").toggle();
	}	
});


// 全部開いているか確認する関数(戻り値はtrue or false)
function confirmAll(){
	// 全dl.aのループ(展開A数をカウント)
	var tmp = 0;
	$("dl.a").each( function(){
		if( $(this).css("display") != "none" ){
			tmp++;
		}
	});
	
	// 全展開されている時…
	if( $("dl.a").length == tmp ){
		return true;		// trueを返す
	} else {
		return false;		// falseを返す
	}
}


// 全展開 or 全閉じ処理関数
function ctrlAll( swt ){
	// 全dl.aのループ
	$("dl.a").each( function(){
		//var thisId = $(this).attr("id");
		if( swt ){
			$(this).show();		// 全展開処理
		} else {
			$(this).hide();		// 全閉じ処理
		}
	});
}

