/* -------------------------------------------------------------------
** Function:  findObj(n, d)
**
** Find and return the element whose name is specified by "n"
** within optional document (or layer) specifier "d".  If "n" contains
** a "?" character, then the format of "n" is <object_name?frame_name>.
**
** Returns the element object if found or null if not.
** ------------------------------------------------------------------- */
function findObj(n, d)
{
   var p, i, x;  

   // if no document (or layer) supplied, then use current document.
   if (!d)
      d = document; 
   
   // If within a frame, get the frame index and
   // assign the frame window to "d" and crop "n" just before the "?".
   if ((p = n.indexOf("?")) > 0 && parent.frames.length)
   {
      d = parent.frames[n.substring(p+1)].document; 
      n = n.substring(0,p);
   }
  
   // If the element is not within the frame,
   // then find try to find it in the whole document.
   if (!(x=d[n]) && d.all) 
      x = d.all[n]; 
  
   // if element was not found within the document,
   // look for the element in forms (if any).   
   for (i = 0; !x && i < d.forms.length; i++) 
      x = d.forms[i][n];
   
   // if element was not found within the forms,
   // look for the element in layers (if any).
   for (i = 0; !x && d.layers && i < d.layers.length; i++) 
      x = findObj(n,d.layers[i].document); 

   return x;  // return the element object.
}

/* -------------------------------------------------------------------
** Function:  swapImage(img_name1, swap_img_src1, [img_name2, swap_img_src2], ...)
**
** Replace the image src of each image_name with the corresponding
** swap_img_src.  Create the "aSwapElements" array for later
** restoring the original image source with the function "swapImageRestore
**
** Returns: <nothing>
** ------------------------------------------------------------------- */
function swapImages()
{
   var i, j = 0, x, a = swapImages.arguments;

   // Create the "aSwapElements" array
   document.aSwapElements = new Array;

   for(i = 0; i < a.length; i += 2)
      if ((x = findObj(a[i])) != null)
      {
         // save reference to the "aSwapElements" array
         // This array is used by swapImgRestore() to restore
         // the old src value.
         document.aSwapElements[j++] = x;

         // if oldSrc property does not exist, then
         // create it and copy the original source.
         if(!x.oldSrc)
            x.oldSrc = x.src;
         x.src = a[i+1];
      }
}

/* -------------------------------------------------------------------
** Function:  swapImageRestore()
**
** Loop through the array "aSwapElements" created by "swapImage" and
** restore all images references store there.
** ------------------------------------------------------------------- */
function swapImagesRestore()
{
   var i, x, a=document.aSwapElements;

   for(i = 0; a && i < a.length && (x = a[i]) && x.oldSrc; i++)
      x.src = x.oldSrc;
}

/* -------------------------------------------------------------------
** Function:  preloadImages(image_src1, [image_src2], ...)
**
** Loop through the argument array which contains all the images
** to be preloaded.  If the image src path contain an "#" at the
** beginning of the line, then it is assumed that one of the images
** for one reason or another has been temporarily commented out.
** ------------------------------------------------------------------- */
function preloadImages()
{
   var d = document;

   if (d.images)
   {
      if(!d.aPreloadedImages)
         d.aPreloadedImages = new Array();

      var i, j = d.aPreloadedImages.length, a = preloadImages.arguments;
      for (i = 0; i < a.length; i++)
         if (a[i].indexOf("#") != 0)
         {
            d.aPreloadedImages[j] = new Image;
            d.aPreloadedImages[j++].src=a[i];
         }
   }
}

