/**
 * 親チェックボックスがtrueのとき、全ての子チェックボックスはtrueに、
 * １つでも子チェックボックスがfalseのとき、親チェックボックスはfalseに。
 *
 * @author Tsue Shogo
 * @since 2010/06/16
 */
$(function(){

	// チェックボックスにイベントの登録
	$(".area_checkbox").change(function(){

		var id_str = $(this).attr("id");

		if ($(this).attr("checked")) {

			// 自身のチェックがtrueになったら子全てをtrueにする
			$(".area_checkbox").each(function(){
				var each_id_str = $(this).attr("id");
				re = new RegExp(id_str, "i");
				if (each_id_str.search(re) != -1 && id_str != each_id_str) {
					$(this).attr("checked", true);
					$(this).change();
				}
			});

		} else {

			// 自身のチェックがfalseになったら親をfalseにする
			var parent_id = get_parents(id_str);
			if (parent_id) {
				$("#" + parent_id).attr("checked", false);
				//$("#" + parent_id).change();
			}

			// さらに、子全てをオフにする（ただし、この時子にはchangeイベントは発生させない）
			if (1) {
				$(".area_checkbox").each(function(){
					var each_id_str = $(this).attr("id");
					re = new RegExp(id_str, "i");
					if (each_id_str.search(re) != -1 && id_str != each_id_str) {
						$(this).attr("checked", false);
					}
				});
			}

		}
	});
})
function get_parents (id_str) {

	// id名から親を取得 (id名 = "area-**-**-**" )
	if (id_str.search(/area-([0-9]+)-([0-9]+)-([0-9]+)/i) == 0) {
		return "area-" + RegExp.$1 + "-" + RegExp.$2;
	} else if (id_str.search(/area-([0-9]+)-([0-9]+)/i) == 0) {
		return "area-" + RegExp.$1;
	} else if (id_str.search(/area-([0-9]+)/i) == 0) {
		return "";
	} else {
		return "";
	}

}
