  
  /*  Your are permitted to reuse this code as long as the following copyright
      notice is not removed:

      The image handling code is copyright 1998 by insideDHTML.com, LLC. More information about this
      code can be found at Inside Dynamic HTML: HTTP://www.insideDHTML.com
      
      modification by Alex Fung <Alex.Fung.Ho-san@graduate.hku.hk>: 
        remove onload assignment.
      April 2002 modification by Alex Fung <Alex.Fung.Ho-san@graduate.hku.hk>: 
        fixed problems:
          1) nothing happens when no image has order=loadCounter
          2) finished is set incorrectly
          3) maxID variable added
          4) note order >= maxID will be ignored unless explicitly loadImage()ed
             
  */

  var lock=false;
  var imageList = document.images;
  var finished = false;

  function loadImage(loadCounter) {
    // optionalgroup argument
    group = arguments[1]
    // Check if critical section is locked
    if (!lock) {
      lock=true  // lock section
      var img=null, maxID=10000, nextID=maxID, next=null, block=false, matchGroup = false, order
      var imageLength = document.images.length;
      while(1) { // jump back here once if for loop found nothing
        for (var i=0; i < imageLength; i++) {
          img = imageList[i]; // cache image to manipulate
          order = parseInt(img.getAttribute("_ORDER"));
          matchGroup = (group == img.getAttribute("_GROUP"));
          if ((order==loadCounter) && (matchGroup)) 
            if (next==null) 
              next = img; // first img is treated after for loop
            else
              img.src = img.getAttribute("_SRC");           
          // Get Next ID
          if ((order>loadCounter) && (nextID>order) && (matchGroup)) nextID=order;   
          // Clear past onload events - necessary for animated gifs
          if (img.complete) img.onerror = img.onload = null;
        }
        if (next != null) break; // leave if an img found to load
        if (nextID == maxID) break; // if none >= loadCounter, leave
        // if none at loadCounter but still some at nextID, loop back
        loadCounter=nextID;
        nextID=maxID;
      }
      finished = (nextID==maxID);  // fixed
      lock=false; // unlock section
      if (next!=null) {
        if (nextID < maxID) { // don't act if ==
          //if (group==null)    
          //  next.onload = new Function("loadImage("+(nextID)+", null)")
          //else 
          //  next.onload = new Function("loadImage("+(nextID)+", '" + group + "')")
          next.onerror = next.onload = new Function("loadImage("+(nextID)+((group==null)? ", null)" : (", '" + group + "')")))
          // Also check failure
        }
        next.src = next.getAttribute("_SRC")
      }
    }  
    else
      if (!finished) // Critical section locked - try again
        if (group==null)
          setTimeout("loadImage("+loadCounter+", null)",100)
        else
          setTimeout("loadImage("+loadCounter+", '" + group + "')",100)
  }

  //  window.onload = new Function("loadImage(1)")
 
  // Image order defined by _ORDER, _GROUP, and _SRC attributes on the IMG element.

