window.addEventListener("message", receiveRun, false);
function receiveRun(e) {
	switch(e.origin){
		case "https://www.bengoshi-kokoro.com":
		case "https://www.iframe.law-kokoro.com":
		case "https://www.kokoro-news.com":
		console.log(e.data);
			const topUrl = $(".foot_ttl a").attr("href");
			const mobile = $(window).width() < 600 ;
			const topSpace = (mobile) ? $(window).width() * 0.04 : 15;
			const navHeight = $(".nav").height();
			if (Array.isArray(e.data)) {//iframe高さ値の処理(複数)
				for (const [key, value] of Object.entries(e.data)) {
					const idName = `${key}_if`;
					const ifHight = `${value}`;
					document.getElementById(idName).style.height = ifHight + "px";
				}
			}else if (e.data.charAt(0) === "s") {//アンカーリンクスクロール値の処理
				if(e.data.indexOf("$") !== -1){
						var ifScroll = Number(e.data.slice(2));
					}else{
						var ifScroll = Number(e.data.slice(1));
				}
				const ifHeight = $("#main_if").offset().top ;
				var scrollposition = ifHeight + ifScroll - navHeight - topSpace;
				if(mobile ){
					if(e.data.indexOf("$") !== -1){
							setTimeout(function() {
								window.scroll(0, scrollposition);
							}, 100);
						}else{
							$("html, body").animate({scrollTop:scrollposition}, 500, "linear");
					}
				}else{
					$("html, body").animate({scrollTop:scrollposition}, 500, "linear");
				}
			}else if (e.data.charAt(0) === "h") {//iframe高さ値の処理
				document.getElementById("main_if").style.height = e.data.slice(1) + "px";
			}else if (e.data.charAt(0) === "r") {//iframe高さ値取得の再処理(アコーディオン開閉時)
				if(e.data.indexOf("btmclose") !== -1 ){
					const resendHeight = e.data.split("=");
					const rHeight = resendHeight[1].split("&");
					const ifScroll = Number(rHeight[1]);
					const ifHeight = $("#main_if").offset().top ;
					const scrollposition = ifHeight + ifScroll - navHeight - topSpace;
					$("html, body").animate({scrollTop:scrollposition}, 500, "linear");
					document.getElementById("main_if").style.height = rHeight[0] + "px";
				}else{
					document.getElementById("main_if").style.height = e.data.slice(1) + "px";
				}
			}else if (e.data.charAt(0) === "c") {//iframe高さ値取得の再処理と親ページのurl再送信(カテゴリ選択時)
				window.scroll(0, 0);
				document.getElementById("main_if").style.height = e.data.slice(1) + "px";//iframe高さ値取得の再処理
				sendParenturl();//mediainfo、massmediaページの場合url再送信(カテゴリ選択時)
			}else if (e.data.charAt(0) === "a") {
				if(location.href.indexOf("/access/")!== -1){//アクセスページの時、別インラインフレーム内スクロール
					const id = e.data.slice(2);
					const win = document.getElementById("main_if").contentWindow;
					win.postMessage(id,"*");
				}else{//アクセスページへ移動し、スクロール
					window.location.href = topUrl + "access/?" + e.data.slice(2);
				}
			}else if (e.data.charAt(0) === "b") {//iframe高さ値取得の再処理(複数)
				if(e.data.indexOf("topscr") !== -1 ){//スクロールあり
					const resendHeight = e.data.split("=");
					const idName = resendHeight[1];
					const ifHeight = resendHeight[2];
					if(mobile){
						const ifoffset = $(".main_col").offset().top ;
						if($(".if_submenu").hasClass("show")){
							const submenuheight = $(".if_submenu").outerHeight(true);
							var heightposition = ifoffset - submenuheight;
							var maincolTopSpace = $(window).width() * 0.03;
							console.log("maincolTopSpace"+maincolTopSpace);
						}else{
							var heightposition = ifoffset;
							var maincolTopSpace = $(window).width() * 0.035;
							console.log("maincolTopSpace"+maincolTopSpace);
						}
						const scrollposition = heightposition - navHeight - maincolTopSpace;
						$("html, body").scrollTop(scrollposition);
					}else{
						window.scroll(0, 0);
					}
					document.getElementById(idName + "_if").style.height = ifHeight + "px";
				}else{
					const resendHeight = e.data.slice(1).split("=");
					const idName = resendHeight[0];
					const ifHeight = resendHeight[1];
					document.getElementById(idName + "_if").style.height = ifHeight + "px";
				}
			}else if (e.data.charAt(0) === "d") {//エリアマップアンカーリンクスクロール値の処理
				const idHeight = e.data.slice(1).split("&");
				const idName = idHeight[0];
				const ifScroll = Number(idHeight[1]);
				const ifHeight = $(idName + "_if").offset().top;
				const scrollposition = ifHeight + ifScroll - navHeight - topSpace;
				$("html, body").animate({scrollTop:scrollposition}, 500, "linear");
			}else if (e.data.charAt(0) === "e") {//インラインフレーム内からインラインフレーム外アンカーリンクへのスクロール処理
				const id = e.data.slice(1);
				const ifHeight = $(id).offset().top;
				const scrollposition = ifHeight - navHeight - topSpace;
				$("html, body").animate({scrollTop:scrollposition}, 500, "linear");
			}else if (e.data.charAt(0) === "f") {//トップページへ移動し、スクロール(事務所のへの行き方ボタン)
				window.location.href = topUrl + e.data.slice(1);
			}else if (e.data.charAt(0) === "g") {//インラインフレームのスクロールid送信(複数)【要検討】
				const sendId = e.data.slice(1).split("&");
				const id = sendId[0];
				const idName = sendId[1];
				const win = document.getElementById(idName + "_if").contentWindow;
				win.postMessage( id + "&mif&" + idName,"*");
			}else if (e.data.charAt(0) === "i") {//インラインフレームのスクロール処理(複数)【要検討】
				const idHeight = e.data.slice(1).split("&");
				const idName = idHeight[0];
				const ifScroll = Number(idHeight[1]);
				const ifHeight = $("#" + idName + "_if").offset().top;
				const scrollposition = ifHeight + ifScroll - navHeight - topSpace;
				$("html, body").animate({scrollTop:scrollposition}, 500, "linear");
			}else if (e.data.charAt(0) === "j") {//別ページへ遷移
				window.location.href = topUrl + e.data.slice(2);
			}else if (e.data.charAt(0) === "k") {//インラインフレーム切替
				if(e.data.indexOf("pgtransacc=") !== -1 ){//アクセスページへ遷移
					var receiveData = e.data.split("=");
					var id = receiveData[1];
					window.location.href = topUrl + "access/?"+id;
				}else{
					if(e.data.indexOf("swtype=") !== -1){//アクセス遷移後
						var receiveData = e.data.slice(1).split("=");
						var data = receiveData[1].split("_");
						var btntype = data[0];
						var id  = data[1];
						const mainIf = document.getElementById("main_if");
						const attrurl = mainIf.getAttribute("src");
						const afswtype = receiveData[1];
						mainIf.setAttribute("src", attrurl.replace("swtype="+ bfswtype(attrurl), "swtype=" + afswtype));
					}else if(e.data.indexOf("acc=") !== -1){//アクセス通常
						var receiveData = e.data.split("=");
						var data = receiveData[1].split("_");
						var btntype = data[0];
						var id = data[1];
					}else{//スタッフ
						if(e.data.indexOf("_") !== -1){
							var receiveData = e.data.slice(1).split("_");
							var btntype = receiveData[0];
							var id = receiveData[1];
						}else{
							var btntype = e.data.slice(1);
							var id = e.data.slice(1);
						}
						stflistLinkGen(btntype);//stf_list_btnリンク作成
					}
					if(id !== "staff"){
						paginationLinkGen(btntype,id);//btn_pre,btn_nextリンク作成
					}
					if($(".if_submenu").hasClass("hidden")){
						$(".if_submenu").removeClass("hidden").addClass("show");
					}
					if(mobile){
						floatingmenudel();//floatingmenu削除
					}
				}
			}
		break;
	}
}
function bfswtype(attrurl){//swtypeパラメータ取得
	const parameters = attrurl.substr(attrurl.indexOf("?") + 1).split( "&" );
	const swtypeprm = parameters.filter(function (item) {
		return item.indexOf("swtype") !== -1
	});
	const bfswtype = swtypeprm[0].split( "=" );
	return bfswtype[1];
}
function stflistLinkGen(btntype){//stf_list_btnリンク作成
	switch(true){
		case btntype === "b":
		case btntype === "law":
			var listname = "弁護士";
			var btntype = "law";
			break;
		case btntype === "z":
		case btntype === "ac":
			var listname = "税理士";
			var btntype = "ac";
			break;
		case btntype === "sr":
			var listname = "社会保険労務士・同試験合格者";
			break;
		case btntype === "gs":
			var listname = "行政書士・同試験合格者";
			break;
		case btntype === "tk":
			var listname = "宅地建物取引士・同試験合格者";
			break;
		case btntype === "fp":
			var listname = "FP・2級FP技能士";
			break;
		case btntype === "sonota":
		case btntype === "staff":
			var listname = "その他のスタッフ";
			var btntype = "staff";
			break;
		case btntype.indexOf("mokuji-") !== -1:
			var btntype = "ichiran";
		default:
	}
	const stfListBtn = document.getElementById("stf_list_btn");
	const stfSubmenu = document.getElementById("staff_submenu");
	const attrurl = stfListBtn.getAttribute("href");
	stfListBtn.setAttribute("href", attrurl.replace("swtype=" + bfswtype(attrurl), "swtype=mokuji-" + btntype));
	const text = listname+"一覧";
	stfListBtn.innerHTML = text;
	stfListBtn.removeAttribute("class");
	if( btntype === "staff" || btntype === "ichiran"){
		stfListBtn.classList.add("hidden");
	} else {
		stfListBtn.classList.add(btntype);
	}
}
function areaallTxtGen(corp){//area_all_btnのテキスト作成
	switch(true){
		case corp === "law":
			var corpname = "弁護士法人心";
			break;
		case corp === "ac":
			var corpname = "税理士法人心";
			break;
		case corp === "sr":
			var corpname = "社会保険労務士法人心";
			break;
		case corp === "grp":
			var corpname = "心グループ";
		default:
	}
	const areaAllBtn = document.getElementById("area_all_btn");
	const text = corpname+"の全事務所一覧";
	areaAllBtn.innerHTML = text;
}
function paginationLinkGen(btntype,id){//btn_pre,btn_nextリンク作成
	const pnData_array = pnData[btntype];
	console.dir(pnData_array);
	console.log(pnData_array);
	const sdata = pnData_array.find( (data) => data === id );
	const prevIndex = pnData_array.indexOf(sdata) - 1;
	const prev = prevIndex < 0 ? pnData_array[pnData_array.length - 1] : pnData_array[prevIndex];
	const nextIndex = pnData_array.indexOf(sdata) + 1;
	const next = nextIndex >= pnData_array.length ? pnData_array[0] : pnData_array[nextIndex];
	const btnPrev = document.getElementById("btn_prev");
	const btnNext = document.getElementById("btn_next");
	const attrPrev = btnPrev.getAttribute("href");
	const attrNext = btnNext.getAttribute("href");
	const ifPagination = document.querySelector(".if_pagination");
	const afswtype = btntype !== "access" ? "swtype=" + btntype + "_" : "swtype=";
	if(ifPagination.classList.contains("hidden")){
		ifPagination.classList.remove("hidden");
		ifPagination.classList.add("show");
	}
	btnPrev.setAttribute("href", attrPrev.replace("swtype="+ bfswtype(attrPrev), afswtype + prev));
	btnNext.setAttribute("href", attrNext.replace("swtype="+ bfswtype(attrNext), afswtype + next));
}
function floatingmenudel(){//floatingmenu削除
	const floatingMenu = document.querySelector(".floatingmenu");
	const toTopLink = document.querySelector(".to_top a");
	if(floatingMenu){
		floatingMenu.classList.add("hidden");
		const footer = document.querySelector(".footer");
		if (footer) {
			footer.style.paddingBottom = "";
		}
	}
	if (toTopLink) {
		toTopLink.style.paddingBottom = "";
		const pagetopHeight = document.querySelector(".to_top").clientHeight;
		const space = window.innerWidth * 0.005;
		document.querySelector(".if_pagination div").style.bottom = pagetopHeight + space +"px";
	}
}
function sendParenturl() {//mediainfo、massmediaページの場合url送信
	var parentUrl = location.href;
	var regexp = /mediainfo|massmedia/;
	if(regexp.test(parentUrl)){
		var win = document.getElementById("main_if").contentWindow;
		win.postMessage("pu," + parentUrl,"*");
	}
}
//データ送信用
$(window).on("load",function(){
	sendParenturl();//mediainfo、massmediaページの場合url送信
	if( 1 < window.location.search.length ){
		const id = window.location.search.substring(1);
		const win = document.getElementById("main_if").contentWindow;
		if(location.href.indexOf("/staff/")!== -1){//staffページの場合送信なし
			const swtype = id;
			var btntype = id.replace(/\d/g, "");
			if(btntype === "b"){
				var btntype = "law";
			}else if(btntype === "z"){
				var btntype = "ac";
			}
			const mainIf = document.getElementById("main_if");
			const attrurl = mainIf.getAttribute("src");
			const afswtype = btntype !=="staff" ?btntype + "_" + swtype:swtype;
			if(btntype.indexOf("mokuji-") === -1){
				mainIf.setAttribute("src", attrurl.replace("mokuji-if","index-if").replace("swtype="+ bfswtype(attrurl), "swtype=" + afswtype));
			}else{
				const mokujicorp = btntype.split( "-" );
				mainIf.setAttribute("src", attrurl + "-"+ mokujicorp[1]);
			}
			stflistLinkGen(btntype);//stf_list_btnリンク作成
			if($(".if_submenu").hasClass("hidden")){
				$(".if_submenu").removeClass("hidden").addClass("show");
			}
			if(btntype !== "staff" && btntype.indexOf("mokuji-") === -1){
				paginationLinkGen(btntype,id);//btn_pre,btn_nextリンク作成
			}
			if($(window).width() < 600){
				floatingmenudel();//floatingmenu削除
			}
		}else if(location.href.indexOf("/access/")!== -1){//accessページの場合?以下のid送信
			win.postMessage( "access=" + prm_corp + "_" +id + "&pn","*");
		}else{//?以下のid送信
			win.postMessage(id,"*");
		}
	}
});
//アコーディオンコンテンツ
$(function() {
	$('.riyuuttl_list>dt>a').append('');
	$('.riyuuttl_list>dd>span').append('');
	$('.riyuuttl_list>dt>a').click(function() {
		if($(this).hasClass('close')){
			$(this).text("詳しくはこちら");
			$(this).append('');
		} else {
			$(this).text("閉じる");
			$(this).append('');
		};
		$(this).parent().next().slideToggle();
		$(this).toggleClass('close');
		
	});
	$('.riyuuttl_list>dd>span').click(function() {
		$(this).parent().prev().children('a').text("詳しくはこちら");
		$(this).parent().prev().children('a').append('');
		$(this).parent().prev().children().toggleClass('close');
		var offsetTop = $(this).parent().prev().offset().top;
		var navHeight = $('.nav').height();
		var scrollPosition = offsetTop-navHeight-15;
		if($(window).scrollTop() > scrollPosition){
			$('html,body').animate({scrollTop: scrollPosition}, 500);
			$(this).parent().delay(400).slideToggle();
		}else{
			$(this).parent().slideToggle();
		}
	});
});
//選ばれる理由アコーディオン
$(function(){
	$('.riyuuttl_list .continue_btn').append('');
	$('.riyuuttl_list .riyuu_on').hide();
	$('.riyuuttl_list .continue_btn').click(function(){
		var navHeight = $('.nav').height();
		var topSpace = ($(window).width() < 600) ? $(window).width() * 0.04 : 15;
		var offsetTop = $(this).parent().offset().top - navHeight - topSpace;
		if($(window).scrollTop() > offsetTop) {
			$('html,body').animate({scrollTop:offsetTop}, 500);
			 $(this).prev().delay(400).slideToggle();
		} else {
			$(this).prev().slideToggle();
		}
	});
});
function openwin(url) {
	//印刷画面ポップアップ
	window.open(url, "", "width=960,height=450, scrollbars=yes,resizable=yes");
}
function openwinpp(url) {
	//プライバシーポリシーポップアップ
	var y = (screen.height-200) ;
	window.open(url,"","width=960,height="+y+", scrollbars=yes,resizable=yes");
}
function openparking(url) {
	//駐車場ポップアップ
	window.open(url, "", "width=740,height=600, scrollbars=yes,resizable=yes");
}