Updated for Cordova 3.x. Fixed some bugs along the way.

Thank you for this great code! I needed it to work in Cordova 3.x, and here is the result.