/**
* 東宝堂コーポレートサイト メインJS
*/
(function() {
'use strict';
// モバイルナビ
var ham = document.getElementById('hdHam');
var mn = document.getElementById('mn');
if (ham && mn) {
ham.addEventListener('click', function() {
ham.classList.toggle('on');
mn.classList.toggle('on');
});
var mobLinks = mn.querySelectorAll('.mob-link');
for (var i = 0; i < mobLinks.length; i++) {
mobLinks[i].addEventListener('click', function() {
ham.classList.remove('on');
mn.classList.remove('on');
});
}
}
// HUB CARDS
var hubCards = document.querySelectorAll('.hub-card');
hubCards.forEach(function(card) {
card.addEventListener('click', function() {
var detailId = this.dataset.detail;
var detail = document.getElementById(detailId);
var grid = document.getElementById('hubGrid');
var wasActive = this.classList.contains('active');
grid.querySelectorAll('.hub-card').forEach(function(c) { c.classList.remove('active'); });
grid.querySelectorAll('.hub-detail').forEach(function(d) { d.classList.remove('show'); });
if (!wasActive && detail) {
this.classList.add('active');
var cards = Array.from(grid.querySelectorAll('.hub-card'));
var thisTop = this.getBoundingClientRect().top;
var rowCards = cards.filter(function(c) { return Math.abs(c.getBoundingClientRect().top - thisTop) < 10; });
var lastInRow = rowCards[rowCards.length - 1];
lastInRow.insertAdjacentElement('afterend', detail);
detail.classList.add('show');
setTimeout(function() { detail.scrollIntoView({ behavior: 'smooth', block: 'nearest' }); }, 100);
}
});
});
// FAQ
var faqQuestions = document.querySelectorAll('.faq-q');
faqQuestions.forEach(function(q) {
q.addEventListener('click', function() {
var item = this.parentElement;
var answer = item.querySelector('.faq-a');
var isOpen = item.classList.contains('open');
document.querySelectorAll('.faq-item').forEach(function(i) {
i.classList.remove('open');
i.querySelector('.faq-a').style.maxHeight = null;
});
if (!isOpen) {
item.classList.add('open');
answer.style.maxHeight = answer.scrollHeight + 'px';
}
});
});
// BOT(プレースホルダー)
var botBtn = document.getElementById('botBtn');
var botWin = document.getElementById('botWin');
var botClose = document.getElementById('botClose');
function toggleBot() {
if (botWin) {
botWin.classList.toggle('on');
var bdg = document.querySelector('.bot-btn .bdg');
if (bdg) bdg.style.display = 'none';
}
}
if (botBtn) botBtn.addEventListener('click', toggleBot);
if (botClose) botClose.addEventListener('click', toggleBot);
var openBotBtn = document.getElementById('openBotBtn');
if (openBotBtn) {
openBotBtn.addEventListener('click', function() {
if (botWin) {
botWin.classList.add('on');
var bdg = document.querySelector('.bot-btn .bdg');
if (bdg) bdg.style.display = 'none';
}
});
}
// Bot クイック質問
var qButtons = document.querySelectorAll('.bot-q-btn');
qButtons.forEach(function(btn) {
btn.addEventListener('click', function() {
var bd = document.getElementById('botBd');
var u = document.createElement('div');
u.style.cssText = 'background:var(--primary);color:white;border-radius:12px 12px 4px 12px;padding:10px 14px;font-size:13px;margin-left:auto;max-width:80%;margin-bottom:10px;text-align:right;';
u.textContent = btn.textContent.trim();
bd.appendChild(u);
var qb = document.getElementById('botQb');
if (qb) qb.remove();
setTimeout(function() {
var m = document.createElement('div');
m.className = 'bot-msg';
m.innerHTML = 'DocsBotが接続されると、ここにAIの回答が表示されます。';
bd.appendChild(m);
bd.scrollTop = bd.scrollHeight;
}, 500);
bd.scrollTop = bd.scrollHeight;
});
});
// Bot 送信
var botSend = document.getElementById('botSend');
var botIn = document.getElementById('botIn');
function sendBot() {
if (!botIn || !botIn.value.trim()) return;
var bd = document.getElementById('botBd');
var u = document.createElement('div');
u.style.cssText = 'background:var(--primary);color:white;border-radius:12px 12px 4px 12px;padding:10px 14px;font-size:13px;margin-left:auto;max-width:80%;margin-bottom:10px;text-align:right;';
u.textContent = botIn.value;
bd.appendChild(u);
botIn.value = '';
setTimeout(function() {
var m = document.createElement('div');
m.className = 'bot-msg';
m.innerHTML = 'DocsBotが接続されると、ここにAIの回答が表示されます。';
bd.appendChild(m);
bd.scrollTop = bd.scrollHeight;
}, 500);
bd.scrollTop = bd.scrollHeight;
}
if (botSend) botSend.addEventListener('click', sendBot);
if (botIn) {
botIn.addEventListener('keypress', function(e) {
if (e.key === 'Enter') sendBot();
});
}
// SCROLL REVEAL
var obs = new IntersectionObserver(function(entries) {
entries.forEach(function(e) {
if (e.isIntersecting) e.target.classList.add('vis');
});
}, { threshold: 0.1 });
document.querySelectorAll('.rv').forEach(function(el) { obs.observe(el); });
// AUTO OPEN BOT(3秒後)
setTimeout(function() {
if (botWin && !botWin.classList.contains('on')) {
botWin.classList.add('on');
var bdg = document.querySelector('.bot-btn .bdg');
if (bdg) bdg.style.display = 'none';
}
}, 3000);
})();