package flare.vis.controls
{
    import flare.animate.Tween;
    import flare.display.TextSprite;
    import flare.vis.events.TooltipEvent;
    
    import flash.display.DisplayObject;
    import flash.display.DisplayObjectContainer;
    import flash.display.InteractiveObject;
    import flash.display.Stage;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.filters.DropShadowFilter;
    import flash.geom.Rectangle;
    import flash.text.TextFormat;
    import flash.utils.Timer;

    [Event("show", type="flare.vis.events.TooltipEvent")]
    [Event("hide", type="flare.vis.events.TooltipEvent")]
    [Event("update", type="flare.vis.events.TooltipEvent")]

    /**
     * Interactive control for displaying a tooltip in response to mouse
     * hovers exceeding a minimum time interval. By default, a 
     * <code>flare.display.TextSprite</code> instance is used to show a
     * tooltip. To change the tooltip text, clients can set either the
     * <code>text</code> or <code>htmlText</code> properties of this
     * <code>TextSprite</code>. For example:
     * 
     * <pre>
     * // create a new tooltip control and set the text
     * var ttc:TooltipControl = new TooltipControl();
     * TextSprite(ttc.tooltip).text = "The tooltip text";
     * </pre>
     * 
     * <p>Furthermore, this control fires events corresponding to tooltip show,
     * update (move), and hide events. Listeners can be added to dynamically
     * change the tooltip text when these events occur. Additionally, the
     * default text tooltip can be replaced with an arbitrary
     * <code>DisplyObject</code> to provide completely customized tooltips.</p>
     * 
     * @see flare.vis.events.TooltipEvent
     * @see flare.display.TextSprite
     */
    public class TooltipControl extends Control
    {        
        private var _cur:DisplayObject;
        
        private var _showTimer:Timer;
        private var _hideTimer:Timer;
        private var _show:Boolean = false;
        private var _t:Tween;
        
        /** The tooltip delay, in milliseconds. */
        public function get showDelay():Number { return _showTimer.delay; }
        public function set showDelay(d:Number):void { _showTimer.delay = d; }
        
        /** The delay before hiding a tooltip, in milliseconds. */
        public function get hideDelay():Number { return _hideTimer.delay; }
        public function set hideDelay(d:Number):void { _hideTimer.delay = d; }
        
        /** The legal bounds for the tooltip in stage coordinates.
         *  If null (the default), the full stage bounds are used. */
        public var tipBounds:Rectangle = null;
        
        /** The x-offset from the mouse at which to place the tooltip. */
        public var xOffset:Number = 0;
        /** The y-offset from the mouse at which to place the tooltip. */
        public var yOffset:Number = 25;
        
        /** The display object presented as a tooltip. */
        public var tooltip:DisplayObject = null;
        
        /** Duration of fade animations (in seconds) for tooltip show and hide.
         *  If less than or equal to zero, no fade will be performed. */
        public var fadeDuration:Number = 0.3;
        
        /** Indicates if the tooltip should follow the mouse pointer. */
        public var followMouse:Boolean = true;
        
        // --------------------------------------------------------------------
        
        /**
         * Creates a new TooltipControl.
         * @param filter a Boolean-valued filter function indicating which
         *  items should receive tooltip handling
         */
        public function TooltipControl(filter:*=n