﻿// This JavaScript file defines the object "button"
Button = function(buttonList, nIndex, oDataObject, aXY, sState)
{
    // Make object root scope accessible to inner objects
    var that = this;
    
    // Make arguments and derived varibles public
    this._buttonList = buttonList;
    this._slRootObject = buttonList._slRootObject;
    this._ID = buttonList._sCollectionName + nIndex + "_";
    this._nIndex = nIndex;
    this._parent = buttonList._eParent;
    this._oDataObject = oDataObject;
    this._host = this._parent.getHost();
    this._aXY = aXY;
    this._sControlXAML = buttonList._sButtonXAML;
    this._sState = sState;
    
    // Set default options
    this._margin = 3;

    // The first step is to retrive the XAML content for the "button"
    this._buildControl();
}

Button.prototype =
{
    // Resize and position elements
    _autoSize : function()
    {
        // Position elements
        this._button["Canvas.Left"] = this._aXY[0];
        this._button["Canvas.Top"] = this._aXY[1];
    
        // Resize elements to fit
        this._absoluteRight = this._aXY[0] + this._button.Width;
        this._absoluteBottom = this._aXY[1] + this._button.Height;
        
        // Hide unused states
        this._stateSelected["Canvas.Left"] = 0;
        this._stateSelected["Canvas.Top"] = 0;
        this._stateSelected.Visibility = "Collapsed";
     
        
    },
    
    // Create control element from XAML
    _buildControl : function()
    {
        // Cusomise XAML to make names unique
        var originalXaml = this._sControlXAML.replace(/Name="/g, "Name=\"" + this._ID);

        // Create element from XAML
        var newElement = this._host.content.createFromXaml(originalXaml);

        // The XML object is added to the root Canvas
        this._parent.children.add(newElement);

        // Set control references
        this._setControlReferences();
        
        // Position and resize objects
        this._autoSize();
        
        this._changeState(this._sState);
    },


    // Find name using original XAML name
    _findNameByXamlID : function(nameInXamlFile)
    {
        return this._parent.findName(this._getIdFor(nameInXamlFile));
    },


    // Get name in original XAML name using instances name
    _getIdFor : function(nameInXamlFile)
    {
        return this._ID + nameInXamlFile;
    },

    // Make references to elements
    _setControlReferences : function()
    {
        // Reference elements
        this._button = this._findNameByXamlID("button");
        this._text = this._findNameByXamlID("buttonText");
        this._back = this._findNameByXamlID("buttonBackNormal");
        this._buttonGrd = this._findNameByXamlID("buttonBackNormal_grd");
 
        
        // State elements     
        this._stateSelected = this._findNameByXamlID("selected");        
        this._stateNormal = this._findNameByXamlID("normal");     
           
        // Set the text to the DataObject name
        this._text.text = this._oDataObject.Name;
        
        
        // Hookup events
        var buttonMouseUp = Silverlight.createDelegate(this, this._oDataObject.onClick);
        this._button.AddEventListener("MouseLeftButtonUp", buttonMouseUp);
        
        this._button.AddEventListener("MouseEnter",
            Silverlight.createDelegate(this, this._buttonMouseEnter)
        );
        
        this._button.AddEventListener("MouseLeave",
            Silverlight.createDelegate(this, this._buttonMouseLeave)
        );
        
    },
    
    
    // Change button state
    _changeState : function(sState)
    {
        // Set update state property
        this._sState = sState;
    
        // Switch between states
        switch (this._sState) {
            case "selected":
                this._stateNormal.Visibility = "Collapsed";
                this._stateSelected.Visibility = "Visible"; 
                // Tell the button list to refresh other buttons states
                this._buttonList._unselectOthers(this._nIndex);
            break;
            
            default:
                this._stateNormal.Visibility = "Visible";
                this._stateSelected.Visibility = "Collapsed";
            break;
        }

    },
    
    //BUTTON OVER HIGHLIGHT
    _onButtonOver: function() 
    {
    this._back.Opacity = 0.7;
    this._buttonGrd.Color = "#FFFEFEFE";
    this._back.StrokeThickness = "3";
    },
    
    //BUTTON OUT HIGHLIGHT
    _onButtonLeave: function() 
   {
    this._back.Opacity = 1;
    this._buttonGrd.Color = "#FF7B8084";
    this._back.StrokeThickness = "0.5";
   },
    
    
    
    
    // EVENT LISTENERS --------------------------------------------------------
    
    
    // Event listener for button clicked
    _buttonClicked : function(sender, eventArgs)
    {
        //console.log("Button clicked: '" + this._oDataObject.Name);
    },
    
    
    // Event listener for button mouse enter
    _buttonMouseEnter : function(sender, eventArgs)
    {
      this._onButtonOver();
        //console.log("Button MouseEnter: '" + this._oDataObject.Name);
     
    },
    
    
    // Event listener for button mouse leave
    _buttonMouseLeave : function(sender, eventArgs)
    {
     this._onButtonLeave();
        //console.log("Button MouseLeave: '" + this._oDataObject.Name);
    }
}