package
{
    import com.hurlant.util.Base64;
    
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.filters.BlurFilter;
    import flash.text.TextField;
    import flash.text.TextFormat;
    import flash.utils.ByteArray;
    
    import org.libspark.betweenas3.BetweenAS3;
    import org.libspark.betweenas3.easing.Cubic;
    import org.libspark.betweenas3.easing.Quad;
    import org.libspark.betweenas3.tweens.ITween;
    
    public class DC extends Sprite
    {
        private const R:uint = 60;
        private const W:Number = stage.stageWidth / 2;
        private const H:Number = stage.stageHeight / 2;
        private var _letter:Array = [];
        private var _ball:Ball;
        private var _temp:TextField = new TextField();
        private var _bounceTiem:Number = 0.36;
        
        public function DC()
        {
            stage.scaleMode = StageScaleMode.SHOW_ALL;
            stage.frameRate = 60;
            
            var param:Object = loaderInfo.parameters;
            var word:String = param["w"];
            if(word == null)
                word = "Dreamcast";
            else {
                var ba:ByteArray = new ByteArray();
                ba = Base64.decodeToByteArray(word);
                ba.uncompress();
                word = ba.readUTFBytes(ba.length);
            }
            _temp.text = word;
            var format:TextFormat = new TextFormat(null, R);
            _temp.setTextFormat(format);
            
            for(var i:uint = 0; i < _temp.text.length; i++)
            {
                var tf:TextField = new TextField();
                tf.text = _temp.text.charAt(i);
                tf.selectable = false;
                tf.setTextFormat(format);
                
                if(i)
                    tf.x = _letter[i-1].x + _letter[i-1].textWidth;
                else
                    tf.x = W - _temp.textWidth / 2;
                tf.y = H + R/2;
                _letter.push(tf);
            }
            
            _ball = new Ball();
            _ball.filters = [new BlurFilter()];
            _ball.x = _letter[0].x - R*2.2;
            _ball.y = _letter[0].y - R*4;
            addChild(_ball);
            
            var tween:ITween = BetweenAS3.bezierTo(_ball, {x: _letter[0].x + _letter[0].textWidth, y: _letter[0].y + 35}, {x: _letter[0].x + R, y: _letter[0].y - R}, 2, Cubic.easeIn);
            tween.onComplete = bounce;
            tween.onCompleteParams = [1];
            tween.play();
        }
        
        private function bounce(n:uint):void
        {
            character(n-1);
            var tween:ITween = BetweenAS3.bezierTo(_ball, {x: _letter[n].x + _letter[n].textWidth, y: _ball.y}, {x: _letter[n].x + _letter[n].textWidth / 4, y: _letter[n].y - 10}, _bounceTiem);
            if(_bounceTiem >= 0.22) {
                _bounceTiem -= 0.02;
            }
            if(n < _temp.text.length - 1)
                tween.onComplete = function():void
                {
                    bounce(n+1)
                };
            else
                tween.onComplete = ぴょーん;
            tween.play();
        }
        
        private function character(index:uint):void
        {
            addChild(_letter[index]);
            BetweenAS3.tween(_letter[index], {y: _letter[index].y, alpha: 1}, {y: _letter[index].y + 20, alpha: 0}).play();
        }
        
        private function ぴょーん():void
        {
            character(_temp.text.length - 1);
            
            var tween:ITween = BetweenAS3.bezierTo(_ball, {x: W, y: _letter[0].y - R*0.8}, {x: (W + _ball.x) / 2, y: _letter[0].y - R*5}, 1.2, Quad.easeOut);
            tween.play();
            tween.onComplete = ぐるぐる;
        }
        
        private function ぐるぐる():void
        {
            var sp:Sprite = new Sprite();
            sp.filters = [new BlurFilter()];
            addChild(sp);
            sp.graphics.lineStyle(6, _ball.color);
            sp.graphics.moveTo(_ball.x, _ball.y);
            
            addEventListener(Event.ENTER_FRAME, onEnterFrame(sp));
            
            var tweenArray:Array = [];
            for(var i:uint = 0; i < 900; i++)
            {
                var toX:Number = _ball.x - R * i / 720 * Math.cos(i * Math.PI / 180);
                var toY:Number = _ball.y - R * i / 720 * Math.sin(i * Math.PI / 180);
                tweenArray.push(BetweenAS3.to(_ball, {x: toX, y: toY}, 0.002));
            }
            var tween:ITween = BetweenAS3.serialTweens(tweenArray);
            tween.onComplete = function():void
            {
                removeEventListener(Event.ENTER_FRAME, onEnterFrame);
                sp.graphics.lineTo(_ball.x, _ball.y);
                removeChild(_ball);
            };
            tween.play();
        }
        
        private function onEnterFrame(sp:Sprite):Function
        {
            return function(e:Event):void
            {
                sp.graphics.lineTo(_ball.x, _ball.y);
            };
        }
    }
}

import flash.display.Sprite;

class Ball extends Sprite
{
    private var _color:uint;
    
    public function get color():uint
    {
        return _color;
    }
    
    public function Ball(j:Boolean = true) {
        if(j)
        {
            graphics.beginFill(0xea5504);
            _color = 0xea5504;
        } else {
            graphics.beginFill(0x0000FF);
            _color = 0x0000FF;
        }
        graphics.drawCircle(0, 0, 4);
        graphics.endFill();
    }
}