// Sliding Banner JavaScript

/* ************************************************************************************ */
/*                                Configuuration Section                                */
/* ************************************************************************************ */
 // Set the startup interval here.  There are actually two startup intervals: one delay
 // before starting the scroll, and a retry delay, in case the page is not ready when
 // the initial delay expires.  "Not ready" means that not all of the banner are loaded.
 // The times specified here are in milliseconds (1000 = 1 second).
  bStartupDelay = 2000;
  bStartupRetry = 500;

 // Set the scroll speed.  This is also in milliseconds, though the time specified should
 // generally be much shorter.  This is the interval between image moves.
  bScrollSpeed = 50;

 // Set the scroll direction.  The scroller can only move horizontally; maybe someday
 // we'll create on that movea vertically as well.  For now, there are only two choices.
 // NOTE: FOR NOW, THIS ONLY HANDLES 'LEFT' SCROLLING; WE'LL ADD THE REST LATER.
  bScrollDirection = 'left';        // 'left' or 'right'

 // Set the popu delay.  After the visitor mouses over an image, this delay prevents
 // popups from appearing in rapid-fire.  The cursor must remain on one image for at
 // least this number of milliseconds before the popup appears.
  bPopupDelay = 1000;

 // THE FOLLOWING VALUES *MUST* MATCH THE CORRESPONDING VALUES IN THE PHP CODE.
 // Set the image information.  For most applications, this information will never 
 // change.  There may, however, be instances where this will be different.
  bImageDirectory = '/banner/images';
 // The first letter of each image file name determines its purpose: either small, for
 // thumbnails or large for expanded images.  All images in the image directory (above)
 // must start with one or the other of these letters, and they must be in the same 
 // case.  Every thumbnail image must have a matching large image.
  bLargeImageInitial = 'M';
  bThumbImageInitial = 'T';

/* ************************************************************************************ */
/*                       DON'T CHANGE ANYTHING BEYOND THIS POINT!                       */
/* ************************************************************************************ */
 // Operational values
  var bPics;                        // Picture array
  var bInterval;                    // Timeout and Interval handle
  var bDiv;                         // Banner div element
  var bCnt;                         // Number of images in banner div
  var bOffset;                      // Distance to move images
  var bImg;                         // Preloaded popup image

 // Popup the large view window.
  var bannerPopup = new TipObj('bannerPopup');
  with (bannerPopup) {
   tips.popup = new Array(-100,10,100,'','');
   template = '<div id="bannerPopup"><img src="'+bImageDirectory+'/%3%" alt="" border="0"><h6>%4%</h6></div>';
   tipStick = 1;
  }
/*
  var bSaveOnMouseMove = document.onmousemove;
  if (isNS4) document.captureEvents(Event.MOUSEMOVE);
  document.onmousemove = function(evt) {
    bannerPopup.track(evt);
    if (bSaveOnMouseMove)   bSaveOnMouseMove();
    if (isNS4) return document.routeEvent(evt);
  }
*/
  function bannerShowImage() {
    clearInterval(bInterval);
    bannerPopup.show('popup');
  }

 // Perform the scroll operation.  This is pretty simple: move the images by the offset
 // amount.  Once one is out of view, move it behind the last image.
  function bannerScroll() {
    for (i=0; i<bCnt; i++) {
      bThisPos = bPics[i][3] + bOffset;
      if (bThisPos == -(bPics[i][2])) {
        x = 0;
        for (j=0; j<bCnt; j++) {
          if (j!=i)   x += bPics[j][2];
        }
        bThisPos = x;
      }
      bPics[i][3] = bThisPos;
      bPics[i][0].style.left = bThisPos+'px';
    }
  }

 // Pause and restart the banner.  These functions are called when the visitor mouses
 // over and/or out of one of the images in the banner div.
  function bannerPause(img) {
    bannerPopup.hide();
    clearInterval(bInterval);
    bannerPopup.tips.popup[3] = img;
    bImg = new Image();
    bImg.src = bImageDirectory+'/'+img;
    txt = img.substring(1,img.indexOf('.')).replace(/_/g,' ');
    bannerPopup.tips.popup[4] = txt;
    bInterval = setInterval('bannerShowImage()',bPopupDelay);
    return false;
  }
  function bannerPlay() {
    bannerPopup.hide();
    clearInterval(bInterval);
    bInterval = setInterval('bannerScroll()',bScrollSpeed);
    return false;
  }

 // Start the banner process.  For this, we scann the banner DIV (we assum to know the
 // name) for installed images.  If the images are completely loaded, we can start the
 // scroll; otherwise, reset the timer, and try again.
  function bannerStart() {
    bDiv = getRef('bannerDiv',null);
    if (bDiv) {
      bPics = new Array();                // reset the picture array
      bImgs = bDiv.getElementsByTagName('img');
      bCnt = bImgs.length;      // get number of images in div
      for (i=0; i<bCnt; i++) {
        bThis = bImgs[i];         // Get the image element
        // If this image is not completely loaded, we can't start yet.
        if (bThis.complete==false) {
          bInterval = setTimeout('bannerStart()',bStartupRetry);
          bPics = null;
          return false;
        }
        // Get the name (URL) and size of the image.
        bPics[i] = Array(bThis,bThis.src, bThis.width, parseInt(bThis.style.left));
      }
      // We're done here, so set the direction and start scrolling.
      bOffset = (bScrollDirection='left' ? -1 : 1);
      bInterval = setInterval('bannerScroll()',bScrollSpeed);
    }
  }

 // Set up onload stuff, so the banner starts shortly after the page is loaded.
  var bSaveOnload = window.onload;
  window.onload = function() {
    bInterval = setTimeout('bannerStart()',bStartupDelay);
    if (bSaveOnload)    bSaveOnload();
  }
