mediawiki.page.gallery: Various clean up and minor optimisations
* Moved some variables to a lower scope more appropiate,
to make it easier to understand what they are used for,
and to reduce chances of mistakes in the future.
* Use push() instead of .length assignment.
* Use the same property for checks as for access ([0] vs length),
also potentially faster, but nothing worth mentioning.
* Avoid duplicate $this.outerWidth(), expensive computation.
Bug: T88654
Change-Id: Ib03d05a5c6422afdec11c56360bd568bdb2633fb