That's a heck of a lot of code to ask us to go through. I suggest:
1) please surround your posted code with code tags:
// ... your code goes here
This way your code will retain its formatting and be more readable.
2) Please isolate for us the code of interest so that we don't have to go through a ton of unrelated code. Please remember that we're volunteers doing this on our free time.