var banImages = [];
var banLinks = [];
var homeImages = [];
var homeLinks = [];
function preloadImages() {
  for (var im,n=i=0; im=banImg[i]; i++) {
    if (im.length>0) {
      banImages[n] = new Image();
      banImages[n].src = im;
      banLinks[n] = banLnk[i];
      n++;
    }
  }
  for (var im,n=i=0; im=homeImg[i]; i++) {
    if (im.length>0) {
      homeImages[n] = new Image();
      homeImages[n].src = im;
      homeLinks[n] = homeLnk[i];
      n++;
    }
  }
}
    
function initBannerRotate(bannerId, imgList, linkList) {
  // if there are no images, do not execute
  if (imgList.length<1) return;
  // get banner anchor tag
  var ban = document.getElementById(bannerId);
  if (!ban) return;
  // get the image inside it
  var img = ban.firstChild;
  while (img.nodeName!='IMG' && img.nextSibling) {img=img.nextSibling;}
  if (img.nodeName!='IMG') return;
  
  var num = Math.floor(Math.random() * imgList.length); // pick a random slide
  img.src = imgList[num].src;
  ban.href = linkList[num]; 
  if (imgList.length > 1) {
    setInterval(execNextSlide, banSpd || 4000);
  }
  function execNextSlide() {
    nextSlide(bannerId, ban, img, imgList, linkList);
  } 
}
                                                                                                                       
function nextSlide(bannerId, ban, img, imgList, linkList) {
  // Only rotate if other slides exist
  if (!(imgList.length > 1)) return;
  // grab and use random slide (not the current slide)
  var num;
  do {
    num = Math.floor(Math.random() * imgList.length); // pick a random slide
  } while (img.src==imgList[num].src);
  img.src = imgList[num].src;
  ban.href = linkList[num];
  /* THIS SECTION USED FOR CHOOSING THE NEXT SLIDE IN SEQUENCE
  // loop through images. If one is found that equals the current image,
  // grab the next one and replace it.  var n=0;
  for (var im; im=banImages[n]; n++) {
    if (img.src==im.src) break;
  }
  // if a match was found, advance to the next match (wrapping if nec.)
  if (banImages[n]) {
    n++;
    if (!banImages[n]) {n=0;}
    // set new values for img (IMG tag) and ban (A tag)
    img.src=banImages[n].src;
    ban.href=banLinks[n];
  }*/
}
  
preloadImages();
initBannerRotate('banner1', banImages, banLinks);
initBannerRotate('banner2', banImages, banLinks);
if (homeImages.length>0) {
  initBannerRotate('bannerH', homeImages, homeLinks);
}
  

