﻿// Button manager object
ButtonList = function(slRootObject, sCollectionName, sButtonXAML, eParent, sDirection)
{
    // Make object root scope accessible to inner objects
    var that = this;
    
    // Make arguments and derived varibles public
    this._slRootObject = slRootObject;
    this._eParent = eParent;
    this._sCollectionName = sCollectionName;
    this._sButtonXAML = sButtonXAML;
    this._aButtons = [];
    this._sDirection = sDirection;
    
    // Set default options
    this._buttonsLength = 0;
    this._nMargin = 2;
}


// Add a button to the list
ButtonList.prototype.add = function(oDataObject)
{
    
    // Set default position co-ordinates
    var nNextLeft = 0;
    var nNextTop = 0;
    
    // Retrieve button array length
    this._buttonsLength = this._aButtons.length;
    
    // If the previous button exists, extract it's absolute width or height for placement
    if (typeof(this._aButtons[this._buttonsLength-1]) != "undefined") {
        if (this._sDirection == "horisontal") { 
            nNextLeft = this._aButtons[this._buttonsLength-1]._absoluteRight + this._nMargin;
        } else {
            nNextTop = this._aButtons[this._buttonsLength-1]._absoluteBottom + this._nMargin;
        }
    }
    
    if (typeof(oDataObject.DefaultState) == "undefined") {
        oDataObject.DefaultState = "normal";
    }
    
    // Push the new button to the button array
    this._aButtons.push(
        new Button(
            this,                       // Button list scope
            this._buttonsLength,        // Index for new button
            oDataObject,                // Data object
            [nNextLeft, nNextTop],      // Co-ordinates
            
            oDataObject.DefaultState    // Default button state
        )
    );
}


// On selection of a button, unselect others
ButtonList.prototype._unselectOthers = function(nSelectedIndex)
{
    // Loop through buttons in array
    for (var i=0; i<=this._buttonsLength; i++) {
        
        // If the button isn't selected
        if (i!=nSelectedIndex) {
        
            // Set the button state to 'normal'
            this._aButtons[i]._changeState("normal");
        }
    }
}