/*
 Copyright (c) 2006 www.ericfeminella.com. All rights reserved.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is furnished
 to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all
 copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
 INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

 @internal
 */

package com.ericfeminella.display
{
    import flash.display.DisplayObjectContainer;
    import flash.display.DisplayObject;
    import mx.core.Container;

    /**
     *
     * All static class which provides an API in which child objects
     * of a particular DisplayObjectContainer can be reparented to a
     * new DisplayObjectContainer within the application.
     *
     */
    public final class DisplayObjectReparenter
    {
        /**
         *
         * Reparents a child DisplayObject to a new container and retains the
         * current state of the DisplayObject once relocated
         *
         * @param   Specifies the DisplayObject which is to be reparented
         * @param   Specifies the current container of the DisplayObject
         * @param   Specifies the new DisplayObjectContainer to add the
         *          DisplayObject
         * @throws  An exception specifying that the DisplayObject is
         *          not a child of the container
         * @throws  An exception specifying that the DisplayObject could
         *          not be reparented
         *
         */
        public static function reparentChild(child:DisplayObject, parent:DisplayObjectContainer, newParent:DisplayObjectContainer) : void
        {
            if (parent.contains(child))
            {
                try {
                    parent.removeChild( child );
                    newParent.addChild( child );
                }
                catch(err:Error)
                {
                    throw new Error("Unable to Reparent: DisplayObject instance is not a child of specified container");
                }
            }
            else
            {
                throw new Error("Unable to Reparent: DisplayObject instance is not a child of specified container");
            }
        }

        /**
         *
         * Reparents an array of DisplayObjects to a new container and retains the
         * current state of the child DisplayObjects once reparented
         *
         * @param   Specifies an Array of type DisplayObject in which to reparent
         * @param   Specifies the current container of the Array of DisplayObjects
         * @param   Specifies the new DisplayObjectContainer to add the children
         * @throws  An exception specifying that an item in the array is not of
         *          type DisplayObject
         * @throws  An exception specifying that a DisplayObject could not be
         *          reparented
         *
         */
        public static function reparentChildren(displayObjectArray:Array, parent:DisplayObjectContainer, newParent:DisplayObjectContaine) : void
        {
            var n:int = displayObjectArray.length;

            for (var i:int = 0; i < n; i++) {

                if ( displayObjectArray[i] is DisplayObject )
                {
                    reparentChild( displayObjectArray[i], parent, newParent );
                }
                else
                {
                    throw new Error("Unable to reparent: object must be of type DisplayObject");
                }
            }
        }
    }
}