<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
         xmlns:s="library://ns.adobe.com/flex/spark" 
         xmlns:mx="library://ns.adobe.com/flex/mx"
         width="100%" height="100%" xmlns:pages="com.afi.estimator.views.pages.*" creationComplete="init()">
    
    <fx:Declarations>
        <s:Move id="currentMoveEffect" effectEnd="currentEffectEnd(event)">
        </s:Move>
        <s:Move id="nextMoveEffect" effectEnd="nextEffectEnd()">
        </s:Move>
    </fx:Declarations>
    
    <fx:Script>
        <![CDATA[
            import com.afi.estimator.log.Logger;
            import com.afi.estimator.model.PagesModel;
            import com.afi.estimator.views.View;
            import com.afi.estimator.views.pages.*;
            import com.afi.estimator.views.pages.WelcomePage;
            import com.afi.estimator.views.shell.background;
            import com.afi.estimator.views.ui.TitleForm;
            import com.afi.estimator.views.vo.PageVO;
            
            import mx.containers.Form;
            import mx.core.IVisualElement;
            import mx.effects.Parallel;
            import mx.events.EffectEvent;
            import mx.events.FlexEvent;
            
            import spark.effects.Animate;
            import spark.effects.Move;
            import spark.effects.Scale;
            import spark.effects.animation.MotionPath;
            import spark.effects.animation.SimpleMotionPath;  
            

            
            public var current_page:TitleForm;
            
            private var current_container:FormContainer;
            
            private var next_container:FormContainer;
            
            private var tweening:Boolean = false;
            
            private var nextMoveComplete:Function;
            
            private var current_page_index:uint = 0;
            
            private var callbacks:Array = [];
               
            private var xPosition:Number =0;
           
            public static var instance:FormScroller;
            
            /**
            * Debug Settings
            */
            
            private var default_page:PageVO;
            
            
            private function init():void
            {
                FormScroller.instance = this;
                
                var welcome:WelcomePage = new WelcomePage();
                
                var container:FormContainer = new FormContainer();
                
                container.addElement(welcome);
                
                addElement(container);
                
                current_container = container;
            }
            
            private function runCallbacks():void {
                var toDelete:Array = [];
                var i:int = 0;
                
                for each (var c:Object in callbacks) {
                    c.fn.apply(c.caller);
                    if (c.expire) toDelete.push(i);
                    i++;
                }
                
                // Remove expired callbacks
                for each (var index:int in toDelete) {
                    callbacks.splice(index,index);
                }
            }
            
            public function addCallback(fn:Function, caller:*, args:Object = null):int {
                var expire:Boolean = args.expire || false;
                
                callbacks.push({
                    fn: fn,
                    expire: expire,
                    caller: caller
                });
                
                return callbacks.length - 1;
            }
            
            public function scrollToForm(dir:int, 
                                         page:PageVO= null,
                                         complete:Function = null):void
            {
                if(!tweening) {
                    runCallbacks();
                    //nextMoveComplete = complete;
                    
                    var newFormStartX:int = (dir == PagesModel.SCROLL_RIGHT) ? rightTarg : leftTarg;
                    
                    var currentFormTargX:int = (dir == PagesModel.SCROLL_RIGHT) ? leftTarg : rightTarg;
                    
                    if(page == null) {
                        current_page_index = current_page_index + dir;
                        page = PagesModel.pages[current_page_index];
                    } else {
                        current_page_index = PagesModel.getIndexByVO(page);
                    }
                    
                    View.instance.footer.currentState = page.section;
                    
                    var klass:Class = page.klass;
                    var instance:Object = new klass();
                    var view:IVisualElement = instance as IVisualElement;
                    current_page = instance as TitleForm;
                    var container:FormContainer = new FormContainer();
                    container.addElement(view);
                    container.x = newFormStartX;
                    container.addEventListener(FlexEvent.CREATION_COMPLETE, function(e:FlexEvent):void {
                       var currentForm:FormContainer = current_container;
                       
                       /*var view:View = new View();
                       var back:background = view.getChildByName("backGround") as background;
                       moveForm(currentMoveEffect, back, newFormStartX,0)*/
                       
                       moveForm(currentMoveEffect, currentForm, 0, currentFormTargX);
                       moveForm(nextMoveEffect, container, newFormStartX, 0);
                       current_container = container;
                    });
                    addElement(container);
                    next_container = container;
                    tweening = true;
                }
            }
            
            private function moveForm(m:Move, targ:Object, from:Number, to:Number):void {
                m.stop();
                m.xFrom = from;
                m.xTo = to;
                //m.duration = duration;
                m.play([targ]);
            }
            
            public function gotoPage(s:String):void
            {
                switch(s) {
                    case 'calculate':
                      scrollToForm(1, PagesModel.pages[4]);
                    break;
                    case 'thanks':
                        scrollToForm(1, PagesModel.pages[9]);
                    break;
                    case 'results':
                        scrollToForm(-1, PagesModel.pages[5]);
                    break;
                }
            }
            
            private function slideOut(s:FormContainer, prev:Boolean):void
            {
               
            }
            
            private function removePreviousPage(prev:Boolean):void
            {
               removeElement(current_container);
            }
            
            private function removeContainer(s:FormContainer):void
            {
                //removeElement(s);
            }
            
            public function next():void
            {
                gotoNext();
            }
            
            private function currentEffectEnd(e:EffectEvent):void
            {
                removeElement(e.effectInstance.target as IVisualElement);
            }
            
            private function nextEffectEnd():void
            {
                if(nextMoveComplete != null) nextMoveComplete();
                tweening = false;
            }
            
            
            public function previous():void
            {
                gotoPrevious();
            }
            
            private function get rightTarg():int {
                return (root.width + 10);
            }
            
            private function get leftTarg():int {
                return -root.width - 10;
            }
            
            private function gotoNext():void
            {
                if(current_page_index != PagesModel.pages.length-1) {
                    moveBackGroundLeft();
                    scrollToForm(PagesModel.SCROLL_RIGHT);
                }
            }
            
            private function gotoPrevious():void
            {
                if(current_page_index != 0) {
                    moveBackGroundRight();
                    scrollToForm(PagesModel.SCROLL_LEFT);
                }
            }
            
            private function moveBackGroundLeft():void{
                var view:View = this.parentApplication.getElementAt(0) as View;
                view.backgrnd.backGroundMoveEffect.stop();
                view.backgrnd.backGroundMoveEffect.xFrom = xPosition;
                xPosition = xPosition - 200;
                view.backgrnd.backGroundMoveEffect.xTo = xPosition;
                view.backgrnd.backGroundMoveEffect.play([view.backgrnd.inner]);
            }
            
            private function moveBackGroundRight():void{
                var view:View = this.parentApplication.getElementAt(0) as View;
                view.backgrnd.backGroundMoveEffect.stop();
                view.backgrnd.backGroundMoveEffect.xFrom = xPosition;
                xPosition = xPosition + 200;
                view.backgrnd.backGroundMoveEffect.xTo = xPosition;
                view.backgrnd.backGroundMoveEffect.play([view.backgrnd.inner]);
            }
            
            ]]>
    </fx:Script>
    
    
    
    
</s:Group>