/*
  aimsLISButtons supports the application button and tool image manipulation.
  It includes support for tracking the currently selected tool and preloading
  the images.  The preloading part is an adaptation of the web-standard dreamweaver/multimedia 
  image rollover preloading.
  
  @author Cord Thomas, Lupine Information Systems
  @date 10.1.2002
 */

  var iCnt = 4;
  var fExt = ".gif";  // This is the type of image this page supports.
  var fImagesDir = "Images/";

  function LIS_Buttons(event, imgIdx) {
    var d=document;
    var imgName = d.images[imgIdx].name;
    var imgSrc = d.images[imgIdx].src;
    var imgState = getButtonState(imgSrc);
    try {
      if (event == "over") {
        //alert (imgName + ' ' + imgSrc + ' ' + imgState);
        if (imgState > 2)
          d.images[imgIdx].src = d.MM_p[(imgIdx * iCnt) + 4].src;
        else
          d.images[imgIdx].src = d.MM_p[(imgIdx * iCnt) + 2].src;
        } else if (event == "out" ) {
          if (imgState > 2)
            d.images[imgIdx].src = d.MM_p[(imgIdx * iCnt) + 3].src;
       	  else
            d.images[imgIdx].src = d.MM_p[(imgIdx * iCnt) + 1].src;
          } else if (event == "down") {
          var iImages = d.images.length;
          // If this is a button, then do not unclick other tools, otherwise unclick other tools
          var imgType = imgName.substr(0,1);
          if (imgType=="b") {
            // temporarily set the button to be depressed
            d.images[imgIdx].src=d.MM_p[(imgIdx * iCnt) + 4].src;
            // alert (' We do some button action');
            d.images[imgIdx].src=d.MM_p[(imgIdx * iCnt) + 2].src;
          } else {
            // We have a tool, so reset all other images
            for (var x=0; x<iImages; x++) {
               d.images[x].src=d.MM_p[(x * iCnt) + 1].src;
            }
            // then set the selected one to down.
            d.images[imgIdx].src = d.MM_p[(imgIdx * iCnt) + 4].src;
         }
       }
     } catch (eButtons) {
       window.status = 'Error managing buttons.  Please make sure all buttons images are available.  LIS_Buttons requires 4 buttons for each entry - 01, 02, 03, and 04';
     }
   }

   // Returns whether the button is a tool that is
   // currently depressed.
   function getButtonState(imgSrc) {
     var dotPos = imgSrc.lastIndexOf('.');
     var imgState = imgSrc.substring(dotPos-2, dotPos);
     return parseInt(imgState);
   }

   // Preloads all button images.
   function LIS_preloadImages() {
     var d=document;
     var imgName;
     if(d.images){
       if(!d.MM_p)
         d.MM_p=new Array();
       var i;
       for (i=0; i<d.images.length; i++) {
          for (j=1; j <= iCnt; j++) {
             try {
               imgName = d.images[i].name.substring(2, d.images[i].name.length);
               d.MM_p[(i * iCnt) + j]=new Image;
               d.MM_p[(i * iCnt) + j].src=fImagesDir + imgName + "_0" + j + fExt;
             } catch (eLoad) {
               window.status = eLoad.toString();
             }
          }
       }
    }
  }
