Here are two small adjustments to the code:
1/ use time instead of time_step (which version of Game Studio do you use?)
2/ Change the line

if((pan_difference > 0) && (abs(pan_difference) < 180))
to
if((pan_difference > 1) && (abs(pan_difference) < 180))