Your indenting is a messed up. Can I suggest you run your code through http://jsbeautifier.org/ . That will make it easier to review and maintain.