document.addEventListener('DOMContentLoaded',()=>{const selectMajorScaleElement=document.getElementById('majorscale');const selectMinorScaleElement=document.getElementById('minorscale');const selectMinorElement=document.getElementById('minorchord');const selectMajorElement=document.getElementById('majorchord');const selectDiminishedElement=document.getElementById('diminishedchord');const selectAugmentedElement=document.getElementById('augmentedchord');var showchord=document.getElementById('showchord');const pianoKeys=document.querySelectorAll('.piano-keys .key');const majorScaleIntervals=[2,2,1,2,2,2,1];const allNotes=['C','C#','D','D#','E','F','F#','G','G#','A','A#','B'];const solfegeMap={'C':'Do','C#':'Do#','D':'Re','D#':'Re#','E':'Mi','F':'Fa','F#':'Fa#','G':'So','G#':'So#','A':'La','A#':'La#','B':'Ti'};const indianMap={'C':'सा','C#':'रे♭','D':'रे','D#':'गा♭','E':'गा','F':'मा','F#':'मा#','G':'प','G#':'धा♭','A':'धा','A#':'नि♭','B':'नि'};function matchNote(keyElement,noteLetter){const keyNote=keyElement.dataset.note;if(keyNote===noteLetter){return!0}
const noteLabelSpan=keyElement.querySelector('.note-label');if(!noteLabelSpan)return!1;const label=noteLabelSpan.textContent;return(label===solfegeMap[noteLetter]||label===indianMap[noteLetter])}
function getMajorScale(rootNote){let scale=[];let startIndex=allNotes.indexOf(rootNote);if(startIndex===-1)return scale;scale.push(allNotes[startIndex]);let currentIndex=startIndex;for(let i=0;i<majorScaleIntervals.length;i++){currentIndex=(currentIndex+majorScaleIntervals[i])%allNotes.length;scale.push(allNotes[currentIndex])}
return scale}
const commonMajorProgressions={'C':['C','G','Am','F'],'C#':['C#','G#','A#m','F#'],'D':['D','A','Bm','G'],'D#':['D#','A#','Cm','G#'],'E':['E','B','C#m','A'],'F':['F','C','Dm','Bb'],'F#':['F#','C#','D#m','B'],'G':['G','D','Em','C'],'G#':['G#','D#','Fm','C#'],'A':['A','E','F#m','D'],'A#':['A#','F','Gm','D#'],'B':['B','F#','G#m','E'],};selectMajorScaleElement.addEventListener('change',(event)=>{const selectedScale=event.target.value;const scaleNotes=getMajorScale(selectedScale);selectMajorElement.selectedIndex=0;selectMinorElement.selectedIndex=0;selectDiminishedElement.selectedIndex=0;selectAugmentedElement.selectedIndex=0;selectMinorScaleElement.selectedIndex=0;pianoKeys.forEach(keyElement=>keyElement.classList.remove('mark'));pianoKeys.forEach(keyElement=>{if(scaleNotes.some(note=>matchNote(keyElement,note))){keyElement.classList.add('mark')}
if(scaleNotes.length){showchord.value=`${selectedScale} MAJOR SCALE`}else{showchord.value=''}});const firstKey=document.querySelector('.piano-keys .key');if(firstKey){firstKey.focus();firstKey.scrollIntoView({behavior:'smooth',block:'center'})}
const progressionEl=document.getElementById('common-progression');if(commonMajorProgressions[selectedScale]){const progression=commonMajorProgressions[selectedScale].join(' → ');progressionEl.textContent=`Common Progression: ${progression}`}else{progressionEl.textContent=''}})})