package {
    import flash.display.Graphics;
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.external.ExternalInterface;
    import flash.text.TextField;

    public class AJAXCharts extends Sprite
    {
        private static const DEFAULT_STYLE : String = "line";
        private static const DEFAULT_BG_COLOR : uint = 0xFFFFFF;
        private static const DEFAULT_FG_COLOR : uint = 0x000000;
        private static const DEFAULT_THICKNESS : uint = 5;
        private static const DEFAULT_MIN : uint = 0;
        private static const DEFAULT_MAX : uint = 100;
        private static const DEFAULT_TIPS : Boolean = true;
        
        private var styles : Object;
        private var data : Array = [];
        
        public function AJAXCharts()
        {
            stage.align = StageAlign.TOP_LEFT;
            stage.scaleMode = StageScaleMode.NO_SCALE;
            setStyle( null );
            registerJSCallbacks();
        }
        
        private function registerJSCallbacks() : void
        {
            ExternalInterface.addCallback( "setData", setData );
            ExternalInterface.addCallback( "setStyle", setStyle );
        }
        
        private function setData( data : * ) : void
        {
            trace( "setData", data );
            this.data = data as Array;
            drawChart();
        }
        
        private function setStyle( style : Object ) : void
        {
            trace( "setStyle", style );
            this.styles = style ? style : {};
            
            if ( !styles.style )
                styles.style = DEFAULT_STYLE;
                
            if ( !styles.bgcolor )
                styles.bgcolor = DEFAULT_BG_COLOR;
            
            if ( !styles.fgcolor )
                styles.fgcolor = DEFAULT_FG_COLOR; 
                
            if ( !styles.thickness )
                styles.thickness = DEFAULT_THICKNESS; 
            
            if ( !styles.min )
                styles.min = DEFAULT_MIN; 
                
            if ( !styles.max )
                styles.max = DEFAULT_MAX; 
                
            if ( !styles.tips )
                styles.tips = DEFAULT_TIPS; 
        
            drawChart();
        }
        
        private function drawChart() : void
        {
            var g: Graphics = this.graphics;    
            g.clear();
            g.lineStyle( 1, styles.fgcolor );
            g.beginFill( styles.bgcolor,1 );
            g.drawRect( 0, 0, stage.stageWidth-1, stage.stageHeight-1 );
            g.endFill();
            
            var span : Number = styles.max - styles.min;
            var xIncrement : Number;
            
            var i : uint = 0;
            if ( styles.style.toString() == "column" )
            {    
                xIncrement = stage.stageWidth / ( data.length );
                i = 1;
            }
            else
                xIncrement = stage.stageWidth / ( data.length-1 );
                
            
            for ( var x: int = this.numChildren-1; x >= 0; x -- )
            {
                this.removeChildAt( x );
            }
            
            for each ( var value : Number in data )
            {
                
                var _value : Number = stage.stageHeight * ( 1- ((value - styles.min) / span ));
                
                if ( i == 0 )
                    g.moveTo(( xIncrement * i ), _value ); 
                
                else
                {
                    switch( styles.style.toString() )
                    {
                        case "plot":
                            g.drawCircle( ( xIncrement * i ), _value, 3 );
                            break;
                        case "column":
                            g.beginFill( styles.fgcolor, .5 );
                            g.drawRect( ( xIncrement * i ) - xIncrement*3/4, _value, xIncrement/2, stage.stageHeight - _value );
                            g.endFill();    
                            break;
                        default:
                            g.lineTo( ( xIncrement * i ), _value );
                            break;
                            
                    }
                    
                }
                
                if ( styles.tips )
                {    
                    var tf : TextField = new TextField()
                    tf.text = value.toString();
                    tf.selectable = false;
                    
                    var z : int = tf.text.indexOf( "." );
                    tf.text = tf.text.substring( 0 , z+3 );
                    
                    tf.x =(xIncrement * i);
                    tf.y = _value;
                    
                    this.addChild( tf );
                }
                i++;
            }
        }
    }
}