/**
 * Generated by Apache Royale Compiler from org\apache\royale\html\beads\SingleSelectionContainerBead.as
 * org.apache.royale.html.beads.SingleSelectionContainerBead
 *
 * @fileoverview
 *
 * @suppress {checkTypes|accessControls}
 */

goog.provide('org.apache.royale.html.beads.SingleSelectionContainerBead');

goog.require('org.apache.royale.core.IParent');
goog.require('org.apache.royale.core.IParentIUIBase');
goog.require('org.apache.royale.core.ISelectable');
goog.require('org.apache.royale.core.IStrand');
goog.require('org.apache.royale.debugging.assert');
goog.require('org.apache.royale.events.Event');
goog.require('org.apache.royale.events.EventDispatcher');
goog.require('org.apache.royale.events.IEventDispatcher');
goog.require('org.apache.royale.events.ValueEvent');
goog.require('org.apache.royale.utils.array.rangeCheck');
goog.require('org.apache.royale.core.IBead');
goog.require('org.apache.royale.utils.Language');



/**
 * Constructor.
 *
 *  @langversion 3.0
 *  @playerversion Flash 10.2
 *  @playerversion AIR 2.6
 *  @productversion Royale 0.9
 * @constructor
 * @extends {org.apache.royale.events.EventDispatcher}
 * @implements {org.apache.royale.core.IBead}
 */
org.apache.royale.html.beads.SingleSelectionContainerBead = function() {
  org.apache.royale.html.beads.SingleSelectionContainerBead.base(this, 'constructor');
};
goog.inherits(org.apache.royale.html.beads.SingleSelectionContainerBead, org.apache.royale.events.EventDispatcher);


/**
 * Prevent renaming of class. Needed for reflection.
 */
goog.exportSymbol('org.apache.royale.html.beads.SingleSelectionContainerBead', org.apache.royale.html.beads.SingleSelectionContainerBead);


/**
 * @protected
 * @type {org.apache.royale.core.IStrand}
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype._strand;


/**
 * @private
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.org_apache_royale_html_beads_SingleSelectionContainerBead_addListenersToChildren = function() {
  var /** @type {org.apache.royale.core.IParent} */ parent = org.apache.royale.utils.Language.as(this._strand, org.apache.royale.core.IParent);
  var /** @type {number} */ numElements = parent.numElements;
  this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements = [];
  for (var /** @type {number} */ i = 0; i < numElements; i++) {
    var /** @type {org.apache.royale.core.ISelectable} */ elem = org.apache.royale.utils.Language.as(parent.getElementAt(i), org.apache.royale.core.ISelectable);
    if (elem) {
      org.apache.royale.utils.Language.as(elem, org.apache.royale.events.IEventDispatcher).addEventListener("change", org.apache.royale.utils.Language.closure(this.org_apache_royale_html_beads_SingleSelectionContainerBead_handleChange, this, 'handleChange'));
      this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements.push(elem);
    }
  }
  this.org_apache_royale_html_beads_SingleSelectionContainerBead_normalizeSelection();
};


/**
 * @private
 * @type {number}
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex = -1;


/**
 * @private
 * @type {boolean}
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.org_apache_royale_html_beads_SingleSelectionContainerBead__selectionRequired = true;


/**
 * @private
 * @type {Array}
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.org_apache_royale_html_beads_SingleSelectionContainerBead__elements;


/**
 * @private
 * @type {boolean}
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.org_apache_royale_html_beads_SingleSelectionContainerBead__handlingChange;


/**
 * @royaleignorecoercion org.apache.royale.events.IEventDispatcher
 * @private
 * @param {org.apache.royale.events.Event} event
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.org_apache_royale_html_beads_SingleSelectionContainerBead_handleChange = function(event) {
  if (this.org_apache_royale_html_beads_SingleSelectionContainerBead__handlingChange)
    return;
  this.org_apache_royale_html_beads_SingleSelectionContainerBead__handlingChange = true;
  this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex = -1;
  var /** @type {org.apache.royale.core.IParentIUIBase} */ component = org.apache.royale.utils.Language.as(event.target, org.apache.royale.core.IParentIUIBase);
  var /** @type {org.apache.royale.core.ISelectable} */ elem = org.apache.royale.utils.Language.as(component, org.apache.royale.core.ISelectable);
  var /** @type {boolean} */ isSelected = elem.selected;
  var /** @type {org.apache.royale.core.IParent} */ parent = org.apache.royale.utils.Language.as(this._strand, org.apache.royale.core.IParent);
  var /** @type {number} */ numElements = this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements.length;
  for (var /** @type {number} */ i = 0; i < numElements; i++) {
    elem = org.apache.royale.utils.Language.as(this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements[i], org.apache.royale.core.ISelectable);
    if (elem) {
      if (elem == component) {
        if (this.selectionRequired)
          elem.selected = true;
        if (elem.selected)
          this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex = i;
      }
      else if (isSelected)
        elem.selected = false;
    }
  }
  this.org_apache_royale_html_beads_SingleSelectionContainerBead__handlingChange = false;
  this.dispatchEvent(new org.apache.royale.events.Event("change"));
};


