package
{
    import com.flashdynamix.motion.Tweensy;
    
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.MouseEvent;
    
    import org.tinytlf.components.TextField;
    
    [SWF("1000", height="1500", frameRate="60")]
    public class ResizedDualImageFlow extends Sprite
    {
        [Embed("assets/fonts/calibri.ttf", fontFamily="Calibri")]
        private var calibri:Class;
        [Embed("assets/fonts/calibrib.ttf", fontFamily="Calibri", fontWeight="bold")]
        private var calibriBold:Class;
        [Embed("assets/fonts/calibrii.ttf", fontFamily="Calibri", fontStyle="italic")]
        private var calibriItalic:Class;
        [Embed("assets/fonts/calibriz.ttf", fontFamily="Calibri", fontWeight="bold", fontStyle="italic")]
        private var calibriBoldItalic:Class;
        
        [Embed("assets/wikipedia.css", mimeType="application/octet-stream")]
        private const cssSource:Class;
        
        [Embed("assets/crimson_rose_dual_flow.xml", mimeType="application/octet-stream")]
        private const xmlSource:Class;
        
        private var tf:TextField;
        
        public function ResizedDualImageFlow()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            tf = new TextField();
            tf.width = 1000;
            tf.height = 600;
            tf.style = new cssSource().toString();
            tf.text = new xmlSource().toString();
            
            tf.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            
            addChild(tf);
            
            SWFProfiler.init(this);
        }
        
        private var startWidth:Number = 1000;
        private var startX:Number = 1000;
        private var deltaX:Number = 1000;
        
        private function onMouseDown(event:MouseEvent):void
        {
            startWidth = tf.width;
            startX = event.stageX;
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
        }
        
        private function onMouseUp(event:MouseEvent):void
        {
            startWidth = 1000;
            startX = 1000;
            stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
        }
        
        private function onMouseMove(event:MouseEvent):void
        {
            if(Math.abs(deltaX - event.stageX) < 30)
                return;
            
            tf.width = startWidth - (startX - event.stageX);
            deltaX = event.stageX;
        }
        
        private function onClick(event:Event):void
        {
            tf.width < 1000 ? back() : forth();
            
            return;
            
            tf.width += inc;
            
            if(tf.width < 600)
                inc = 50;
            else if(tf.width > 1000)
                inc = -50;
        }
        
        private var inc:Number = 50;
        
        private function forth(...args):void
        {
            if(tf.width != 1000)
                return;
            
            Tweensy.to(tf, {width:600}, 1.5);//.onComplete = back;
        }
        
        private function back(...args):void
        {
            if(tf.width != 600)
                return;
            
            Tweensy.to(tf, {width:1000}, 1.5);//.onComplete = forth;
        }
    }
}