/*
**  lib_images.js - v1.53
**  written by Joshua J Baker
**  josh@cobaltcreative.com
**
**  Methods
**    switchImage(image1, image2)
**      - switches the image src's.
**    addImage(id, src, trigger);
**      - adds an image to the imgObj.
**    getImage(id)
**      - return the image object refered to by id.
**    registerLayer(layer, ...)
**      - allows for getImage to access sub layers (netscape only, IE ignores).
**   
**  Events
**    onImageLoad(loadedImages, totalPreImages, id)
**    window_onload()
**
**  Notes
**    use the window_onload() event instead of the window.onload.
**
**  Example
**     <!--<script src="lib/lib_stylesheet.js"></script>
**     registerLayer('slidingThingy')
**     addImage('teamButtonover', 'images/team_members-over.gif')
**     onmouseout="switchImage('company','companyoff')" onmouseover="switchImage('company','companyover')"><img border="0" name="company"
**     <div id="slidingThingy"><a href="javascript:slidingThingyClick()"><img border="0" name="circle" src="images/hollow_circle.gif" WIDTH="25" HEIGHT="15"></a></div>-->
**
*/

var imgObj = new Object();
var setLoader = false;
var layArr = new Array();
var loadingArray = new Array();
var totalImages = 0;
var loadedImages = 0;
var endLoad = false;
var onLoadMethods = new Array("window_onload", "Window_Onload", "windowonload", "WINDOWONLOAD");
function onImageLoad() {/* prototype */}

for (var i=0;i<onLoadMethods.length;i++)
{
   eval("function " + onLoadMethods[i] + "(){}");
}

function getImage(img)
{
  if (document.images[img])
  {
    return document.images[img];
  }
  if (document.layers)
  {
    for (var i=0;i<layArr.length;i++)
    {
      var lay = "", tempLay, layLength = 0;
      for (var layLength=1;layLength<26;layLength++)
      {
        if (layArr[i][j]==null) break;
      }
      for (var j=0;j<layLength;j++)
      {
        tempLay = lay + ".document.layers['" + layArr[i][j] + "']";
        if (!eval(tempLay.substring(1)))
        {
          continue;
        }
        lay = tempLay;
      }
      if (lay != "")
      {
        lay = eval(lay.substring(1));
        if (lay.document.images[img])
        {
          return lay.document.images[img];
        }
      }
    }
  }
  if (imgObj[img])
  {
    return imgObj[img].image;
  }
  return false;
}

function switchImage(img1, img2)
{
  
  if (document.images && document.isLoaded)
  {
    img1 = getImage(img1);
    img2 = getImage(img2);
    if (img1&&img2)
    {
      img1.src = img2.src;
    }
  }
}

function addImage(id, src, trigger)
{
  if (arguments.length >= 2 && id != "")
  {
    imgObj[id] = new Object();
    imgObj[id].id = id;
    imgObj[id].trigger = trigger;
    imgObj[id].image = new Image();
    imgObj[id].image.src = src;
    imgObj[id].image.onerror = new Function("window.status = 'Error loading image: [" + id + "]';endLoad = true;");
    imgObj[id].image.onabort = new Function("window.status = 'Loading aborted at image: [" + id + "]';endLoad = true;");
    imgObj[id].image.onload  = new Function("eval(\"imgLoaded('" + id + "')\");");
    totalImages++;
  }
}

function registerLayer()
{
  layArr[layArr.length] = arguments;
}

function imgOnload()
{
  window.loaded = true;
  document.isLoaded = true;
  for (var i=0;i<onLoadMethods.length;i++)
  {
    eval(onLoadMethods[i] + "();");
  }
}

function imgLoaded(id){
  if (setLoader)
  {
    setTimeout("imgLoaded('" + id + "')", 1);
    return;
  }
  setLoader = true;
  loadedImages++;   
  onImageLoad(loadedImages, totalImages, id);
  setLoader = false;
  eval(imgObj[id].trigger);
}

window.onload = imgOnload;

