If you are attempting this from the mouse clicked event handler, the problem is that the event fires before the index is selected. This is why it doesn't work on the first click but does work on the second. Of course, by the second click, all of the other scripting is out of sequence.
The correct way to fire this script is from the propertyChange event handler. Simply take all of your code and nest it in the following if
statement:
if event.propertyName == 'selectedIndex':