/*
  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.
  <!-- to add a tool or button, do the following:
     1 - you need 4 images - named <name>_01, <name>_02, <name>_03, <name>_04
       01 is default - normal button/tool waiting for action
       02 is mouseover up - highlighted - but not clicked
       03 is depressed - not highlighted, but has been clicked an active tool
       04 is depressed nouseover - highlighted and is active tool or depressed button

     2 - add an IMG tag with the following elements:
     alt="<some alternative text to show if image cannot be shown in browser and for hover text for browser that support>"
     name="t_<cardinal name of image>" or "b_<cardinal name of image>" - b for button and t for tool.  The cardinal
          name of the image is the name of the image file w/o the numbers and .gif
     src="<full source name of first image>"
     onClick="LIS_Buttons('down', <index of tool or button in page>);"
          The index of the image starts at 0 and each additional button adds 1.
          To add tool functionality - like if pressing the button or tool does something, then add the appropriate
             javascript after the semi-colon.
     onMouseOver="LIS_Buttons('over', <index of tool or button in page>);"
     onMouseOut="LIS_Buttons('out', <index of tool or button in page>);
     border="0"  sets the image to not have a hover border - not really important, but good if you are going to also create a href around image for some reason.

     nb - 1: all images must be same image format (gif, jpg, etc) - the type is set at the top of the javascript - now is gif, but could be jpg.
          2: if you are inserting images in middle of the list of images - need to bump up the image indexes in all subsequent tags.
     -->
  @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++) {
               if (isButton(d.images[x]) || isTool(d.images[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';
     }
   }

   function isButton(img) {
       return (img.name.length > 0 && img.name.substring(0,2) == "b_");
   }

   function isTool(img) {
       return (img.name.length > 0 && img.name.substring(0,2) == "t_");
   }

   // 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();
             }
          }
       }
    }
  }

