//
// Image Animation Unit 1.0 rev 1 by Justin Pasher
// "Multi-image animation" (MIA) support based on concept by Roy Whittle
// (C) 2000 Lazor Software. All Rights Unknown...
//
// Use this code at your own risk. If you use this code, please leave the above comment
// intact. There's nothing worse than someone stealing code and pawning it off as their
// own. :) With that said... let's move on.
//
//   Quick Overview:
//       Image Animation Unit (IAU) is just that...an easy to install and configure
//     image animation unit. It allows fancy animations of images on your web site when
//     the mouse cursor is moved over a particular image.
//
//   How it works:
//       If you have no desire to know exactly how and why this unit works, you can skip
//     this section.
//
//     There are three main steps to the IAU
//
//       1: Create the image object with all specified settings (pictures to use, animation
//          delay, etc...)
//       2: Create a timer, if needed (used for MIA support)
//       3: Animate the picture on the OnMouseOver and OnMouseOut events
//
//   Technical aspects:
//     * You MUST call CreateSimpleAnim FIRST to actually define an animated image. If you
//       wants to create a MIA, you can use the AddImage function and refer to the name of
//       the previously created image object
//
//

//=============//
// VAR Section //
//=============//
  // used internally to keep track of the number of images. DO NOT MODIFY MANUALLY
  var lastimg = 0;

  // These "constants" can be used for calling the AddImage function
  var OVER = 1;   // use image for OnMouseOver
  var OUT  = 2;   // use image for OnMouseOut

  // "Constants" used internally to determine the status of an animation
  var aDONE = 0;   // animation is stopped
  var aOVER = 1;   // currently animating "in"  (mouseOver)
  var aOUT  = 2;   // currently animating "out" (mouseOut)

  // Can we use image swapping (IE4+ or NS3+)?
  if (!document.images) UseImgSwapping = false; else UseImgSwapping = true;

  // Add the following fields to the Array object (helps us store custom variables)
  Array.prototype.name        = null;
  Array.prototype.delay       = 10;
  Array.prototype.status      = aDONE;
  Array.prototype.nextstatus  = 0;  // either 0=do nothing, 1=run MouseOut event
  Array.prototype.imgnum      = 0;  // what image # is the MIA on?
  Array.prototype.numpicsover = 0;  // num of pics for OnMouseOver event
  Array.prototype.numpicsout  = 0;  // num of pics for OnMouseOut event
  Array.prototype.picsover    = null;
  Array.prototype.picsout     = null;

  // This is the array that holds all of the pictures
  var objIMG = new Array();

//===========//
// FUNCTIONS //
//===========//
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function GetObjNum([string] IMGName)                                                 //
  //                                                                                      //
  // This function (called internally) returns the array index associated with IMGName.   //
  // It return -1 if the image is not found. You should have no need to call it manually. //
  //////////////////////////////////////////////////////////////////////////////////////////
