`;
planDisplayArea.innerHTML = contentHTML;
// Update active button style
const buttons = daySelector.querySelectorAll('button');
buttons.forEach(btn => {
if (btn.textContent === day) {
btn.classList.add('active', 'bg-blue-500', 'text-white');
btn.classList.remove('bg-white', 'text-blue-600');
} else {
btn.classList.remove('active', 'bg-blue-500', 'text-white');
btn.classList.add('bg-white', 'text-blue-600');
}
});
};
// --- PDF DOWNLOAD LOGIC ---
window.downloadPDF = function() {
const { jsPDF } = window.jspdf;
const doc = new jsPDF('p', 'mm', 'a4');
const pageHeight = doc.internal.pageSize.height;
let yPos = 20;
const addText = (text, x, y, size, style, color = [0, 0, 0]) => {
doc.setFontSize(size);
doc.setFont('helvetica', style);
doc.setTextColor(color[0], color[1], color[2]);
doc.text(text, x, y, { maxWidth: 180 });
};
const checkPageBreak = (requiredSpace) => {
if (yPos + requiredSpace > pageHeight - 20) {
doc.addPage();
yPos = 20;
}
};
// Title
addText('Optimal Aging Exercise Plan', 105, 15, 18, 'bold');
doc.setLineWidth(0.5);
doc.line(15, 18, 195, 18);
// Intro
addText('Philosophy', 15, yPos + 5, 14, 'bold');
addText("This plan is built on four pillars of physical fitness crucial for healthy aging: Cardiovascular Health, Strength & Bone Density, Flexibility & Mobility, and Balance & Stability.", 15, yPos + 12, 10, 'normal');
yPos += 25;
// Weekly Plan
Object.keys(weeklyPlan).forEach(day => {
const plan = weeklyPlan[day];
checkPageBreak(30); // Estimate space for a day's plan
doc.setLineWidth(0.2);
doc.line(15, yPos - 2, 195, yPos - 2);
addText(`${day}: ${plan.title}`, 15, yPos + 5, 12, 'bold', [0, 0, 150]);
yPos += 10;
plan.details.forEach(detail => {
addText(detail.heading, 20, yPos, 10, 'bold');
yPos += 5;
if (detail.text) {
const lines = doc.splitTextToSize(detail.text, 165);
addText(lines, 25, yPos, 10, 'normal');
yPos += (lines.length * 4) + 2;
}
if (detail.items) {
detail.items.forEach(item => {
const lines = doc.splitTextToSize(`• ${item}`, 160);
addText(lines, 25, yPos, 10, 'normal');
yPos += (lines.length * 4) + 1;
});
}
yPos += 2;
});
});
// Considerations
checkPageBreak(40);
doc.setLineWidth(0.5);
doc.line(15, yPos, 195, yPos);
yPos += 5;
addText('Important Considerations', 15, yPos, 14, 'bold');
yPos += 7;
const considerations = [
"Warm-Up & Cool-Down: Never skip them to prevent injury and help with recovery.",
"Listen to Your Body: You should feel challenged, but not in pain. If something hurts, stop.",
"Progression: As you get stronger, gradually increase the duration, intensity, or weight/resistance.",
"Hydration: Drink plenty of water before, during, and after exercise."
];
considerations.forEach(item => {
const lines = doc.splitTextToSize(`• ${item}`, 170);
addText(lines, 20, yPos, 10, 'normal');
yPos += (lines.length * 4) + 2;
});
// Disclaimer
addText("Disclaimer: This is a general fitness guide and not a substitute for professional medical advice. Please consult with a doctor before beginning any new exercise program.", 15, pageHeight - 15, 8, 'italic');
doc.save(`Optimal-Aging-Exercise-Plan.pdf`);
};
// --- INITIAL LOAD ---
renderDayButtons();
showPlanForDay('Monday'); // Show Monday's plan by default
});