File size: 11,136 Bytes
cbe204b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
let currentStep = 1;
let audioFiles = {};
let selectedAvatars = [];

function nextStep() {
    if (validateCurrentStep()) {
        if (currentStep < 4) {
            document.querySelector(`.step[data-step="${currentStep}"]`).classList.remove('active');
            document.getElementById(`step${currentStep}`).classList.remove('active');
            currentStep++;
            document.querySelector(`.step[data-step="${currentStep}"]`).classList.add('active');
            document.getElementById(`step${currentStep}`).classList.add('active');
            
            if (currentStep === 4) {
                updateGenerationSummary();
            }
        }
    }
}

function prevStep() {
    if (currentStep > 1) {
        document.querySelector(`.step[data-step="${currentStep}"]`).classList.remove('active');
        document.getElementById(`step${currentStep}`).classList.remove('active');
        currentStep--;
        document.querySelector(`.step[data-step="${currentStep}"]`).classList.add('active');
        document.getElementById(`step${currentStep}`).classList.add('active');
    }
}

function validateCurrentStep() {
    if (currentStep === 1) {
        const title = document.getElementById('videoTitle').value;
        if (!title) {
            showToast('Please enter a video title', 'error');
            return false;
        }
    } else if (currentStep === 2) {
        const numSpeakers = parseInt(document.getElementById('numSpeakers').value);
        for (let i = 1; i <= numSpeakers; i++) {
            if (!audioFiles[i]) {
                showToast(`Please upload audio for Speaker ${i}`, 'error');
                return false;
            }
        }
    } else if (currentStep === 3) {
        const numSpeakers = parseInt(document.getElementById('numSpeakers').value);
        const selectedOptions = document.querySelectorAll('.avatar-option.selected');
        if (selectedOptions.length !== numSpeakers) {
            showToast(`Please select ${numSpeakers} avatars`, 'error');
            return false;
        }
    }
    return true;
}

function updateSpeakerCount() {
    const numSpeakers = parseInt(document.getElementById('numSpeakers').value);
    const container = document.getElementById('audioUploadContainer');
    
    if (currentStep === 1 || currentStep === 2) {
        container.innerHTML = '';
        for (let i = 1; i <= numSpeakers; i++) {
            const audioItem = document.createElement('div');
            audioItem.className = 'audio-upload-item';
            audioItem.setAttribute('data-speaker', i);
            audioItem.innerHTML = `
                <label>Speaker ${i} Audio</label>
                <div class="upload-zone" onclick="document.getElementById('audio${i}').click()">
                    <svg width="40" height="40" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                        <path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"></path>
                        <polyline points="17 8 12 3 7 8"></polyline>
                        <line x1="12" y1="3" x2="12" y2="15"></line>
                    </svg>
                    <span>Click to upload or drag & drop</span>
                    <small>MP3, WAV, M4A (Max 120MB)</small>
                </div>
                <input type="file" id="audio${i}" accept="audio/*" style="display: none;" onchange="handleAudioUpload(this, ${i})">
                <div class="audio-preview" style="display: none;">
                    <div class="audio-info"></div>
                </div>
            `;
            container.appendChild(audioItem);
        }
    }
}

function handleAudioUpload(input, speakerId) {
    const file = input.files[0];
    if (file) {
        audioFiles[speakerId] = file;
        const preview = input.parentElement.querySelector('.audio-preview');
        const uploadZone = input.parentElement.querySelector('.upload-zone');
        const audioInfo = preview.querySelector('.audio-info');
        
        preview.style.display = 'block';
        uploadZone.style.display = 'none';
        audioInfo.innerHTML = `
            <strong>${file.name}</strong><br>
            Size: ${(file.size / 1024 / 1024).toFixed(2)} MB<br>
            Type: ${file.type}
        `;
        
        showToast(`Audio for Speaker ${speakerId} uploaded successfully`, 'success');
    }
}

// Avatar selection
document.addEventListener('DOMContentLoaded', function() {
    const avatarOptions = document.querySelectorAll('.avatar-option');
    avatarOptions.forEach(option => {
        option.addEventListener('click', function() {
            const numSpeakers = parseInt(document.getElementById('numSpeakers').value);
            const selectedOptions = document.querySelectorAll('.avatar-option.selected');
            
            if (selectedOptions.length >= numSpeakers && !this.classList.contains('selected')) {
                showToast(`You can only select ${numSpeakers} avatars`, 'info');
                return;
            }
            
            this.classList.toggle('selected');
            updateSelectedAvatars();
        });
    });
    
    // Update speaker count on change
    document.getElementById('numSpeakers').addEventListener('change', updateSpeakerCount);
    
    // Initialize drag and drop
    initDragAndDrop();
});

function updateSelectedAvatars() {
    selectedAvatars = [];
    document.querySelectorAll('.avatar-option.selected').forEach(option => {
        selectedAvatars.push(option.dataset.avatar);
    });
}

