// =========================
//
// Piano Class
//
//
// =========================

var wpfe;

// --------------------------------------
// CONSTRUCTOR
// --------------------------------------
function Piano (sender, eventArgs)
{
   wpfe = sender;
}

// Start playback on key down, change image
function keyDown (sender, eventArgs)
{
   switch (eventArgs.Key)
   {
      case 55: pressKey("C");  break;
      case 48: pressKey("C2"); break;
      case 53: pressKey("D");  break;
      case 33: pressKey("D2"); break;
      case 32: pressKey("E");  break;
      case 51: pressKey("F");  break;
      case 36: pressKey("F2"); break;
      case 31: pressKey("G");  break;
      case 37: pressKey("G2"); break;
      case 43: pressKey("A");  break;
      case 39: pressKey("A2"); break;
      case 42: pressKey("B");  break;
   }
}

// Change the image back on key up
function keyUp (sender, eventArgs)
{
   switch (eventArgs.Key)
   {
      case 55: depressKey("C");  break;
      case 48: depressKey("C2"); break;
      case 53: depressKey("D");  break;
      case 33: depressKey("D2"); break;
      case 32: depressKey("E");  break;
      case 51: depressKey("F");  break;
      case 36: depressKey("F2"); break;
      case 31: depressKey("G");  break;
      case 37: depressKey("G2"); break;
      case 43: depressKey("A");  break;
      case 39: depressKey("A2"); break;
      case 42: depressKey("B");  break;
   }
}

function pressKey(currentPianoKeyID)
{
   var currentImage = wpfe.findName("img" + currentPianoKeyID);
   currentImage.opacity = 1;
   var currentMediaElement = wpfe.findName(currentPianoKeyID);
   currentMediaElement.stop();
   currentMediaElement.play();
}

function depressKey(currentPianoKeyID)
{
   var currentImage = wpfe.findName("img" + currentPianoKeyID);
   currentImage.opacity = 0;
}

// In order to avoid problems when the control looses focus while a key is held down, 
// we use this function to reset the piano to its initial state (no key pressed). It is 
// called on the root canvas' GotFocus event.
function resetPiano()
{
      depressKey("C");
      depressKey("C2");
      depressKey("D");
      depressKey("D2");
      depressKey("E");
      depressKey("F");
      depressKey("F2");
      depressKey("G");
      depressKey("G2");
      depressKey("A");
      depressKey("A2");
      depressKey("B");
}