function chgTo(to,wait) {
  var sm = this;
  if (sm.pending!==undefined) sm.pending = clearTimeout(sm.pending);
  if (wait) return sm.pending = setTimeout(function(){
    sm.pending=undefined;chgTo.call(sm,to);},wait);
  if (to!=sm.state && to in sm.states) {
    var s = sm.states[sm.state];
    if (s && 'exit' in s) s.exit.call(sm);
    sm.last.state = sm.state;
    sm.state = to;
    if ('enter' in sm.states[to]) {
      var auto = sm.states[to].enter.call(sm);
      if (auto) chgTo.call(sm,auto.state,auto.wait||0);   
      };
    };
  }
// any pending state change is first cancelled
any pending state change is first cancelled
any pending state change is first cancelled
function chgTo(to,wait) {
  var sm = this;
  if (sm.pending!==undefined) sm.pending = clearTimeout(sm.pending);
  if (wait) return sm.pending = setTimeout(function(){
    sm.pending=undefined;chgTo.call(sm,to);},wait);
  if (to!=sm.state && to in sm.states) {
    var s = sm.states[sm.state];
    if (s && 'exit' in s) s.exit.call(sm);
    sm.last.state = sm.state;
    sm.state = to;
    if ('enter' in sm.states[to]) {
      var auto = sm.states[to].enter.call(sm);
      if (auto) chgTo.call(sm,auto.state,auto.wait||0);   
      };
    };
  }
function chgTo ( to , wait ) { var sm = this ; if ( sm . pending !== undefined ) sm . pending = clearTimeout ( sm . pending ) ; if ( wait ) return sm . pending = setTimeout ( function ( ) { sm . pending = undefined ; chgTo . call ( sm , to ) ; } , wait ) ; if ( to != sm . state && to in sm . states ) { var s = sm . states [ sm . state ] ; if ( s && 'exit' in s ) s . exit . call ( sm ) ; sm . last . state = sm . state ; sm . state = to ; if ( 'enter' in sm . states [ to ] ) { var auto = sm . states [ to ] . enter . call ( sm ) ; if ( auto ) chgTo . call ( sm , auto . state , auto . wait || 0 ) ; } ; } ; }
any pending state change is first cancelled
any pending state change is first cancelled