 #
 # The full text of the license is available at http://www.gnu.org/copyleft/lesser.html
 
-YD = YAHOO.util.Dom
-OD = ORBEON.util.Dom
 Builder = ORBEON.Builder
+Controls = ORBEON.xforms.Controls
+Document = ORBEON.xforms.Document
 Event = YAHOO.util.Event
 Events = ORBEON.xforms.Events
-Controls = ORBEON.xforms.Controls
+OD = ORBEON.util.Dom
+YD = YAHOO.util.Dom
 
 # For the generic label, generic hint, button's label, and link's label:
 #   Show/hide placeholders

        Builder.mouseExitsGridTdEvent.subscribe () -> currentCell = null
        (element) -> currentCell? and f$.is '*', f$.closest currentCell, element
    pointerOutsideCell = (element) -> not pointerInsideCell element

    # Actions
    removeFor = (element) -> f$.removeAttr 'for', element                                                               # So on click on the label, the focus isn't set on the input on click

    # Finite state machine description
    # Diagram: https://docs.google.com/a/orbeon.com/drawings/d/1cJ0B3Tl7QRTMkVUbtlA55C0TUvRiOt5hzR8-dc-aBrk/edit
    transitions = [
        { events: [ mouseEntersGridTd ],      elements: elementsInContainer,  from: [ 'initial' ],                  conditions: [ isEmpty ],                        to: 'placeholder',              actions: [ removeFor, createMock, showPlaceholder ]  }
        { events: [ mouseEntersGridTd ],      elements: elementsInContainer,  from: [ 'initial' ],                  conditions: [ isNonEmpty ],                     to: 'mock',                     actions: [ removeFor, createMock ]                   }
        { events: [ mouseExistsGridTd ],      elements: elementsInContainer,  from: [ 'mock' ],                                                                     to: 'initial',                  actions: [ removeMock ]                              }
        { events: [ mouseExistsGridTd ],      elements: elementsInContainer,  from: [ 'placeholder' ],                                                              to: 'initial',                  actions: [ hidePlaceholder, removeMock  ]            }
        { events: [ click ],                  elements: elementClosest,       from: [ 'placeholder', 'mock' ],                                                      to: 'wait-xhr-to-edit',         actions: [ storeSeqNo ]                              }
        { events: [ ajaxResponse ],           elements: elementsAll,          from: [ 'wait-xhr-to-edit' ],         conditions: [ isNextSeqNo ],                    to: 'edit',                     actions: [ startEdit ]                               }
        { events: [ enterKey, lostFocus ],    elements: elementClosest,       from: [ 'edit' ],                                                                     to: 'edit-done',                actions: [ endEdit, removeMock, fireEditDone ]       }
        { events: [ editDone ],               elements: elementsAll,          from: [ 'edit-done' ],                conditions: [ pointerOutsideCell ],             to: 'initial'                                                                        }
        { events: [ editDone ],               elements: elementsAll,          from: [ 'edit-done' ],                conditions: [ pointerInsideCell, isEmpty ],     to: 'placeholder-after-edit',   actions: [ storeSeqNo, createMock, showPlaceholder ] }
        { events: [ editDone ],               elements: elementsAll,          from: [ 'edit-done' ],                conditions: [ pointerInsideCell, isNonEmpty ],  to: 'mock',                     actions: [ createMock ]                              }
        { events: [ ajaxResponse ],           elements: elementsAll,          from: [ 'placeholder-after-edit' ],   conditions: [ isNextSeqNo ],                    to: 'placeholder',              actions: [ showPlaceholder ]                         }
    ]

    # Finite state machine runner
