Resize and reposition dynamic image

This has been addressed in several topics within the forum, but not quite like what I need…

I’m currently building an application that allows users to “import” uploaded images into the FUI and move them around to place them where they want. I have three seperate sections where they can do this and each section has a different maxWidth and maxHeight… What I’m trying to accomplich is to be able dynamically bring the image in resize it so that it fits within the _root.maxWidth and _root.maxHeight, while constraining the proportions, THEN I need it to be be placed (if needed) within the boundry of the section they are importing the image to…

I’m grabbing the image via the tree component and continuing from there… here is my code.

myTreeDataProvider = new XML();
myTreeDataProvider.ignoreWhite = true;
myTreeDataProvider.onLoad = function() {
 myTree.dataProvider = myTreeDataProvider;
myTreeListener = new Object();
myTreeListener.change = function(eventObject) {
 // the selected node
 var theSelectedNode =;
 // the label of the selected node
 var theSelectedNodeLabel = theSelectedNode.attributes.label;
 var theSelectedNodePath = theSelectedNode.attributes.imgPath;
 // add a status message to the status message text area component
 loadMovie(theSelectedNodePath, "thumbnailDisplay");
 if (theSelectedNode.hasChildNodes()) {
 } else {
  statusTxt.text = "Loading Image...";
var tempImageBoxHolder;
var tempImageBoxHolderHolder;
// add the event listeners
myTree.addEventListener("nodeOpen", myTreeListener);
myTree.addEventListener("nodeClose", myTreeListener);
myTree.addEventListener("change", myTreeListener);
insertBtn.onRelease = function() {
 tempImageBoxHolder = _root.currentAccess.attachMovie("imageBoxHolder", "imageBoxHolder"+String(tempLevel), tempLevel);
 tempImageBoxHolder._x = iBoxX;
 tempImageBoxHolder._y = iBoxY;
 tempImageBoxHolderHolder = tempImageBoxHolder.attachMovie("imagePlaceHolder", "imagePlaceHolder"+String(tempLevel), tempLevel);
 loadPath = tempImageBoxHolderHolder;
 loadMovie(thumbnailDisplay._url, loadPath);
 var mclListener:Object = new Object();
 mclListener.onLoadInit = function(loadPath:MovieClip) {
  if (_root.currentSection eq "header") {
   trace("im the header section");
  } else if (_root.currentSection eq "content") {
   trace("im the content section");
  } else if (_root.currentSection eq "footer") {
   trace("im the footer section");
  maxHeight = _root.totalHeight;
  maxWidth = _root.totalWidth;
  if (tempImageBoxHolder._height>maxHeight) {
   tempImageBoxHolder._height = maxHeight;
   tempImageBoxHolder._yscale = tempImageBoxHolder._xscale=100;
   tempImageBoxHolder._y = 0;
  } else if (tempImageBoxHolder._width>maxWidth) {
   tempImageBoxHolder._height = maxWidth;
   tempImageBoxHolder._xscale = tempImageBoxHolder._yscale=100;
   tempImageBoxHolder._x = 0;
  newX = tempImageBoxHolder._x+tempImageBoxHolder._width;
  newY = tempImageBoxHolder._y+tempImageBoxHolder._height;
  if (newX>maxWidth) {
   tempImageBoxHolder._x = 0;
  } else if (newY>maxHeight) {
   tempImageBoxHolder._y = 0;
 var image_mcl:MovieClipLoader = new MovieClipLoader();
 image_mcl.loadClip(thumbnailDisplay._url, loadPath);
 _root.a = thumbnailDisplay._url;
 _root.browseImage._visible = false;
 _root.theTarget.image = true;
 _root.imageBtn.selected = true;
cancelBtn.onRelease = function() {
 _root.browseImage._visible = false;
this.myTree.setStyle("indentation", -1);

if you feel that the code could be simplified, or reorganized, please help me out in the process… thanks so much!