Optimal timing control of switched stochastic systems