// =================================== //
// Image Resizing                      //
// v1.0 - May 14, 2006                 //
// ----------------------------------- //
// Created by Lloyd Hassell            //
// Website: lloydhassell.brinkster.net //
// Email: lloydhassell@hotmail.com     //
// =================================== //

// INITIALIZATION:

var imageResizing = new Object();

imageResizing.resizingObj = new Object();

// CONFIGURATION:

imageResizing.increment = 20;
imageResizing.frameRate = 30;

// MAIN:

function resizeImageBy(IMGNAME,X,Y,INCREMENT,FRAMERATE,RESIZEENDFUNC) {
   var tempImgObj = getImgObj(IMGNAME);
   X = tempImgObj.width + X;
   Y = tempImgObj.height + Y;   
   //if (tempImgObj.width >= 48 && tempImgObj.width <= 200)
   resizeImageInit(IMGNAME,tempImgObj,X,Y,INCREMENT,FRAMERATE,RESIZEENDFUNC);
   }

function resizeImageTo(IMGNAME,WIDTH,HEIGHT,INCREMENT,FRAMERATE,RESIZEENDFUNC) {
   var tempImgObj = getImgObj(IMGNAME);
   if (isBlank(WIDTH) && !isBlank(HEIGHT)) WIDTH = Math.round(tempImgObj.width * HEIGHT / tempImgObj.height);
   if (!isBlank(WIDTH) && isBlank(HEIGHT)) HEIGHT = Math.round(tempImgObj.height * WIDTH / tempImgObj.width);
   resizeImageInit(IMGNAME,tempImgObj,WIDTH,HEIGHT,INCREMENT,FRAMERATE,RESIZEENDFUNC);
   }

function resizeImageInit(IMGNAME,IMGOBJ,ENDWIDTH,ENDHEIGHT,INCREMENT,FRAMERATE,RESIZEENDFUNC) {
   if (!imageResizing.resizingObj[IMGNAME]) {
      imageResizing.resizingObj[IMGNAME] = new Object();
      imageResizing.resizingObj[IMGNAME].imgObj = IMGOBJ;
      imageResizing.resizingObj[IMGNAME].defaultWidth = IMGOBJ.width;
      imageResizing.resizingObj[IMGNAME].defaultHeight = IMGOBJ.height;
      }
   imageResizing.resizingObj[IMGNAME].startWidth = IMGOBJ.width;
   imageResizing.resizingObj[IMGNAME].startHeight = IMGOBJ.height;
   if (ENDWIDTH < 0) ENDWIDTH = 0;
   imageResizing.resizingObj[IMGNAME].endWidth = (isBlank(ENDWIDTH)) ? imageResizing.resizingObj[IMGNAME].defaultWidth : ENDWIDTH;
   if (ENDHEIGHT < 0) ENDHEIGHT = 0;
   imageResizing.resizingObj[IMGNAME].endHeight = (isBlank(ENDHEIGHT)) ? imageResizing.resizingObj[IMGNAME].defaultHeight : ENDHEIGHT;
   imageResizing.resizingObj[IMGNAME].distX = imageResizing.resizingObj[IMGNAME].endWidth - imageResizing.resizingObj[IMGNAME].startWidth;
   imageResizing.resizingObj[IMGNAME].distY = imageResizing.resizingObj[IMGNAME].endHeight - imageResizing.resizingObj[IMGNAME].startHeight;
   imageResizing.resizingObj[IMGNAME].distZ = Math.sqrt(Math.pow(imageResizing.resizingObj[IMGNAME].distX,2) + Math.pow(imageResizing.resizingObj[IMGNAME].distY,2));
   imageResizing.resizingObj[IMGNAME].currentDistZ = 0;
   imageResizing.resizingObj[IMGNAME].angle = Math.atan(imageResizing.resizingObj[IMGNAME].distX/imageResizing.resizingObj[IMGNAME].distY);
   imageResizing.resizingObj[IMGNAME].increment = (isBlank(INCREMENT)) ? imageResizing.increment : INCREMENT;
   imageResizing.resizingObj[IMGNAME].frameRate = (isBlank(FRAMERATE)) ? imageResizing.frameRate : FRAMERATE;
   imageResizing.resizingObj[IMGNAME].resizeEndFunc = (isBlank(RESIZEENDFUNC)) ? null : new Function(RESIZEENDFUNC);
   window.clearTimeout(imageResizing.resizingObj[IMGNAME].timeout);
   if (imageResizing.resizingObj[IMGNAME].distZ > 0) resizeImage(IMGNAME);
   }

function resizeImage(IMGNAME) {
   imageResizing.resizingObj[IMGNAME].currentDistZ += imageResizing.resizingObj[IMGNAME].increment;
   if (imageResizing.resizingObj[IMGNAME].currentDistZ > imageResizing.resizingObj[IMGNAME].distZ) imageResizing.resizingObj[IMGNAME].currentDistZ = imageResizing.resizingObj[IMGNAME].distZ;
   if (imageResizing.resizingObj[IMGNAME].distY > 0) {
      imageResizing.resizingObj[IMGNAME].imgObj.width = Math.round(imageResizing.resizingObj[IMGNAME].startWidth + Math.sin(imageResizing.resizingObj[IMGNAME].angle) * imageResizing.resizingObj[IMGNAME].currentDistZ);
      imageResizing.resizingObj[IMGNAME].imgObj.height = Math.round(imageResizing.resizingObj[IMGNAME].startHeight + Math.cos(imageResizing.resizingObj[IMGNAME].angle) * imageResizing.resizingObj[IMGNAME].currentDistZ);
      }
   else if (imageResizing.resizingObj[IMGNAME].distY < 0) {
      imageResizing.resizingObj[IMGNAME].imgObj.width = Math.round(imageResizing.resizingObj[IMGNAME].startWidth - Math.sin(imageResizing.resizingObj[IMGNAME].angle) * imageResizing.resizingObj[IMGNAME].currentDistZ);
      imageResizing.resizingObj[IMGNAME].imgObj.height = Math.round(imageResizing.resizingObj[IMGNAME].startHeight - Math.cos(imageResizing.resizingObj[IMGNAME].angle) * imageResizing.resizingObj[IMGNAME].currentDistZ);
      }
   if (imageResizing.resizingObj[IMGNAME].currentDistZ != imageResizing.resizingObj[IMGNAME].distZ) imageResizing.resizingObj[IMGNAME].timeout = window.setTimeout('resizeImage(\'' + IMGNAME + '\')',imageResizing.resizingObj[IMGNAME].frameRate);
   else if (imageResizing.resizingObj[IMGNAME].resizeEndFunc != null) imageResizing.resizingObj[IMGNAME].resizeEndFunc();
   }
