Freelancer:
hoyleg
Winner
Attempt number three
I think I better understand your requirement now, here's my next attempt! The general behaviour and specific use cases are detailed in the comments.