/* ============================================================
 article
============================================================ */

$(function(){

	$('main div.title button, main div.panel button').on('click', function() {
		const aysID = $(this).attr('class');
		sessionStorage.setItem('aysID', aysID);
		window.location.href = '/ja/ays-stories/';
	});

	$('main div.back a').on('click', function() {
		sessionStorage.setItem('cat', 'all');
		window.location.href = '/ja/ays-stories/';
	});

	$('main div.title p.date, main div.title h1, main div.title ul').wrapAll('<div class="inner"></div>');

	$('main div.intro ul.person dl').each(function() {
		$(this).after('<p class="cap sp"></p>');
		$(this).next('p.cap').html($(this).find('dd.cap').html());
	});

	$('main br.spacer').each(function() {
		$(this).after('<span class="spacer"></span>');
	});

});

$(window).on('load resize', function() {
	$('main div.intro ul.person li:has(dl.sub)').each(function() {
		let $li = $(this);
		let $mainDl = $li.find('> dl');
		let $subDl = $li.find('dl.sub');
		let $figure = $li.find('figure');
		if ($mainDl.length && $subDl.length) {
			let mainHeight = $mainDl.outerHeight();
			let subHeight = $subDl.outerHeight();
			let targetHeight = mainHeight - subHeight;
			$figure.css('height', targetHeight -25 + 'px');
		}
	});
});

document.addEventListener("DOMContentLoaded", () => {

	const url = document.querySelector('meta[property="og:url"]')?.content  || location.href;
	const title = document.querySelector('meta[property="og:title"]')?.content || document.title;
	const setHref = (sel, href) => document.querySelector(sel)?.setAttribute('href', href);

	setHref('div.sns li.sns-fb a', `https://www.facebook.com/share.php?u=${encodeURIComponent(url)}`);
	setHref('div.sns li.sns-x a', `https://twitter.com/share?url=${encodeURIComponent(url)}&text=${encodeURIComponent(title)}`);
	setHref('div.sns li.sns-line a', `https://line.me/R/msg/text/?${encodeURIComponent(title)}%0D%0A${encodeURIComponent(url)}`);

	document.querySelectorAll('div.sns ul li a').forEach(a => {
		a.addEventListener('click', e => {
			e.preventDefault();
			const href = a.getAttribute('href');
			const w = window.open(
				href,
				'win_small',
				'width=600,height=500,location=no,toolbar=no,menubar=no,resizable=no,scrollbars=yes,status=no'
			);
			if (w) {
				try { w.focus(); } catch(_) {}
			} else {
				window.open(href, '_blank');
			}
		});
	});

});

$(function() {
    const currentPath = window.location.pathname;

    const currentCategories = $('.title ul li button').map(function() {
        return $(this).attr('class');
    }).get();

    let articleList = [];

    $('.panel > ul > li').each(function() {
        const $this = $(this);
        const $link = $this.find('dd a');
        const articleHref = $link.attr('href');

        if (articleHref === currentPath) {
            $this.hide();
            return;
        }
        let score = 0;
        const targetCategories = $this.find('button').map(function() {
            return $(this).attr('class');
        }).get();

        targetCategories.forEach(function(cat) {
            if (currentCategories.includes(cat)) {
                score++;
            }
        });

        articleList.push({
            element: $this,
            score: score
        });

        $this.hide();
    });

    articleList.sort(function(a, b) {
        return b.score - a.score;
    });
    const $container = $('.panel > ul');
    const displayLimit = 3;

    articleList.slice(0, displayLimit).forEach(function(item) {
        item.element.show();
        $container.append(item.element); 
    });
});


