fullscreen & centred background image script
<p>I have made a simple function that adapts the size of an image according to the size of the window. I can't determine when exactly, but sometimes the img does not fill the width of the screen, but continues to stick to the height. Any idea why this could be?</p>

<p>If i console log (iRatio &lt;= wRatio) anything seems to fit, but the shown result is incorrect.</p>

<p>The img is set as <code>postion: absolute; with: 100%; top:0; left:0;</code> in the css.
<code>$win</code> contains <code>$(window)</code> and <code>$img</code> the background image</p>

<pre><code>function autoImageSize($img, $win){
        var wHeight = $win.height(),
            wWidth = $win.width(),
            iHeight = $img.height(),
            iWidth = $img.width(),
            iRatio = iWidth / iHeight, 
            wRatio = wWidth / wHeight;

          if(iRatio &lt;= wRatio){
            $img.css({
               width: "100%", 
               height: "auto",
               top: "-" + ((iHeight - wHeight)/2) + "px",
               left: 0
            });
          }else{
            $img.css({
               width: "auto", 
               height: "100%",
               top: 0,
               left: "-" + ((iWidth - wWidth)/2) + "px"
             });
          }

          return [$img.width(), $img.height()];
};
</code></pre>
