File size: 3,749 Bytes
9e01f6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Shared JavaScript across all pages

// Scroll animation handler
document.addEventListener('DOMContentLoaded', function() {
    const fadeElements = document.querySelectorAll('.fade-in');
    
    const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
            if (entry.isIntersecting) {
                entry.target.classList.add('visible');
            }
        });
    }, { threshold: 0.1 });
    
    fadeElements.forEach(element => {
        observer.observe(element);
    });
});

// Audio pronunciation function
function playJapaneseAudio(text) {
    const utterance = new SpeechSynthesisUtterance(text);
    utterance.lang = 'ja-JP';
    utterance.rate = 0.8;
    speechSynthesis.speak(utterance);
}

// Particle effect for correct answers
function createParticles(x, y, count = 20) {
    for (let i = 0; i < count; i++) {
        const particle = document.createElement('div');
        particle.className = 'particle';
        particle.style.left = `${x}px`;
        particle.style.top = `${y}px`;
        
        const angle = Math.random() * Math.PI * 2;
        const velocity = 2 + Math.random() * 2;
        const vx = Math.cos(angle) * velocity;
        const vy = Math.sin(angle) * velocity;
        
        document.body.appendChild(particle);
        
        setTimeout(() => {
            particle.style.transform = `translate(${vx * 100}px, ${vy * 100}px)`;
        }, 10);
        
        setTimeout(() => {
            particle.remove();
        }, 1000);
    }
}

// Local storage for user progress
const userProgress = {
    get: function(key) {
        try {
            return JSON.parse(localStorage.getItem(`ninja_${key}`)) || 0;
        } catch {
            return 0;
        }
    },
    
    set: function(key, value) {
        localStorage.setItem(`ninja_${key}`, JSON.stringify(value));
    },
    
    updateStreak: function() {
        const today = new Date().toDateString();
        const lastVisit = this.get('last_visit');
        
        if (lastVisit !== today) {
            const streak = this.get('streak') || 0;
            const yesterday = new Date();
            yesterday.setDate(yesterday.getDate() - 1);
        
        if (lastVisit === yesterday.toDateString() || !lastVisit) {
            this.set('streak', streak + 1);
        } else {
            this.set('streak', 1);
        }
        
        this.set('last_visit', today);
        return this.get('streak');
    }
};

// Typing animation for Japanese text
function typeJapaneseText(element, text, speed = 50) {
    let i = 0;
    element.textContent = '';
    
    function type() {
        if (i < text.length) {
            element.textContent += text.charAt(i);
            i++;
            setTimeout(type, speed);
        }
    }
    type();
}

// Shuffle array for quiz questions
function shuffleArray(array) {
    const shuffled = [...array];
    for (let i = shuffled.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
    }
    return shuffled;
}

// API call to get Japanese vocabulary
async function getJapaneseVocabulary(level = 'n5') {
    try {
        // Using a free Japanese API
        const response = await fetch(`https://jisho.org/api/v1/search/words?keyword=%23common&page=1`);
        const data = await response.json();
        return data.data || [];
    } catch (error) {
        console.error('Failed to fetch vocabulary:', error);
        return [];
    }
}

// Initialize app
document.addEventListener('DOMContentLoaded', function() {
    // Update user streak
    userProgress.updateStreak();
    
    console.log('Nihongo Ninja Dojo loaded successfully!');
});