To see the code in action, visit JSFiddle