File size: 4,466 Bytes
c8c0dcb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
document.addEventListener('DOMContentLoaded', function() {
    // Poem data
    const poems = [
        {
            text: "In the quiet of the evening, under skies so wide, I find my heart beating close to yours, my love. The stars above whisper secrets of our bond, eternal as the night, deep as the ocean's song.",
            keywords: ["evening", "love", "stars"]
        },
        {
            text: "Autumn leaves dance in crimson and gold, mirroring the fire that burns within my soul. Each leaf that falls is a promise made, that in your arms is where I'll fade.",
            keywords: ["autumn", "leaves", "fire"]
        },
        {
            text: "Your eyes are the moonlight that guides me through the dark, your touch the spark that ignites the fire in my heart. In this vast universe, you are my constant star, my love, my life, no matter near or far.",
            keywords: ["moon", "eyes", "universe"]
        },
        {
            text: "Like the river finds the sea, my heart has found its way to thee. Through storm and sunshine, come what may, in your love I'll always stay.",
            keywords: ["river", "sea", "love"]
        },
        {
            text: "In the garden of your heart, I plant my love so true. With every beat it grows, a flower just for you. The petals soft as whispers, the scent so sweet and light, blooming in the sunshine of your love so bright.",
            keywords: ["garden", "heart", "flower"]
        }
    ];

    // DOM elements
    const poemPrompt = document.getElementById('poem-prompt');
    const generateBtn = document.getElementById('generate-btn');
    const poemText = document.getElementById('poem-text');
    const poemContainer = document.getElementById('poem-container');
    const prevBgBtn = document.getElementById('prev-bg');
    const nextBgBtn = document.getElementById('next-bg');
    const bgSelect = document.getElementById('bg-select');

    // Background images
    const backgrounds = [
        { id: 'autumn', path: 'assets/images/bg-autumn.svg' },
        { id: 'stars', path: 'assets/images/bg-stars.svg' },
        { id: 'hearts', path: 'assets/images/bg-hearts.svg' }
    ];

    let currentBgIndex = 0;

    // Set initial background
    updateBackground();

    // Event listeners
    generateBtn.addEventListener('click', generatePoem);
    poemPrompt.addEventListener('keypress', function(e) {
        if (e.key === 'Enter') {
            generatePoem();
        }
    });

    prevBgBtn.addEventListener('click', showPreviousBackground);
    nextBgBtn.addEventListener('click', showNextBackground);
    bgSelect.addEventListener('change', function() {
        const selectedId = this.value;
        const index = backgrounds.findIndex(bg => bg.id === selectedId);
        if (index !== -1) {
            currentBgIndex = index;
            updateBackground();
        }
    });

    // Functions
    function generatePoem() {
        const input = poemPrompt.value.toLowerCase();

        // Simple filtering based on input keywords
        let filteredPoems = poems;
        if (input.trim()) {
            const keywords = input.split(/\s+/);
            filteredPoems = poems.filter(poem => {
                return keywords.some(keyword =>
                    poem.keywords.some(kw => kw.includes(keyword))
                );
            });
        }

        // If no matching poems, use all poems
        if (filteredPoems.length === 0) {
            filteredPoems = poems;
        }

        // Select random poem
        const randomIndex = Math.floor(Math.random() * filteredPoems.length);
        const selectedPoem = filteredPoems[randomIndex];

        // Display poem with animation
        poemText.style.opacity = 0;
        setTimeout(() => {
            poemText.textContent = selectedPoem.text;
            poemText.style.opacity = 1;

            // Change background
            showNextBackground();
        }, 300);
    }

    function updateBackground() {
        const currentBg = backgrounds[currentBgIndex];
        document.body.style.backgroundImage = `url('${currentBg.path}')`;
        bgSelect.value = currentBg.id;
    }

    function showNextBackground() {
        currentBgIndex = (currentBgIndex + 1) % backgrounds.length;
        updateBackground();
    }

    function showPreviousBackground() {
        currentBgIndex = (currentBgIndex - 1 + backgrounds.length) % backgrounds.length;
        updateBackground();
    }
});