I just took a long ride this weekend and dealt with a lot of negative thoughts near the last half, especially since over 50 miles of it wasn't a trail so much as a glorified, overgrown gravel alleyway. I practice mindfulness meditation every day in my spare time, it does wonders not only for bicycling but for the rest of your life.
As other people have mentioned earlier, it can be helpful when you feel yourself sinking into the funk, to realize that YOU aren't the funk. You're not a depressed person, you're not terrible, you're not a dumb*** for doing it, or whatever kinds of evils your brain creates for you to enjoy. The bad thinking is something unto itself. Looking at your evil thoughts like they are just a temporary unwelcome visitor helps put some space between you and the negativity, and can give you some control over your brain again.
of course, that's assuming you're dealing with negative thoughts that are mostly mental. if you find your mood corresponds to nutrition as other people have mentioned, that's even better--a much simpler fix!
here's a link for anyone interested:
http://www.thegreatcourses.com/tgc/c....aspx?cid=1933