/// <summary>
        /// Update the load Behaviour by dropping it onto window functionality.
        /// </summary>
        private void DoDragAndDrop()
        {
            if (Event.current.type == EventType.DragUpdated || Event.current.type == EventType.DragPerform)
            {
                // Show a copy icon on the drag
                DragAndDrop.visualMode = DragAndDropVisualMode.Copy;

                if (Event.current.type == EventType.DragPerform)
                {
                    DragAndDrop.AcceptDrag();
                    if (DragAndDrop.objectReferences.Length == 1 && DragAndDrop.objectReferences[0] is EliotBehaviour)
                    {
                        Behaviour        = DragAndDrop.objectReferences[0] as EliotBehaviour;
                        _pathToBehaviour = AssetDatabase.GetAssetPath(Behaviour);
                        Behaviour.UpdateBehaviour();
                        if (string.IsNullOrEmpty(Behaviour.Json))
                        {
                            Clear(null);
                            Save(null);
                        }
                        Reverse(null);
                    }
                }

                Event.current.Use();
            }
        }
        /// <summary>
        /// This function is called when the object is loaded.
        /// </summary>
        private void OnEnable()
        {
            hideFlags = HideFlags.HideAndDontSave;

            if (m_GUIStyle == null)
            {
                m_GUIStyle = (GUISkin)AssetDatabase.LoadAssetAtPath(
                    PathManager.EliotStyles() + "Style.guiskin", typeof(GUISkin));
            }
            if (_backgroundTexture == null)
            {
                _backgroundTexture = (Texture2D)AssetDatabase.LoadAssetAtPath(
                    PathManager.EliotImages() + "grid.png", typeof(Texture2D));
                _backgroundTexture.wrapMode = TextureWrapMode.Repeat;
            }
            var icon = (Texture2D)AssetDatabase.LoadAssetAtPath(
                PathManager.EliotImages() + "gamepad_grey.png", typeof(Texture2D));

            if (icon != null)
            {
                titleContent = new GUIContent(" Behaviour", icon);
            }

            if (_pathToBehaviour != null)
            {
                Behaviour = AssetDatabase.LoadAssetAtPath <EliotBehaviour>(_pathToBehaviour);
            }

            try{ Reverse(null); }catch (Exception) { /**/ }
        }

        /// <summary>
        /// Make sure there is exactly one Entry in the Editor.
        /// </summary>
        private static void RemoveExtraEntry()
        {
            var entries = from entry in Nodes where entry is EntryNode select entry;
            var qNodes  = entries.ToList();

            if (qNodes.Count <= 1)
            {
                return;
            }
            foreach (var entry in qNodes)
            {
                if (entry.Transitions.Count == 0)
                {
                    Nodes.Remove(entry);
                }
            }
        }

        /// <summary>
        /// Update the window when mouse is over another one.
        /// </summary>
        private void Update()
        {
            if (_event == null)
            {
                return;
            }
            if (_event.type == EventType.MouseUp)
            {
                _draggingGroup = false;
                ClearGrouped();
                _dragging = false;

                foreach (var node in Nodes)
                {
                    if (!_selectionRect.Contains(node.Rect.center))
                    {
                        continue;
                    }
                    node.Grouped = true;
                }
                _genericMenu = null;
            }
        }

        /// <summary>
        /// Draw GUI and run listeners.
        /// </summary>
        private void OnGUI()
        {
            _event = Event.current;

            var width  = _maxX - _minX + Padding > position.width ? _maxX - _minX + Padding : position.width;
            var height = _maxY - _minY + Padding > position.height ? _maxY - _minY + Padding : position.height;

            _spaceRect      = new Rect(0, 0, width, height);
            _viewRect       = new Rect(0, 0, position.width, position.height);
            _scrollPosition = GUI.BeginScrollView(_viewRect, _scrollPosition, _spaceRect, GUIStyle.none, GUIStyle.none);

            GUI.DrawTextureWithTexCoords(_spaceRect, _backgroundTexture,
                                         new Rect(0, 0, _spaceRect.width / _backgroundTexture.width, _spaceRect.height / _backgroundTexture.height));

            GUI.color = Color.white;

            m_GUIStyle_Start = GUI.skin;
            GUI.skin         = m_GUIStyle;

            UpdateShortcuts();

            InitEntry();
            RemoveExtraEntry();

            DragMultipleWindows();

            _mousePos = Event.current.mousePosition;
            if (!_makeTransitionMode)
            {
                UpdateContextMenu();
                UpdateSelected();
                UpdateNodes();
                UpdateSelectedTransitions();
            }
            else
            {
                if (Event.current.type != EventType.Layout)
                {
                    UpdateNewTransition(_transitionColor, _transitionIsNeg);
                }
                Repaint();
                UpdateNodes();
            }
            GUI.EndScrollView();

            DoDragAndDrop();
            PanScrollView();
            SelectionBox();

            GUI.skin = m_GUIStyle_Start;
        }

        private static void UpdateShortcuts()
        {
            try
            {
                if (Event.current.control && Event.current.keyCode == KeyCode.Z)
                {
                    UndoOperation(null);
                }
                if (Event.current.control && Event.current.keyCode == KeyCode.Y)
                {
                    RedoOperation(null);
                }
                if (Event.current.control && Event.current.keyCode == KeyCode.S)
                {
                    Save(null);
                }
                if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Delete)
                {
                    Delete(null);
                }
                if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.I)
                {
                    AddNode("Invoker");
                }
                if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.O)
                {
                    AddNode("Observer");
                }
                if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.L)
                {
                    AddNode("Loop");
                }
                if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Y)
                {
                    var node = HoverOnNode();
                    if (node != null)
                    {
                        StartTransition(node.Rect, node, (node is ObserverNode ? PositiveColor : node is LoopNode ? LoopColor : NeutralColor), false);
                    }
                }
                if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.N)
                {
                    var node = HoverOnNode();
                    if (node != null)
                    {
                        StartTransition(node.Rect, node, (node is ObserverNode ? NegativeColor : NeutralColor), true);
                    }
                }
            }
            catch (Exception)
            {
                /*Its ok*/
            }
        }