Finite State Machine for javascript and JQuery