package classes.com.senocular.display
{
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;

    public class TransformToolCursor extends TransformToolControl
    {
        protected var _mouseOffset:Point;
        protected var contact:Boolean = false;
        protected var active:Boolean = false;
        protected var references:Dictionary;

        public function TransformToolCursor()
        {
            this._mouseOffset = new Point(20, 20);
            this.references = new Dictionary(true);
            addEventListener(TransformTool.CONTROL_INIT, this.init);
            return;
        }// end function

        public function get mouseOffset() : Point
        {
            return this._mouseOffset.clone();
        }// end function

        public function set mouseOffset(param1:Point) : void
        {
            this._mouseOffset = param1;
            return;
        }// end function

        public function addReference(param1:DisplayObject) : void
        {
            if (param1 && !this.references[param1])
            {
                this.references[param1] = true;
                this.addReferenceListeners(param1);
            }
            return;
        }// end function

        public function removeReference(param1:DisplayObject) : DisplayObject
        {
            if (param1 && this.references[param1])
            {
                this.removeReferenceListeners(param1);
                delete this.references[param1];
                return param1;
            }
            return null;
        }// end function

        public function updateVisible(event:Event = null) : void
        {
            if (this.active)
            {
                if (!visible)
                {
                    visible = true;
                }
            }
            else if (visible != this.contact)
            {
                visible = this.contact;
            }
            this.position(event);
            return;
        }// end function

        public function position(event:Event = null) : void
        {
            if (parent)
            {
                x = parent.mouseX + this.mouseOffset.x;
                y = parent.mouseY + this.mouseOffset.y;
            }
            return;
        }// end function

        private function init(event:Event) : void
        {
            _transformTool.addEventListener(TransformTool.TRANSFORM_TOOL, this.position, false, 0, true);
            _transformTool.addEventListener(TransformTool.NEW_TARGET, this.referenceUnset, false, 0, true);
            _transformTool.addEventListener(TransformTool.CONTROL_TRANSFORM_TOOL, this.position, false, 0, true);
            _transformTool.addEventListener(TransformTool.CONTROL_DOWN, this.controlMouseDown, false, 0, true);
            _transformTool.addEventListener(TransformTool.CONTROL_MOVE, this.controlMove, false, 0, true);
            _transformTool.addEventListener(TransformTool.CONTROL_UP, this.controlMouseUp, false, 0, true);
            this.updateVisible(event);
            this.position(event);
            return;
        }// end function

        private function addReferenceListeners(param1:DisplayObject) : void
        {
            param1.addEventListener(MouseEvent.MOUSE_MOVE, this.referenceMove, false, 0, true);
            param1.addEventListener(MouseEvent.MOUSE_DOWN, this.referenceSet, false, 0, true);
            param1.addEventListener(MouseEvent.ROLL_OVER, this.referenceSet, false, 0, true);
            param1.addEventListener(MouseEvent.ROLL_OUT, this.referenceUnset, false, 0, true);
            return;
        }// end function

        private function removeReferenceListeners(param1:DisplayObject) : void
        {
            param1.removeEventListener(MouseEvent.MOUSE_MOVE, this.referenceMove, false);
            param1.removeEventListener(MouseEvent.MOUSE_DOWN, this.referenceSet, false);
            param1.removeEventListener(MouseEvent.ROLL_OVER, this.referenceSet, false);
            param1.removeEventListener(MouseEvent.ROLL_OUT, this.referenceUnset, false);
            return;
        }// end function

        protected function referenceMove(event:MouseEvent) : void
        {
            this.position(event);
            event.updateAfterEvent();
            return;
        }// end function

        protected function referenceSet(event:Event) : void
        {
            this.contact = true;
            if (!_transformTool.currentControl)
            {
                this.updateVisible(event);
            }
            return;
        }// end function

        protected function referenceUnset(event:Event) : void
        {
            this.contact = false;
            if (!_transformTool.currentControl)
            {
                this.updateVisible(event);
            }
            return;
        }// end function

        protected function controlMouseDown(event:Event) : void
        {
            if (this.references[_transformTool.currentControl.relatedObject])
            {
                this.active = true;
            }
            this.updateVisible(event);
            return;
        }// end function

        protected function controlMove(event:Event) : void
        {
            if (this.references[_transformTool.currentControl.relatedObject])
            {
                this.position(event);
            }
            return;
        }// end function

        protected function controlMouseUp(event:Event) : void
        {
            if (this.references[_transformTool.currentControl.relatedObject])
            {
                this.active = false;
            }
            this.updateVisible(event);
            return;
        }// end function

    }
}
