         }
 
         if ((_layeringFlags & LAYERING_ENABLED) || 
-        	(item is IVisualElement && (item as IVisualElement).layer != 0))
+        	(child is IVisualElement && (child as IVisualElement).layer != 0))
         	invalidateLayering();
             
         return super.addChildAt(child, index != -1 ? index : super.numChildren);

    mx_internal function itemAdded(item:Object, index:int):void
    {
        var myItemRenderer:IVisualElement = createRendererForItem(item);
        
        // Set the renderer's data to the item, but only if the item and renderer are different
        if (myItemRenderer is IDataRenderer && myItemRenderer != item)
            IDataRenderer(myItemRenderer).data = item;
        
        registerRenderer(index, myItemRenderer);
        
        addItemToDisplayList(myItemRenderer as DisplayObject, item, index);
        
        dispatchEvent(new ItemExistenceChangedEvent(
                      ItemExistenceChangedEvent.ITEM_ADD, false, false, 
                      item, index, myItemRenderer));

    {
        return dataProvider ? dataProvider.length : 0;
    }

    private var virtualLayoutOffset:int = 0;
    private var virtualLayoutUnderway:Boolean = false;
     
    /**
     *  @private
     *  Update virtualLayoutOffset and clear the virtualLayoutUnderway flag.
     */
    override public function beginVirtualLayout(startIndex:int):void
    {
        virtualLayoutOffset = startIndex;
        virtualLayoutUnderway = true;
    }
    
    /**
     *  @private
     *  Discard the ItemRenderers that aren't needed anymore, i.e. the ones
     *  outside the logical range startIndex to endIndex.
     * 
     *  Clear the virtualLayoutUnderway flag.
     */
    override public function endVirtualLayout(startIndex:int, endIndex:int):void
    {
        // At this point, insertion of new IRs has pushed all of the unneeded IRs
        // past endIndex-startIndex.  Remove them without invalidating this DataGroup's
        // size or display list.
        for(var i:int = super.numChildren - 1; i > (endIndex - startIndex); i--)
        {
            var itemRenderer:DisplayObject = super.getChildAt(i);
            unregisterRenderer(i, IVisualElement(itemRenderer));
            super.removeChildAt(i);
        }        
        virtualLayoutUnderway = false;
    }
    
    /**
     *  @private
     */
    override public function invalidateSize():void
    {
        if (!virtualLayoutUnderway)
            super.invalidateSize();
    }
    
    /**
     *  @private
     */
    override public function getLayoutElementAt(index:int):ILayoutElement
    {
        var itemRendererIndex:int = index - virtualLayoutOffset;
        var itemRenderer:Object = mx_internal::getRendererForItemAt(itemRendererIndex);

        if (layout && layout.virtualLayout)
        {
            // Note: the code below does most of the same work as itemAdded()
            var item:Object = dataProvider.getItemAt(index);
            var createdIR:Boolean = false;
            
            if (!itemRenderer)
            {
                itemRenderer = createRendererForItem(item);
                createdIR = true;
            }

            if (itemRenderer.parent != this)
                addItemRendererToDisplayList(DisplayObject(itemRenderer), itemRendererIndex);
            else
                super.setChildIndex(DisplayObject(itemRenderer), itemRendererIndex);
            
            if (createdIR)
            {
                if (itemRenderer is IInvalidating)
                    IInvalidating(itemRenderer).validateNow();
                // TBD sync with itemAdded
                dispatchEvent(new ItemExistenceChangedEvent(ItemExistenceChangedEvent.ITEM_ADD, false, false, item));
            }
        }

        return LayoutElementFactory.getLayoutElementFor(itemRenderer);
    }

    /**
     *  @private
     */
