package {
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.events.Event;
    import components.ShapeCache;
    import flash.display.StageScaleMode;
    import flash.display.Stage;
    import flash.events.MouseEvent;
    import flash.display.Graphics;
    import flash.filters.DropShadowFilter;

    [SWF(backgroundColor="#000000", frameRate="75")]
    public class ActionScriptShapes extends Sprite
    {
        private var centerX : Number;
        private var centerY : Number;
        
        private var xSpeed : Number;
        private var ySpeed : Number;
        private var xRadius : Number = 100;
        private var yRadius : Number = 100;
        private var xAngle : Number = 0;
        private var yAngle : Number = 0;
        private var rotataion : Number = 0;
        private var rotataionIncrement : Number;
        
        private var xRad : Number;
        private var yRad : Number;
        
        private var maxChildren : Number;
        
        private var xMoveFunction : Function;
        private var yMoveFunction : Function;
        private var xScaleFunction : Function;
        private var yScaleFunction : Function;
        
        public static var color : Number;
        public static var shapeSize : Number;
        public static var shapeOffsetX : Number;
        public static var shapeOffsetY : Number;
        
        private var filter : DropShadowFilter;
        
        public function ActionScriptShapes()
        {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            initializeParameters();
            this.addEventListener( Event.ENTER_FRAME, onEnterFrame );
            stage.addEventListener( MouseEvent.MOUSE_DOWN, onEvent );
            stage.addEventListener( Event.RESIZE, onEvent );
        }
        
        private function onEvent( event : Event ) : void
        {
            initializeParameters();
        }
        
        private function initializeParameters() : void
        {
            //setup random parameters for animation
            
            for ( var i : Number = numChildren -1; i >= 0; i -- )
            {
                this.removeChildAt( i );
            }
            
            ShapeCache.setShapeClass();
            
            // max 700 children
            maxChildren = 100 + Math.floor( Math.random() * 600 );
            
            centerX = stage.stageWidth/2;
            centerY = stage.stageHeight/2;
            
            xSpeed = Math.random() * 2;
            ySpeed = Math.random() * 2;
            rotataionIncrement = Math.random() * 10;
            
            color = 0xFFFFFF - (Math.random() * 0x888888);
            
            var size : Number = (Math.min( centerX, centerY )/2);
            
            shapeSize = 10 + Math.random() * size;
            shapeOffsetX = 10 + Math.random() * size;
            shapeOffsetY = 10 + Math.random() * size;
            
            xMoveFunction = getMathFunction();
            yMoveFunction = getMathFunction();
            xScaleFunction = getMathFunction();
            yScaleFunction = getMathFunction();
        }
        
        private function getMathFunction() : Function
        {
            var i : int = Math.floor( Math.random() * 4 );
            
            switch ( i ) 
            {
                case 1:
                    return Math.cos;
                    break;
                case 2:
                    return Math.tan;
                    break;
                case 3:
                    return Math.atan;
                    break;
                default:
                    return Math.sin;
                    break;
            }
        }
        
        private function onEnterFrame( event : Event ) : void
        {
            if (numChildren > maxChildren)
            {
                var s:Sprite = this.getChildAt( 0 ) as Sprite;
                removeChild( s );
                ShapeCache.addToCache( s );
            }
            
            xAngle += xSpeed;
            yAngle += ySpeed;
            
            xRad = xAngle * Math.PI/180;
            yRad = yAngle * Math.PI/180;
            
            var shape : Sprite = ShapeCache.getShape();
            
            shape.x = xMoveFunction( xRad ) * xRadius + centerX;
            shape.y = yMoveFunction( yRad ) * yRadius + centerY;
            shape.rotation = rotataion += rotataionIncrement;
            
            shape.scaleX = xScaleFunction( xRad ) * 1;
            shape.scaleY = yScaleFunction( yRad ) * 1;
            
            addChild( shape as Sprite );
        }
        
    }
}