Panel collapsed option not accessible

Panel collapsed option not accessible

I have been working on a project where I have many panels open in one div.  I want to be able to have panel control on the group such as collapse all, expand all.  I thought this would be easy by creating an array of all the panels and then doing this:

for (var i = 0;i<panels.length;i++) {
      var panel = panels[i];
      if (panel.panel('option','collapsed')) {
            panel.panel('toggle');
      }
}

however, this did not work.  The odd part was that when I used Developer Tools (chrome) to debug it, the panels would magically open.  If I didn't do the breakpoints and debug nothing would happen.  Anyone have any ideas or insight?