/**
 * @royaleignorecoercion org.apache.royale.events.IEventDispatcher
 * @private
 * @param {org.apache.royale.events.ValueEvent} ev
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.org_apache_royale_html_beads_SingleSelectionContainerBead_handleItemAdded = function(ev) {
  if (ev.value) {
    var /** @type {org.apache.royale.core.ISelectable} */ elem = org.apache.royale.utils.Language.as(ev.value, org.apache.royale.core.ISelectable);
    if (elem) {
      elem.addEventListener("change", org.apache.royale.utils.Language.closure(this.org_apache_royale_html_beads_SingleSelectionContainerBead_handleChange, this, 'handleChange'));
      this.org_apache_royale_html_beads_SingleSelectionContainerBead_normalizeElements();
      if (elem.selected)
        this.selectedItem = elem;
      else
        this.org_apache_royale_html_beads_SingleSelectionContainerBead_normalizeSelection();
    }
  }
  else
    this.org_apache_royale_html_beads_SingleSelectionContainerBead_addListenersToChildren();
};


/**
 * @royaleignorecoercion org.apache.royale.events.IEventDispatcher
 * @private
 * @param {org.apache.royale.events.ValueEvent} ev
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.org_apache_royale_html_beads_SingleSelectionContainerBead_handleItemRemoved = function(ev) {
  ev.value.removeEventListener("change", org.apache.royale.utils.Language.closure(this.org_apache_royale_html_beads_SingleSelectionContainerBead_handleChange, this, 'handleChange'));
  this.org_apache_royale_html_beads_SingleSelectionContainerBead_normalizeElements();
  this.org_apache_royale_html_beads_SingleSelectionContainerBead_normalizeSelection();
};


/**
 * @private
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.org_apache_royale_html_beads_SingleSelectionContainerBead_normalizeElements = function() {
  var /** @type {number} */ i = 0;
  this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements = [];
  
  var /** @type {Array} */ internalChildren = this.org_apache_royale_html_beads_SingleSelectionContainerBead_host.internalChildren();
  for (i = 0; i < internalChildren.length; i++) {
    if (org.apache.royale.utils.Language.is(internalChildren[i], org.apache.royale.core.ISelectable))
      this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements.push(internalChildren[i]);
  }
};


/**
 * @private
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.org_apache_royale_html_beads_SingleSelectionContainerBead_normalizeSelection = function() {
  var /** @type {number} */ idx = -1;
  var /** @type {number} */ i = 0;
  var /** @type {org.apache.royale.core.ISelectable} */ elem;
  for (i = 0; i < this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements.length; i++) {
    elem = this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements[i];
    if (idx != -1)
      elem.selected = false; else if (elem.selected) {
      idx = this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex = i;
    }
  }
  if (idx == -1 && this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectionRequired && this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements.length) {
    if (org.apache.royale.utils.array.rangeCheck(this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex, this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements)) {
      elem = this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements[this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex];
      elem.selected = true;
    }
    else
      this.selectedIndex = 0;
  }
};


org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.get__org_apache_royale_html_beads_SingleSelectionContainerBead_host = function() {
  return org.apache.royale.utils.Language.as(this._strand, org.apache.royale.core.IParentIUIBase);
};


org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.set__strand = function(value) {
  this._strand = value;
  this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements = [];
  this.org_apache_royale_html_beads_SingleSelectionContainerBead_host.addEventListener("childrenAdded", org.apache.royale.utils.Language.closure(this.org_apache_royale_html_beads_SingleSelectionContainerBead_handleItemAdded, this, 'handleItemAdded'));
  this.org_apache_royale_html_beads_SingleSelectionContainerBead_host.addEventListener("childrenRemoved", org.apache.royale.utils.Language.closure(this.org_apache_royale_html_beads_SingleSelectionContainerBead_handleItemRemoved, this, 'handleItemRemoved'));
  this.org_apache_royale_html_beads_SingleSelectionContainerBead_addListenersToChildren();
};


