Need a suggestion on animating cloth in code

I want to do cloth animation via code like this one http://www.youtube.com/watch?v=GzCLhDKeWeA (scarf in the video case).
Any ideas or suggestion to do that?