function updateGenerationSummary() {
    const duration = document.getElementById('duration').options[document.getElementById('duration').selectedIndex].text;
    const numSpeakers = document.getElementById('numSpeakers').value;
    const background = document.getElementById('background').options[document.getElementById('background').selectedIndex].text;
    
    document.getElementById('summaryDuration').textContent = duration;
    document.getElementById('summarySpeakers').textContent = numSpeakers;
    document.getElementById('summaryBackground').textContent = background;
}

function startGeneration() {
    const btn = event.target;
    const btnText = btn.querySelector('.btn-text');
    const btnLoader = btn.querySelector('.btn-loader');
    
    btnText.style.display = 'none';
    btnLoader.style.display = 'inline-block';
    btn.disabled = true;
    
    // Simulate generation process
    setTimeout(() => {
        btnText.style.display = 'inline';
        btnLoader.style.display = 'none';
        btn.disabled = false;
        
        // Show preview section
        document.querySelector('.generator-section').style.display = 'none';
        document.getElementById('previewSection').style.display = 'block';
        
        // Simulate video URL
        const video = document.getElementById('generatedVideo');
        video.src = 'https://www.w3schools.com/html/mov_bbb.mp4';
        
        showToast('Video generated successfully!', 'success');
        
        // Scroll to preview
        document.getElementById('previewSection').scrollIntoView({ behavior: 'smooth' });
    }, 3000);
}

function downloadVideo() {
    showToast('Preparing download...', 'info');
    setTimeout(() => {
        showToast('Download started!', 'success');
    }, 1500);
}

function shareVideo() {
    if (navigator.share) {
        navigator.share({
            title: 'Generated Conversational Video',
            text: 'Check out this AI-generated conversational video!',
            url: window.location.href
        });
    } else {
        showToast('Share link copied to clipboard!', 'success');
    }
}

function resetGenerator() {
    currentStep = 1;
    audioFiles = {};
    selectedAvatars = [];
    
    // Reset form
    document.getElementById('videoTitle').value = '';
    document.getElementById('duration').selectedIndex = 0;
    document.getElementById('numSpeakers').selectedIndex = 0;
    document.getElementById('background').selectedIndex = 0;
    
    // Reset steps
    document.querySelectorAll('.step').forEach(step => step.classList.remove('active'));
    document.querySelectorAll('.step-panel').forEach(panel => panel.classList.remove('active'));
    document.querySelector('.step[data-step="1"]').classList.add('active');
    document.getElementById('step1').classList.add('active');
    
    // Reset avatar selection
    document.querySelectorAll('.avatar-option').forEach(option => option.classList.remove('selected'));
    
    // Hide preview, show generator
    document.getElementById('previewSection').style.display = 'none';
    document.querySelector('.generator-section').style.display = 'block';
    
    // Scroll to generator
    document.getElementById('generator').scrollIntoView({ behavior: 'smooth' });
}

function scrollToGenerator() {
    document.getElementById('generator').scrollIntoView({ behavior: 'smooth' });
}

function showToast(message, type = 'info') {
    const toast = document.createElement('div');
    toast.className = `toast ${type}`;
    toast.textContent = message;
    document.body.appendChild(toast);
    
    setTimeout(() => toast.classList.add('show'), 100);
    setTimeout(() => {
        toast.classList.remove('show');
        setTimeout(() => toast.remove(), 300);
    }, 3000);
}

function initDragAndDrop() {
    document.addEventListener('dragover', (e) => {
        if (e.target.classList.contains('upload-zone')) {
            e.preventDefault();
            e.target.style.borderColor = 'var(--primary)';
            e.target.style.background = 'var(--bg)';
        }
    });
    
    document.addEventListener('dragleave', (e) => {
        if (e.target.classList.contains('upload-zone')) {
            e.target.style.borderColor = 'var(--border)';
            e.target.style.background = 'transparent';
        }
    });
    
    document.addEventListener('drop', (e) => {
        if (e.target.classList.contains('upload-zone')) {
            e.preventDefault();
            e.target.style.borderColor = 'var(--border)';
            e.target.style.background = 'transparent';
            
            const files = e.dataTransfer.files;
            if (files.length > 0) {
                const speakerId = e.target.parentElement.dataset.speaker;
                const input = e.target.parentElement.querySelector('input[type="file"]');
                input.files = files;
                handleAudioUpload(input, speakerId);
            }
        }
    });
}

// Smooth scroll for navigation
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
    anchor.addEventListener('click', function (e) {
        e.preventDefault();
        const target = document.querySelector(this.getAttribute('href'));
        if (target) {
            target.scrollIntoView({ behavior: 'smooth', block: 'start' });
        }
    });
});

// Header scroll effect
window.addEventListener('scroll', () => {
    const header = document.querySelector('.header');
    if (window.scrollY > 100) {
        header.style.boxShadow = '0 2px 20px rgba(0,0,0,0.1)';
    } else {
        header.style.boxShadow = 'none';
    }
});