function GetObjNum(IMGName)
{
  for (var x=0;x<lastimg;x++)
    if (objIMG[x].name == IMGName) return x;
  return -1;   // no match
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function image_over([string] IMGName)                                                //
  //                                                                                      //
  // This function should be called from the anchor's OnMouseOver event with the name of  //
  // the image to change, defined by the NAME property in the IMG tag                     //
  //////////////////////////////////////////////////////////////////////////////////////////
function image_over(IMGName)
{
  if (!UseImgSwapping) return;
  num = GetObjNum(IMGName);
  if (num == -1) return;  // error!
  if (objIMG[num].numpicsover == 1)
    document.images[IMGName].src = objIMG[num].picsover[0].src;
  else
  {
    if (objIMG[num].status == aOUT)
      objIMG[num].nextstatus = 1;     // tell it to "deanimate" the image when it can
    else if (objIMG[num].status == aDONE)
    {
      objIMG[num].status     = aOVER;
      objIMG[num].nextstatus = 0;
      objIMG[num].imgnum     = 0;
      setTimeout("AnimateImage("+num+")",objIMG[num].delay);
    }
    else objIMG[num].nextstatus = 0;  // status == aOVER
  }
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function image_out([string] IMGName)                                                 //
  //                                                                                      //
  // This function should be called from the anchor's OnMouseOut event                    //
  //////////////////////////////////////////////////////////////////////////////////////////
function image_out(IMGName)
{
  if (!UseImgSwapping) return;
  num = GetObjNum(IMGName);
  if (num == -1) return;  // error!
  if (objIMG[num].numpicsout == 1)
  {
    if ((objIMG[num].status == aDONE) || (objIMG[num].numpicsover == 1))
      document.images[IMGName].src = objIMG[num].picsout[0].src;
    else
      objIMG[num].nextstatus = 1;
  }
  else
  {
    if (objIMG[num].status == aOVER)
      objIMG[num].nextstatus = 1;     // tell it to "deanimate" the image when it can
    else if (objIMG[num].status == aDONE)
    {                                 // ...otherwise "deanimate" now
      objIMG[num].status     = aOUT;
      objIMG[num].nextstatus = 0;
      objIMG[num].imgnum     = 0;
      setTimeout("AnimateImage("+num+")",objIMG[num].delay);
    }
    else objIMG[num].nextstatus = 0;  // status == aOUT
  }
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function CreateSimpleAnim([string] IMGName, [string] img1, [string] img2)            //
  //                                                                                      //
  // This function initializes and creates one "simple" image animation. This is the only //
  // function you have to call if you just want one picture when the mouse is over the    //
  // image and one picture when the mouse is not over the image                           //
  //                                                                                      //
  //   IMGName - The name of the image object (defined by the NAME prop. in the IMG tag)  //
  //   img1    - The image to display when the mouse IS over the image object             //
  //   img2    - The image to display when the mouse IS NOT over the image object         //
  //                                                                                      //
  // The image object uses the following format                                           //
  //                                                                                      //
  //   objIMG[x].name        = IMGName                                                    //
  //   objIMG[x].delay       = 10 (default)                                               //
  //   objIMG[x].status      = 0                                                          //
  //   objIMG[x].nextstatus  = 0                                                          //
  //   objIMG[x].numpicsover = 1                                                          //
  //   objIMG[x].numpicsout  = 1                                                          //
  //   objIMG[x].picsover    = Array of images for the OnMouseOver event                  //
  //   objIMG[x].picsout     = Array of images for the OnMouseOut  event                  //
  //                                                                                      //
  //////////////////////////////////////////////////////////////////////////////////////////
function CreateSimpleAnim(IMGName, img1, img2)
{
  if (!UseImgSwapping) return;
  objIMG[lastimg] = new Array(1);
  objIMG[lastimg].name            = IMGName;
  objIMG[lastimg].picsover        = new Array();
  objIMG[lastimg].picsout         = new Array();
  objIMG[lastimg].picsover[0]     = new Image();
  objIMG[lastimg].picsout[0]      = new Image();
  if ((img1 != undefined) && (img2 != undefined))
  {
    objIMG[lastimg].numpicsover     = 1;
    objIMG[lastimg].numpicsout      = 1;
    objIMG[lastimg].picsover[0].src = img1;
    objIMG[lastimg].picsout[0].src  = img2;
  }
  else
  {
    objIMG[lastimg].numpicsover     = 0;
    objIMG[lastimg].numpicsout      = 0;
  }
  lastimg++;
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function AddImage([string] IMGName, [string] img1, [int] type)                       //
  //                                                                                      //
  // This function adds an image animation to the specified image object                  //
  //   IMGName - The image object (defined by the NAME property of the IMG tag)           //
  //   img1    - The image to add to the animation sequence                               //
  //             NOTE: If the image ends with the word LOOP, then the animation will loop //
  //                   back to the first image for the OnMouseOver event. As you can      //
  //                   guess, this only works for images of type 1 (MouseOver)            //
  //   type    - The image type (1=MouseOver, 2=MouseOut)                                 //
  //                                                                                      //
  //   A note about creating an MIA: The images you specify in the CreateSimpleAnim       //
  //   call will the the first images displayed in the OnMouseOver and OnMouseOut         //
  //   events, respectively. Therefore, when you call AddImage to define images for the   //
  //   events of type OUT, you need to remember the first image that will be displayed    //
  //   will be the one defined from the CreateSimpleAnim event. To avoid confusion, it's  //
  //   usually easier to leave out the second and third parameter when you call the       //
  //   function, i.e., CreateSimpleAnim("PIC1");                                          //
  //////////////////////////////////////////////////////////////////////////////////////////
function AddImage(IMGName, img1, type)
{
  if (!UseImgSwapping) return;
  num = GetObjNum(IMGName);
  if (num == -1) return;  // error!
  if ((type == OVER) && (objIMG[num].numpicsover<15))
  {
    objIMG[num].picsover[objIMG[num].numpicsover]     = new Image();
    objIMG[num].picsover[objIMG[num].numpicsover].src = img1;
    objIMG[num].numpicsover++;
  }
  else if (objIMG[num].numpicsout<15)
  {
    objIMG[num].picsout[objIMG[num].numpicsout]     = new Image();
    objIMG[num].picsout[objIMG[num].numpicsout].src = img1;
    objIMG[num].numpicsout++;
  }
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function ChangeImage([string] IMGName, [int] imgnum, [string] img, [int] type)       //
  //                                                                                      //
  // This function changes an image that had already been defined for a particular        //
  // animation sequence.                                                                  //
  //   IMGName - The image object (defined by the NAME property of the IMG tag)           //
  //   imgnum  - The numercial index for the image to change (starting at 0)              //
  //   img     - The new image to use at index [imgnum]                                   //
  //   type    - The image type (1=MouseOver, 2=MouseOut)                                 //
  //////////////////////////////////////////////////////////////////////////////////////////
function ChangeImage(IMGName, imgnum, img, type)
{
  if (!UseImgSwapping) return;
  num = GetObjNum(IMGName);
  if (num == -1) return;  // error!
  if ((type == OVER) && (imgnum+1 > objIMG[num].numpicsover)) return;
  if ((type == OUT)  && (imgnum+1 > objIMG[num].numpicsout))  return;
  if (type == OVER)
    objIMG[num].picsover[imgnum].src = img;
  else
    objIMG[num].picsout[imgnum].src  = img;
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function CopyImage([string] IMGName1, [string] IMGName2, [int] imgnum1, [int] imgnum2//
  //                    [int] type)                                                       //
  //                                                                                      //
  // This function copies the image at index imgnum1 from IMGName1 to index imgnum2 in    //
  // IMGName2.                                                                            //
  //   IMGName1 - The source image object (defined by the NAME property of the IMG tag)   //
  //   IMGName2 - The destination image object (defined by the NAME property of the IMG   //
  //              tag)                                                                    //
  //   imgnum1  - The numercial index to copy from  IMGName1 (starting at 0)              //
  //   imgnum2  - The numercial index to copy to in IMGName2 (starting at 0)              //
  //   type     - The image type (1=MouseOver, 2=MouseOut)                                //
  //////////////////////////////////////////////////////////////////////////////////////////
function CopyImage(IMGName1, IMGName2, imgnum1, imgnum2, type)
{
  if (!UseImgSwapping) return;
  num1 = GetObjNum(IMGName1);
  if (num1 == -1) return;  // error!
  num2 = GetObjNum(IMGName2);
  if (num2 == -1) return;  // error!

  if ((type == OVER) && (imgnum1+1 > objIMG[num1].numpicsover)) return;
  if ((type == OVER) && (imgnum2+1 > objIMG[num2].numpicsover)) return;
  if ((type == OUT)  && (imgnum1+1 > objIMG[num1].numpicsout))  return;
  if ((type == OUT)  && (imgnum2+1 > objIMG[num2].numpicsout))  return;
  if (type == OVER)
    objIMG[num2].picsover[imgnum2].src = objIMG[num1].picsover[imgnum1].src;
  else
    objIMG[num2].picsout[imgnum2].src  = objIMG[num1].picsout[imgnum1].src;
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function SwapImage([string] IMGName1, [string] IMGName2, [int] imgnum1, [int] imgnum2//
  //                    [int] type)                                                       //
  //                                                                                      //
  // This function swaps the image at index imgnum1 from IMGName1 to index imgnum2 in     //
  // IMGName2.                                                                            //
  //   IMGName1 - The first  image object (defined by the NAME property of the IMG tag)   //
  //   IMGName2 - The second image object (defined by the NAME property of the IMG tag)   //
  //   imgnum1  - The numercial index to swap from IMGName1 (starting at 0)               //
  //   imgnum2  - The numercial index to swap from IMGName2 (starting at 0)               //
  //   type     - The image type (1=MouseOver, 2=MouseOut)                                //
  //////////////////////////////////////////////////////////////////////////////////////////
function SwapImage(IMGName1, IMGName2, imgnum1, imgnum2, type)
{
  if (!UseImgSwapping) return;
  num1 = GetObjNum(IMGName1);
  if (num1 == -1) return;  // error!
  num2 = GetObjNum(IMGName2);
  if (num2 == -1) return;  // error!

  if ((type == OVER) && (imgnum1+1 > objIMG[num1].numpicsover)) return;
  if ((type == OVER) && (imgnum2+1 > objIMG[num2].numpicsover)) return;
  if ((type == OUT)  && (imgnum1+1 > objIMG[num1].numpicsout))  return;
  if ((type == OUT)  && (imgnum2+1 > objIMG[num2].numpicsout))  return;
  tmp1 = new Image();
  if (type == OVER)
  {
    tmp1.src = objIMG[num2].picsover[imgnum2].src;
    objIMG[num2].picsover[imgnum2].src = objIMG[num1].picsover[imgnum1].src;
    objIMG[num1].picsover[imgnum1].src = tmp1.src;
  }
  else
  {
    tmp1.src = objIMG[num2].picsout[imgnum2].src;
    objIMG[num2].picsout[imgnum2].src  = objIMG[num1].picsout[imgnum1].src;
    objIMG[num1].picsout[imgnum1].src  = tmp1.src;
  }
  tmp1 = null;
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function SwapSimpleImage([string] IMGName1, [string] IMGName2)                       //
  //                                                                                      //
  // This function swaps all the images for a "simple" animation (one image for           //
  // OnMouseOver and one for OnMouseOut)                                                  //
  // IMGName2.                                                                            //
  //   IMGName1 - The first  image object (defined by the NAME property of the IMG tag)   //
  //   IMGName2 - The second image object (defined by the NAME property of the IMG tag)   //
  //////////////////////////////////////////////////////////////////////////////////////////
function SwapSimpleImage(IMGName1, IMGName2)
{
  SwapImage(IMGName1,IMGName2,0,0,OVER);
  SwapImage(IMGName1,IMGName2,0,0,OUT);
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function AnimateImage([int] x)                                                       //
  //                                                                                      //
  // This function is called internally. I STRONGLY recommend you do NOT call this routine//
  // manually. I don't know why you'd want to...                                          //
  //   x - the image to animate                                                           //
  //                                                                                      //
  //////////////////////////////////////////////////////////////////////////////////////////
function AnimateImage(x)
{
  if (objIMG[x].status == aOVER)
  {
    var sIMG = objIMG[x].picsover[objIMG[x].imgnum].src;
    if ((sIMG.substring(sIMG.length-4,sIMG.length) == "LOOP") && (objIMG[x].nextstatus != 1))
      objIMG[x].imgnum = 0; // loop back to first animation
    if (sIMG.substring(sIMG.length-4,sIMG.length) != "LOOP")
      document.images[objIMG[x].name].src = objIMG[x].picsover[objIMG[x].imgnum].src;
    if (objIMG[x].imgnum == objIMG[x].numpicsover-1)  // at the end of the animation?
    {
      if (objIMG[x].nextstatus==1)   // has the mouse had already moved off the image?
      {
        objIMG[x].nextstatus = 0;
        objIMG[x].status     = aOUT;
        objIMG[x].imgnum     = 0;
        setTimeout("AnimateImage("+x+")",objIMG[x].delay);
      }
      else objIMG[x].status = aDONE;
    }
    else
    {
      objIMG[x].imgnum++;
      setTimeout("AnimateImage("+x+")",objIMG[x].delay);
    }
  } // if status==aOVER
  else if (objIMG[x].status == aOUT)
  {
    document.images[objIMG[x].name].src = objIMG[x].picsout[objIMG[x].imgnum].src;
    if (objIMG[x].imgnum == objIMG[x].numpicsout-1)
    {
      if (objIMG[x].nextstatus==1)   // has the mouse had already moved off the image?
      {
        objIMG[x].nextstatus = 0;
        objIMG[x].status     = aOVER;
        objIMG[x].imgnum     = 0;
        setTimeout("AnimateImage("+x+")",objIMG[x].delay);
      }
      else objIMG[x].status = aDONE;
    }
    else
    {
      objIMG[x].imgnum++;
      setTimeout("AnimateImage("+x+")",objIMG[x].delay);
    }
  }
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function SetImageProp([string] IMGName, [int] delay)                                 //
  //                                                                                      //
  // This function sets the properties of an image object                                 //
  //   IMGName - The image object (defined by the NAME property of the IMG tag)           //
  //   delay   - This is the only property supported at this time. It controls how many   //
  //             milliseconds to wait between picture animations                          //
  //                                                                                      //
  //////////////////////////////////////////////////////////////////////////////////////////
function SetImgProp(IMGName,Delay1)
{
  num = GetObjNum(IMGName);
  if (num == -1) return;  // error!
  objIMG[num].delay = Delay1;
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function DisableAnimation()                                                          //
  //                                                                                      //
  // This function temprarily disables the IAU                                            //
  //                                                                                      //
  //////////////////////////////////////////////////////////////////////////////////////////
function DisableAnimation()
{
  UseImageAnimation = false;
}
  //////////////////////////////////////////////////////////////////////////////////////////
  // Function EnableAnimation()                                                           //
  //                                                                                      //
  // This function enables the IAU                                                        //
  //                                                                                      //
  //////////////////////////////////////////////////////////////////////////////////////////
function EnableAnimation()
{
  if (document.images) UseImageAnimation = true;
}
