
$(document).ready(function(){
    $('.keyboard .key').hover(
        function(){
            $(this).addClass('highlight');
        },
        function(){
            $(this).removeClass('highlight');
        }
    );
});

function showChord()
{
	var note = $('#noteSelect option:selected').val();
    var chord = $('#chordSelect option:selected').val();
    
    // TODO: validate note & chord
    
    var args = {
        ajax: "true",
        op: "getChord",
        note: note,
        chord: chord
    };
    
    $.getJSON(location.pathname, args, showChordCallback);
    
}

function showChordCallback(json)
{
    // check for status
    if (!json.status) return false;
    
    // remove previous scale highlights
    clearScaleHighlights();
    
    // get an octave count
    var octaveCount = getOctaveCount();
    
    // add css classes to highlight keys
    for (var i=0; i<json.chordNotes.length; i++)
    {
        
    	keyOctave = 1;
    	
        // if there are more than 1 octave, span scale notes onto second octave
        if (octaveCount>1)
        {
            if (jQuery.inArray(json.chordNotes[i], json.notesC) < jQuery.inArray(json.chordNotes[0], json.notesC))
            {
                keyOctave = 2;
            }
        }
    	
    	// check for sharps
        if (json.chordNotes[i][1]='#')
        {
            note = json.chordNotes[i].replace('#','-sharp'); 
        }
        else
        {
            note = json.chordNotes[i];
        }
        
        
    	key = '.keyboard #key_'+note+'_'+keyOctave;
        
        $(key).addClass('scaleHighlight');
        
    }
    
}

function showScale()
{
    var note = $('#noteSelect option:selected').val();
    var scale = $('#scaleSelect option:selected').val();

    // TODO: validate note & scale

    var args = {
        ajax: "true",
        op: "getScale",
        note: note,
        scale: scale
    };

    $.getJSON(location.pathname, args, showScaleCallback);
    
}

function showScaleCallback(json)
{
    
    // check for status
    if (!json.status) return false;
    
    // remove previous scale highlights
    clearScaleHighlights();
    
    // get an octave count
    var octaveCount = getOctaveCount();

    // add css classes to highlight keys
    for (var i=0; i<json.scaleNotes.length; i++)
    {

        keyOctave = 1;

        // if there are more than 1 octave, span scale notes onto second octave
        if (octaveCount>1)
        {
            if (jQuery.inArray(json.scaleNotes[i], json.notesC) < jQuery.inArray(json.scaleNotes[0], json.notesC))
            {
                keyOctave = 2;
            }
        }

        // check for sharps
        if (json.scaleNotes[i][1]='#')
        {
            note = json.scaleNotes[i].replace('#','-sharp'); 
        }
        else
        {
            note = json.scaleNotes[i];
        }

        key = '.keyboard #key_'+note+'_'+keyOctave;
        
        $(key).addClass('scaleHighlight');
    }
    
}

function clearScaleHighlights()
{
    $('.keyboard .key').removeClass('scaleHighlight');  
}

function getOctaveCount()
{
    return $('.keyboard .key').length / 12;    
}

