$(function(){
	//狭い画面幅でのグローバルナビゲーション
	$('').appendTo('header [role="navigation"]');
	$('', {
		'value': '',
		'text': 'メニュー',
		'selected': 'selected'
	}).appendTo('header [role="navigation"] > select');
	$('header [role="navigation"] a').each(function() {
		var el = $(this);
		$('', {
			'value': el.attr('href'),
			'text': el.text()
		}).appendTo('header [role="navigation"] > select');
	});
	$('header [role="navigation"] > select, .widget-archive-dropdown select').change(function() {
		window.location = $(this).find('option:selected').val();
	});
	//トップへ戻るボタン
	$(document).ready(function(){
        // scroll body to 0px on click
        $('#back_top a').click(function () {
            $('body,html').animate({scrollTop: 0}, '400');
            return false;
    	});
	});
});
window.addEventListener("message", receiveRun, false);
function receiveRun(e) {
	switch(e.origin){
		case "https://www.bengoshi-kokoro.com":
		case "https://www.iframe.law-kokoro.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) === "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");
			}
		break;
	}
}
//ページ操作・解析可能後データ処理:?key=pageid形式のpageidをiframe srcに追記
$(document).ready(function(){
	if( 1 < window.location.search.length ){//?以下の文字数チェック
		var params = window.location.search.substring(1);//substring[1]で?より後ろを取得
		if( params.search(/[&=]/) != -1 ){//?以降に&か=がある場合実行
			var param = params.split('&');
			var paramArray = [];
			for ( i = 0; i < param.length; i++ ) {
				neet = param[i].split("=");
				paramArray.push(neet[0]);
				paramArray[neet[0]] = neet[1];
			}
			document.getElementById('main_if').src = document.getElementById('main_if').src + paramArray['page'];
		}
	}
});
//ページデータ読み込み完了後データ送信:?anchorname形式のanchorname送信
$(window).on('load',function(){
	if( 1 < window.location.search.length ){//?以下の文字数チェック
		var params = window.location.search.substring(1);//substring[1]で?より後ろを取得
		if( params.search(/[&=]/) == -1 ){//?以降に&も=もない場合実行
			var win = document.getElementById('main_if').contentWindow;
			win.postMessage(params,'*');
		}
	}
});
function openparking(url) {//駐車場ポップアップ
	window.open(url, "", "width=740,height=600, scrollbars=yes,resizable=yes");
}