Resizing Images - PLEASE HELP!

Hi, I am having issues getting an imagine to resize. I used the Kirupa XML photogallery tutorial, which works fine, but want the images to resize to fit in the square space I have on my site. I am loading images into a movie clip holder, and that all works fine, but I want to get the images to automatically resize, so they are constrained by both a max width and height, but stay proportional. So, if the max was 300 in both directions, a landscape shot would display with a width of 300 and a proportional height, and a portrait shot would be height of 300 and whatever the corresponding width was. The code I have in the movie clip now is below. It sort of works, but not all the time and something is a little off with the calculations. I’ve spent an embarassing amount of time on this but I am missing something. Also note I don’t want the images to change if they are smaller than the space, bc right now flash expands them. Thanks SO SO much for your help.

onClipEvent (load) {
if (this._width>this._height) {
w_target = 320/this._width;
setProperty("", _width, this._widthw_target);
setProperty("", _height, this._height
w_target);

} else {
h_target = 260/this._height;
setProperty("", _height, _heighth_target);
setProperty("", _width, this._width
h_target);
}}

PS - does anyone know a way to make the picture and/or movie clip holding it display centered?

PPS - I have also tried this…

onClipEvent (load) {
if (this._width>this.height && this._width>300) {
percentage = 320/this._width;
setProperty("", _width, this._widthpercentage);
setProperty("", _height, this._height
percentage);

   } else if (this._height>this.width && this.height>200) { 
           percentage = 240/this._height; 
           setProperty("", _height,_height*percentage); 
           setProperty("", _width, this._width*percentage); 

} }