you have var dist_down in your while loop getting initialized ever and ever again. take it out of your while loop and try it again. I'm not sure whether it solves the problem but you should give it a try...^^

Code:
var dist_down;
while(1)
{
...
}