org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.get__selectedIndex = function() {
  return org.apache.royale.utils.array.rangeCheck(this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex, this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements) ? this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex : -1;
};


org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.set__selectedIndex = function(value) {
  if (!this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements) {
    this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex = value;
    return;
  }
  org.apache.royale.debugging.assert(!this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectionRequired || org.apache.royale.utils.array.rangeCheck(value, this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements), "When selectionRequired is true, a valid index must be set");
  org.apache.royale.debugging.assert(value < this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements.length, "index is out of range");
  if (value < 0) {
    this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex = -1;
    this.org_apache_royale_html_beads_SingleSelectionContainerBead_normalizeSelection();
  } else if (value != this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex) {
    var /** @type {org.apache.royale.core.ISelectable} */ elem = this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements[value];
    elem.selected = true;
  }
};


org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.get__selectedItem = function() {
  return org.apache.royale.utils.array.rangeCheck(this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex, this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements) ? this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements[this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex] : null;
};


org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.set__selectedItem = function(value) {
  var /** @type {number} */ idx = this.org_apache_royale_html_beads_SingleSelectionContainerBead__elements.indexOf(value);
  org.apache.royale.debugging.assert(idx != -1, "The specified element is not valid");
  if (idx != this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectedIndex)
    value.selected = true;
};


org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.get__selectionRequired = function() {
  return this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectionRequired;
};


org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.set__selectionRequired = function(value) {
  this.org_apache_royale_html_beads_SingleSelectionContainerBead__selectionRequired = value;
};


Object.defineProperties(org.apache.royale.html.beads.SingleSelectionContainerBead.prototype, /** @lends {org.apache.royale.html.beads.SingleSelectionContainerBead.prototype} */ {
/**
  * @export
  * @type {org.apache.royale.core.IParentIUIBase} */
org_apache_royale_html_beads_SingleSelectionContainerBead_host: {
get: org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.get__org_apache_royale_html_beads_SingleSelectionContainerBead_host},
/**
  * @export
  * @type {org.apache.royale.core.IStrand} */
strand: {
set: org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.set__strand},
/**
  * @export
  * @type {number} */
selectedIndex: {
get: org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.get__selectedIndex,
set: org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.set__selectedIndex},
/**
  * @export
  * @type {org.apache.royale.core.ISelectable} */
selectedItem: {
get: org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.get__selectedItem,
set: org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.set__selectedItem},
/**
  * @export
  * @type {boolean} */
selectionRequired: {
get: org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.get__selectionRequired,
set: org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.set__selectionRequired}}
);


/**
 * Metadata
 *
 * @type {Object.<string, Array.<Object>>}
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.ROYALE_CLASS_INFO = { names: [{ name: 'SingleSelectionContainerBead', qName: 'org.apache.royale.html.beads.SingleSelectionContainerBead', kind: 'class' }], interfaces: [org.apache.royale.core.IBead] };



/**
 * Reflection
 *
 * @return {Object.<string, Function>}
 */
org.apache.royale.html.beads.SingleSelectionContainerBead.prototype.ROYALE_REFLECTION_INFO = function () {
  return {
    variables: function () {return {};},
    accessors: function () {
      return {
        'strand': { type: 'org.apache.royale.core.IStrand', access: 'writeonly', declaredBy: 'org.apache.royale.html.beads.SingleSelectionContainerBead'},
        'selectedIndex': { type: 'int', access: 'readwrite', declaredBy: 'org.apache.royale.html.beads.SingleSelectionContainerBead', metadata: function () { return [ { name: 'Bindable', args: [ { key: '', value: 'change' } ] } ]; }},
        'selectedItem': { type: 'org.apache.royale.core.ISelectable', access: 'readwrite', declaredBy: 'org.apache.royale.html.beads.SingleSelectionContainerBead', metadata: function () { return [ { name: 'Bindable', args: [ { key: '', value: 'change' } ] } ]; }},
        'selectionRequired': { type: 'Boolean', access: 'readwrite', declaredBy: 'org.apache.royale.html.beads.SingleSelectionContainerBead'}
      };
    },
    methods: function () {
      return {
        'SingleSelectionContainerBead': { type: '', declaredBy: 'org.apache.royale.html.beads.SingleSelectionContainerBead'}
      };
    },
    metadata: function () { return [  ]; }
  };
};