Hey all. This is one of my gallery scripts, and it seems to be bugged. The script is supposed to display an image and modify its size and location so it fits in the 398*398 "area" meant for it. Since most of the images are rectangular, this script is essential - if the images are rather wide or long, it forces them into that "area" and keeps their proportions. Now, sometimes it works perfectly, but most of the times it just stretches the rectangular image all over the square area (as if it ignores the code lines that modify the size). When I click the button that activates this script again, it resizes the image as it should do - but it rarely works on the first click. Any ideas what's the problem? function Exlibris(n) { Current = Now + n; Display = new Image(); Display.src = "../Media/Gallery/G" + Current + ".jpg"; X = Display.width; Y = Display.height; if (X == Y) { document.getElementById("Main").style.width = 398; document.getElementById("Main").style.height = 398; } if (X > Y) { document.getElementById("Main").style.width = 398; document.getElementById("Main").style.height = Y*(398/X); } if (X < Y) { document.getElementById("Main").style.width = X*(398/Y); document.getElementById("Main").style.height = 398; } document.getElementById("Main").src = Display.src; document.getElementById("Main").style.left = (398 - Main.width)/2; document.getElementById("Main").style.top = (98 - Main.height)/2; } Code (markup):