// JavaScript Document

$(function(){
	// div#check内、一番目([0])のinputタグをchkBoxに割り当て
	var chkBox = $("div#check input")[0];
	
	// チェックボックスをクリックした時のイベントを設定
	chkBox.onclick = function chkBox(){
		// 詳細情報が全展開時（関数 confirmAllを呼び出し）
		if( confirmAll() ){
			ctrlAll(false);    // 全閉じ処理関数を実行
		} else {
			ctrlAll(true);     // 全展開処理関数を実行
		}
	};
	
	// 詳細情報が全展開時・全展開解除時の処理
	if( confirmAll() == true ){
		$("#show").attr( "checked", "checked" );     // ボックスをチェック
	} else {	
		$("#show").attr( "checked", "" );     // ボックスをチェック
	}
	
	// 各見出しをクリックした時のイベントを設定
	$("div.box > h3 > a").bind( "click", function(){
		// コンテンツの開閉
		$(this).parents("div.box").children("div.c").toggle();
		
		// ＋－アイコンの反転
		if( $(this).parents("div.box").attr("class") == "box close" ){
			$(this).parents("div.box").removeClass("close");
		} else {
			$(this).parents("div.box").addClass("close");
		}
		
		// 詳細情報が全展開時・全展開解除時の処理
		if( confirmAll() == true ){
			$("#show").attr( "checked", "checked" );     // ボックスをチェック
		} else {	
			$("#show").attr( "checked", "" );     // ボックスをチェック
		}
		
		return false;
	});
});

// 詳細情報が全部開いているか確認する関数(戻り値はtrue or false)
function confirmAll(){
	// カウント用変数の使用宣言
	var tmp = 0;
	
	// 全div.cのループ(nodeが各div.cオブジェクト)
	$("div.box").each( function(){
		if( $(this).attr("class") != "box close" ){
			tmp++;     // nodeが表示されている時、変数tmpに1追加
		}
	});
	
	// div.cの数とtmpの数が同じ時(全展開されている時)
	if( $("div.box").length == tmp ){
		return true;     // trueを呼び出し元に返す
	} else {
		return false;    // falseを呼び出し元に返す
	}
}

// 詳細情報を全展開 or 全閉じ処理関数
function ctrlAll( swt ){
	// 全div.cのループ
	$("div.box").each( function(){
		var thisId = $(this).attr("id");
		var imgIcon = document.getElementById(thisId + "_img");
		
		// 全展開指定時
		if( swt ){
			$("div.box").removeClass("close");		// マイナスアイコンを表示
			$("#" + thisId).children("div.c").show();     // div.cを表示
			
		// 全閉じ指定時
		} else {
			$("div.box").addClass("close");			// プラスアイコンを表示
			$("#" + thisId).children("div.c").hide();     // div.cを非表示
		}
	});
}


