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

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

goog.require('org.apache.royale.core.IBorderPaddingMarginValuesImpl');
goog.require('org.apache.royale.core.IStrand');
goog.require('org.apache.royale.core.LayoutBase');
goog.require('org.apache.royale.core.UIBase');
goog.require('org.apache.royale.core.ValuesManager');
goog.require('org.apache.royale.core.layout.EdgeData');
goog.require('org.apache.royale.html.beads.models.DividedContainerModel');
goog.require('org.apache.royale.html.supportClasses.DividedContainerDivider');
goog.require('org.apache.royale.core.IBeadLayout');
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.core.LayoutBase}
 * @implements {org.apache.royale.core.IBeadLayout}
 */
org.apache.royale.html.beads.layouts.HDividedContainerLayout = function() {
  org.apache.royale.html.beads.layouts.HDividedContainerLayout.base(this, 'constructor');
};
goog.inherits(org.apache.royale.html.beads.layouts.HDividedContainerLayout, org.apache.royale.core.LayoutBase);


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


/**
 * @private
 * @type {org.apache.royale.core.IStrand}
 */
org.apache.royale.html.beads.layouts.HDividedContainerLayout.prototype.org_apache_royale_html_beads_layouts_HDividedContainerLayout__strand;


/**
 * @asprivate
 * @royaleignorecoercion org.apache.royale.core.IBorderPaddingMarginValuesImpl
 * @export
 * @override
 */
org.apache.royale.html.beads.layouts.HDividedContainerLayout.prototype.layout = function() {
  var /** @type {org.apache.royale.core.UIBase} */ host = org.apache.royale.utils.Language.as(this.org_apache_royale_html_beads_layouts_HDividedContainerLayout__strand, org.apache.royale.core.UIBase);
  var /** @type {number} */ n = host.numElements;
  if (n == 0)
    return false;
  host.element.style['position'] = 'absolute';
  var /** @type {number} */ useWidth = host.width;
  var /** @type {number} */ useHeight = host.height;
  var /** @type {org.apache.royale.core.layout.EdgeData} */ paddingMetrics = org.apache.royale.core.ValuesManager["valuesImpl"].getPaddingMetrics(host);
  var /** @type {org.apache.royale.core.layout.EdgeData} */ borderMetrics = org.apache.royale.core.ValuesManager["valuesImpl"].getBorderMetrics(host);
  useWidth -= paddingMetrics.left + paddingMetrics.right + borderMetrics.left + borderMetrics.right;
  useHeight -= paddingMetrics.top + paddingMetrics.bottom + borderMetrics.top + borderMetrics.bottom;
  var /** @type {number} */ remainingWidth = useWidth;
  var /** @type {number} */ remainingCount = 0;
  var /** @type {Array} */ actualChildren = [];
  var /** @type {Array} */ separators = [];
  for (var /** @type {number} */ i = 0; i < n; i++) {
    var /** @type {org.apache.royale.core.UIBase} */ child = org.apache.royale.utils.Language.as(host.getElementAt(i), org.apache.royale.core.UIBase);
    if (org.apache.royale.utils.Language.is(child, org.apache.royale.html.supportClasses.DividedContainerDivider)) {
      separators.push(child);
    } else {
      actualChildren.push(child);
      if (!isNaN(child.explicitWidth)) {
        remainingWidth -= child.explicitWidth;
      } else {
        remainingCount++;
      }
    }
  }
  var /** @type {number} */ numSeparators = separators.length;
  remainingWidth -= numSeparators * 10;
  var /** @type {number} */ childInitialWidth = remainingWidth;
  if (remainingCount > 0) {
    childInitialWidth = remainingWidth / remainingCount;
  }
  var /** @type {number} */ xpos = borderMetrics.left + paddingMetrics.left;
  var /** @type {number} */ ypos = borderMetrics.top + paddingMetrics.top;
  var /** @type {number} */ j = 0;
  var /** @type {Array} */ adjustments = org.apache.royale.utils.Language.as(host.model, org.apache.royale.html.beads.models.DividedContainerModel).pairAdjustments;
  var /** @type {Array} */ childWidths = [];
  for (i = 0; i < actualChildren.length; i++) {
    child = org.apache.royale.utils.Language.as(actualChildren[i], org.apache.royale.core.UIBase);
    var /** @type {number} */ childWidth = childInitialWidth;
    var /** @type {number} */ childHeight = useHeight;
    if (!isNaN(child.percentWidth)) {
      childWidth = (child.percentWidth / 100.0) * remainingWidth;
    } else if (!isNaN(child.explicitWidth)) {
      childWidth = child.explicitWidth;
    }
    childWidths.push(childWidth);
  }
  for (j = 0, i = 0; j < adjustments.length; j++, i++) {
    childWidths[i] += adjustments[j];
    childWidths[i + 1] -= adjustments[j];
  }
  for (i = 0, j = 0; i < actualChildren.length; i++) {
    child = org.apache.royale.utils.Language.as(actualChildren[i], org.apache.royale.core.UIBase);
    child.x = xpos;
    child.y = ypos;
    child.setWidth(childWidths[i]);
    child.setHeight(useHeight);
    child.element.style['position'] = 'absolute';
    if (j < separators.length) {
      var /** @type {org.apache.royale.core.UIBase} */ sep = org.apache.royale.utils.Language.as(separators[j], org.apache.royale.core.UIBase);
      sep.width = 10;
      sep.height = useHeight;
      sep.x = xpos + child.width;
      sep.y = ypos;
      sep.element.style['position'] = 'absolute';
      j += 1;
    }
    xpos += child.width + 10;
  }
  return true;
};


org.apache.royale.html.beads.layouts.HDividedContainerLayout.prototype.set__strand = function(value) {
  this.org_apache_royale_html_beads_layouts_HDividedContainerLayout__strand = value;
  org.apache.royale.html.beads.layouts.HDividedContainerLayout.superClass_.set__strand.apply(this, [ value] );
};


Object.defineProperties(org.apache.royale.html.beads.layouts.HDividedContainerLayout.prototype, /** @lends {org.apache.royale.html.beads.layouts.HDividedContainerLayout.prototype} */ {
/**
  * @export
  * @type {org.apache.royale.core.IStrand} */
strand: {
set: org.apache.royale.html.beads.layouts.HDividedContainerLayout.prototype.set__strand}}
);


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



/**
 * Reflection
 *
 * @return {Object.<string, Function>}
 */
org.apache.royale.html.beads.layouts.HDividedContainerLayout.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.layouts.HDividedContainerLayout'}
      };
    },
    methods: function () {
      return {
        'HDividedContainerLayout': { type: '', declaredBy: 'org.apache.royale.html.beads.layouts.HDividedContainerLayout'},
        'layout': { type: 'Boolean', declaredBy: 'org.apache.royale.html.beads.layouts.HDividedContainerLayout'}
      };
    }
  };
};