The problem is that your if / else statements move away from using $(this)
to using a vague reference to a
instead.
Your first if statement is correct: if ($this.hasClass('active') === true)
But then the following else/if is incorrect: else if(a.hasClass('active') === false)
That should read else if($this.hasClass('active') === false)
Example: http://jsfiddle.net/ktTKe/3/