 //
 ////////////////////////////////////////////////////////////////////////////////
 
-package spark.effects
+package spark.transitions
 {
-import flash.display.BitmapData;
-import flash.display.DisplayObject;
-
-import mx.core.IVisualElementContainer;
-import mx.core.UIComponent;
+    
 import mx.core.mx_internal;
 import mx.effects.IEffect;
-import mx.effects.Parallel;
-import mx.events.EffectEvent;
 
 import spark.components.ActionBar;
 import spark.components.Group;
-import spark.components.Image;
 import spark.components.TabbedViewNavigator;
 import spark.components.ViewNavigator;
 import spark.components.supportClasses.ButtonBarBase;
 import spark.components.supportClasses.ViewNavigatorBase;
+import spark.effects.Animate;
 import spark.effects.animation.MotionPath;
 import spark.effects.animation.SimpleMotionPath;
-import spark.effects.easing.Sine;
 
 use namespace mx_internal;
-
-/**
- * 
- */
-public class SlideViewTransition extends ViewTransition
+    
+public class SlideViewTransition extends ViewTransitionBase
 {
-    //--------------------------------------------------------------------------
-    //
-    //  Constants
-    //
-    //--------------------------------------------------------------------------
-    
-    /**
-     *  Slide the views to the left.
-     *  
-     *  @langversion 3.0
-     *  @playerversion Flash 10.1
-     *  @playerversion AIR 2.5
-     *  @productversion Flex 4.5
-     */
-    public static const SLIDE_LEFT:String = "left";
-
-    /**
-     *  Slide the views to the right.
-     *  
-     *  @langversion 3.0
-     *  @playerversion Flash 10.1
-     *  @playerversion AIR 2.5
-     *  @productversion Flex 4.5
-     */
-    public static const SLIDE_RIGHT:String = "right";
-    
     //--------------------------------------------------------------------------
     //
     //  Constructor

    //--------------------------------------------------------------------------
    
    /**
     *  
     * 
     *  @langversion 3.0
     *  @playerversion Flash 10

     */
    override public function captureStartValues():void
    {
        super.captureStartValues();
        
        var oldVisibility:Boolean = endView.visible;
        endView.visible = false;
        cachedNavigator = getSnapshot(targetNavigator);
        endView.visible = oldVisibility;
    }
    
    /**
     *  @inheritDoc
     * 
     *  @langversion 3.0
     *  @playerversion Flash 10
     *  @playerversion AIR 2.5
     *  @productversion Flex 4.5
     */
    override protected function createViewEffect():IEffect
    {	
		// Prepare our start and end views by positioning them prior to 
		// the start of our transition, ensuring that they are cached as 
		// surfaces, and adjust z-order if necessary.
		
		var slideTargets:Array = new Array();
		
        if (startView)
        {
            currentViewProps = { includeInLayout:startView.includeInLayout,
                cacheAsBitmap:startView.cacheAsBitmap };
            
            startView.includeInLayout = false;
            startView.cacheAsBitmap = true;
            
            if (!(mode == SlideViewTransitionMode.COVER))
                slideTargets.push(startView);
        }
        
        if (endView)
        {
            nextViewProps = { includeInLayout:endView.includeInLayout,
                cacheAsBitmap:endView.cacheAsBitmap };
            
            endView.includeInLayout = false;
            endView.cacheAsBitmap = true;
            
            if (!(mode == SlideViewTransitionMode.UNCOVER))
                slideTargets.push(endView);
            
            if (mode == SlideViewTransitionMode.UNCOVER)
				setComponentChildIndex(endView, navigator, 0);  
        }
        
        var slideDistance:Number;
        var slideOffset:Number = 0;
        var animatedProperty:String;
        var verticalTransition:Boolean;
        
        // Predetermine slide direction and distance.
        switch (direction)
        {                       
            case ViewTransitionDirection.DOWN:
                animatedProperty = "y";
                slideDistance = navigator.height;
                slideOffset = -navigator.contentGroup[animatedProperty];
                verticalTransition = true;
                break;
            
            case ViewTransitionDirection.UP:
                animatedProperty = "y";
                slideDistance = -navigator.height;
                slideOffset = navigator.contentGroup[animatedProperty];
                verticalTransition = true;
                break;
            
            case ViewTransitionDirection.RIGHT:
                animatedProperty = "x";
                slideDistance = navigator.width;
                break;
            
            case ViewTransitionDirection.LEFT:
            default:
                animatedProperty = "x";
                slideDistance = -navigator.width;
                break;
        }
        
		// Position the end view prior to start of transition.
        if (!(mode == SlideViewTransitionMode.UNCOVER))
            endView[animatedProperty] = -slideDistance - slideOffset;
        
		// Construction animation sequence.
        var animation:Animate = new Animate();
        var vector:Vector.<MotionPath> = new Vector.<MotionPath>();
        vector.push(new SimpleMotionPath(animatedProperty, null, null, slideDistance + slideOffset));
        animation.motionPaths = vector;
        animation.easer = easer;
        animation.targets = slideTargets;
        animation.duration = duration;
        
        return animation;
    }
        
    /**
     *  @inheritDoc
     * 
     *  @langversion 3.0
     *  @playerversion Flash 10
     *  @playerversion AIR 2.5
     *  @productversion Flex 4.5
     */
    override protected function createConsolidatedEffect():IEffect
    {        
		// Prepare our start and end view elements by positioning them prior to 
		// the start of our transition, ensuring that they are cached as 
		// surfaces, and adjust z-order if necessary.
		
		var slideTargets:Array = new Array();
		
        if (!(mode == SlideViewTransitionMode.COVER))
            slideTargets.push(cachedNavigator);
        
        if (!(mode == SlideViewTransitionMode.UNCOVER))
            slideTargets.push(targetNavigator.contentGroup);

        targetNavigator.contentGroup.includeInLayout = false;
        
        transitionGroup = new Group();
        transitionGroup.includeInLayout=false;
        
		// Ensure the views are in the right stacking order based on our
		// transition mode (cover vs. uncover for instance).
        if (mode == SlideViewTransitionMode.COVER)
		{
			var childIndex:uint = targetNavigator.skin.getChildIndex(targetNavigator.contentGroup);
		    addComponentToContainerAt(transitionGroup, targetNavigator.skin, childIndex);
		}
        else
			addComponentToContainer(transitionGroup, targetNavigator.skin);
        
        if (targetNavigator is TabbedViewNavigator)
        {
            var tabBar:ButtonBarBase = TabbedViewNavigator(targetNavigator).tabBar;
            
            if (tabBar)
            {
                if (!(mode == SlideViewTransitionMode.UNCOVER))
                    slideTargets.push(tabBar);
                tabBar.includeInLayout = false;
            }
        }
        else if (targetNavigator is ViewNavigator)
        {
            if (actionBar)
            {
                if (!(mode == SlideViewTransitionMode.UNCOVER))
                    slideTargets.push(actionBar);
                actionBar.includeInLayout = false;
            }
        }
        
        if (cachedNavigator)
        {
            cachedNavigator.x = cachedNavigator.y = 0;
            cachedNavigator.includeInLayout = false;
            transitionGroup.addElement(cachedNavigator);
        }
        
        var slideDistance:Number;
        var animatedProperty:String;
        var verticalTransition:Boolean;
        
		// Predetermine slide direction and distance.
        switch (direction)
        {           
            case ViewTransitionDirection.RIGHT:
                animatedProperty = "x";
                slideDistance = targetNavigator.width;
                break;
            
            case ViewTransitionDirection.DOWN:
                animatedProperty = "y";
                slideDistance = targetNavigator.height;
                verticalTransition = true;
                break;
            
            case ViewTransitionDirection.UP:
                animatedProperty = "y";
                slideDistance = -targetNavigator.height;
                verticalTransition = true;
                break;
            
            case ViewTransitionDirection.LEFT:
            default:
                animatedProperty = "x";
                slideDistance = -targetNavigator.width;
                break;
        }
         
		// Position the control bars prior to our transition.
		
        if (targetNavigator == parentNavigator)
        {
            if (targetNavigator is TabbedViewNavigator)
            {
                tabBar = TabbedViewNavigator(targetNavigator).tabBar;
                
                if (tabBar && !(mode == SlideViewTransitionMode.UNCOVER))
                    tabBar[animatedProperty] = -slideDistance;
            }
        }
        else
        {
            if (targetNavigator is ViewNavigator && actionBar && (!(mode == SlideViewTransitionMode.UNCOVER)))
                actionBar[animatedProperty] = -slideDistance;
        }
        
        if (!(mode == SlideViewTransitionMode.UNCOVER))
           targetNavigator.contentGroup[animatedProperty] = -slideDistance + targetNavigator.contentGroup[animatedProperty];
                
		// Validate to ensure our snapshots are rendered.
        transitionGroup.validateNow();
        
		// Construction animation sequence.
        var animation:Animate = new Animate();
        var vector:Vector.<MotionPath> = new Vector.<MotionPath>();
        vector.push(new SimpleMotionPath(animatedProperty, null, null, slideDistance));
        animation.motionPaths = vector;
        animation.easer = easer;
        animation.targets = slideTargets;
        animation.duration = duration;
        
        return animation;
    }
    
    /**
     *  @inheritDoc
     * 
     *  @langversion 3.0
     *  @playerversion Flash 10
     *  @playerversion AIR 2.5
     *  @productversion Flex 4.5
     */
    override protected function cleanUp():void
    {
        if (transitionGroup)
			removeComponentFromContainer(transitionGroup, targetNavigator.skin);
        
        transitionGroup = null;
        cachedNavigator = null;
        
        super.cleanUp();
    }
    
    /**
     *  @inheritDoc
     * 
     *  @langversion 3.0
     *  @playerversion Flash 10
     *  @playerversion AIR 2.5
     *  @productversion Flex 4.5
     */
    override public function prepareForPlay():void
    {
        actionBarTransitionDirection = direction;
        super.prepareForPlay();
    }
    
}
}
