///////////////////////////////////////////////////////////////////////////////
//
//  grandpiano.xaml.js
//
//
// © 2007 Microsoft Corporation. All Rights Reserved.
//
// This file is licensed as part of the Silverlight 1.0 SDK, for details look
// here: http://go.microsoft.com/fwlink/?LinkID=89144&clcid=0x409
//
///////////////////////////////////////////////////////////////////////////////
if (!window.GrandPiano)
    window.GrandPiano = {};

GrandPiano.Piano = function() 
{
}

GrandPiano.Piano.prototype =
{
    handleLoad: function(control, userContext, rootElement) 
    {
        this.control = control;
        
        //keyup and down should be attached to the root canvas
        this.rootElement = rootElement;
                            this.rootElement.addEventListener("GotFocus", Silverlight.createDelegate(this, this.resetPiano));
                            this.rootElement.addEventListener("KeyDown", Silverlight.createDelegate(this, this.handleKeyDown));
                            this.rootElement.addEventListener("KeyUp", Silverlight.createDelegate(this, this.handleKeyUp));

        // Piano button event hookup: Find the button and then attach event handlers
        this.pathCanvas = rootElement.findName("pathCanvas");   
        
        for(var i = 0; i<this.pathCanvas.Children.Count; ++i)
        {
            this.pathCanvas.Children.GetItem(i).addEventListener("MouseLeftButtonDown", Silverlight.createDelegate(this, this.handleMouseDown));
            this.pathCanvas.Children.GetItem(i).addEventListener("MouseLeftButtonUp", Silverlight.createDelegate(this, this.handleMouseUp));
        }
    },
    
    handleMouseDown: function(sender, eventArgs) 
    {
        sender.captureMouse();
        var currentPianoKeyID = sender.name.substr(0, sender.name.indexOf("Path"));
        var currentImage = sender.findName("img" + currentPianoKeyID);
        currentImage.opacity = 1;
        var currentMediaElement = sender.findName(currentPianoKeyID);
        currentMediaElement.stop();
        currentMediaElement.play();
    },
    
    handleMouseUp: function(sender, eventArgs) 
    {
        sender.releaseMouseCapture();
        var currentPianoKeyID = sender.name.substr(0, sender.name.indexOf("Path"));
        var currentImage = sender.findName("img" + currentPianoKeyID);
        currentImage.opacity = 0;
    },

    pressKey: function (currentPianoKeyID)
    {
        var currentImage = this.control.content.findName("img" + currentPianoKeyID);
        currentImage.opacity = 1;
        var currentMediaElement = this.control.content.findName(currentPianoKeyID);
        currentMediaElement.stop();
        currentMediaElement.play();
    },

    depressKey: function (currentPianoKeyID)
    {
        var currentImage = this.control.content.findName("img" + currentPianoKeyID);
        currentImage.opacity = 0;
    },

    handleKeyDown: function (sender, eventArgs)
    {
        switch (eventArgs.Key)
        {
            case 55: this.pressKey("C");  break;
            case 48: this.pressKey("C2"); break;
            case 53: this.pressKey("D");  break;
            case 33: this.pressKey("D2"); break;
            case 32: this.pressKey("E");  break;
            case 51: this.pressKey("F");  break;
            case 36: this.pressKey("F2"); break;
            case 31: this.pressKey("G");  break;
            case 37: this.pressKey("G2"); break;
            case 43: this.pressKey("A");  break;
            case 39: this.pressKey("A2"); break;
            case 42: this.pressKey("B");  break;
        }
    },

    handleKeyUp: function (sender, eventArgs)
    {
        switch (eventArgs.Key)
        {
            case 55: this.depressKey("C");  break;
            case 48: this.depressKey("C2"); break;
            case 53: this.depressKey("D");  break;
            case 33: this.depressKey("D2"); break;
            case 32: this.depressKey("E");  break;
            case 51: this.depressKey("F");  break;
            case 36: this.depressKey("F2"); break;
            case 31: this.depressKey("G");  break;
            case 37: this.depressKey("G2"); break;
            case 43: this.depressKey("A");  break;
            case 39: this.depressKey("A2"); break;
            case 42: this.depressKey("B");  break;
        }
    },
    
    resetPiano: function(sender, eventArgs) 
    {
        // 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.
        this.depressKey("C");
        this.depressKey("C2");
        this.depressKey("D");
        this.depressKey("D2");
        this.depressKey("E");
        this.depressKey("F");
        this.depressKey("F2");
        this.depressKey("G");
        this.depressKey("G2");
        this.depressKey("A");
        this.depressKey("A2");
        this.depressKey("B");
    }
}