I think this entire function could possibly be replaced by a call to jquery's toggleClass method and made declarative.
http://api.jquery.com/toggleClass/