hierarchial state machine for javascript