Link requires 2 clicks to show current info

Link requires 2 clicks to show current info

I'm working on adding the ability to download a Hi-Res version of an image that is in a gallery that utilizes the Elevatezoom plugin on Drupal based website. I've got to the point where I can get the correct image to open in the new window, but only after clicking on the thumbnail image twice!

The first click seems setting the link to the currently displayed image(the main image updates, but the link to the hi-res is to the previous image).

For example: The page loads with Image A showing in main window, I click on image B thumbnail. Image B loads in main window, but image A link loads into the hi-res link. If I then click on the thumbnail for Image C, the link will update to image B. If I click on thumbnail for image C again it will display image C(double clicking on a thumbnail will force the correct link to load)

Here's the code I am using to pull the high res link.
The link script:
  1.          jQuery(function(){
                 $('#image_gallery img').click(function(y){
                     var bg = $('.zoomWindow').css('background-image');
                         bg = bg.replace('url("','').replace(')','').replace('"','');
                        $("#hirez").prop("href", bg);
                     });
             });

The link:
  1. <a id="hirez" class="highres btn m-bottom-20" title="Download a High-resolution Image" target="_blank" href="<?= image_style_url("zoom", $photos[0]["uri"])?>"><?= t("Download a High-resolution Image")?></a>

Any help would be extremely appreciated. I'm terribly frustrated at this point and have tried just about everything I can think of.