// JavaScript Document
var arrIdx = 0;
var bannerArr = new Array();
var bannerObj;

function BannerObj()
{
  var imageSrc = "";
  var url = "";
  var image = null;
  var imageAlt = "";
  var imageLoaded = false;
}

// Browser details
var agt = navigator.userAgent.toLowerCase();
var isIe = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var timer;
var currIdx = 0;
var nextIdx = 0;
var onOff = 1;
var timeoutValue = 5000; //10 seconds
var loopCount = 3; // 0=NoLoop; n=LoopTimes; -1=InfiniteLoop; 
var imagesLoaded = 0;

function stop()
{
  onOff = 0;
  clearTimeout(timer);
  
  //stop always on the first bannerImage
  currIdx = 0;
  showCurrBanner();
}

function play()
{
  if(loopCount != 0) {
    onOff = 1;
    nextBanner();
  }
}

function preloadComplete(idx)
{
  bannerArr[idx].imageLoaded = true;
}

function resetTimer()
{
  clearTimeout(timer);
  if (onOff) {
    timer = setTimeout('nextBanner()', timeoutValue);
  }
}

function waitForCurrBanner()
{
  /* Show the current banner */
  if (showCurrBanner()) {
    preloadNextBanner();
    resetTimer();
  } else {
    /*
    * The current banner image isn't loaded yet. 
    * Set a short timer just to wait until the current banner image is loaded.
    */
    clearTimeout(timer);
    timer = setTimeout('waitForCurrBanner()', 500);
    return 0;
  }
}

function nextBanner()
{
  /* Go to the next location */
  currIdx = nextIdx;

  /* Show the current photo */
  if (!showCurrBanner()) {
    waitForCurrBanner();
    return 0;
  }

  preloadNextBanner();
  resetTimer();
}

function preloadNextBanner()
{
  /* Calculate the new next location */
  if (loopCount == 0) {
    stop();
  } else {
    nextIdx = parseInt(currIdx) + 1;
    if (nextIdx >= bannerCount) {
      nextIdx = 0;
      loopCount -= 1;
    }
  }

  /* Preload the next photo */
  preloadBanner(nextIdx);
}

function showCurrBanner()
{
  /*
  * If the current banner is not completely loaded don't display it.
  */
  if (bannerArr[currIdx].image == null ||
      bannerArr[currIdx].imageLoaded == false) {
    preloadBanner(currIdx);
    return 0;
  }

  if (isIe) {
    document.links("bannerUrl").href = bannerArr[currIdx].url;
  } else {
    document.links["bannerUrl"].href = bannerArr[currIdx].url;
  }
  var bannerImg = document.images["bannerImage"];
  if(isIe) {
    bannerImg.style.filter = "progid:DXImageTransform.Microsoft.Fade(duration=2)";
    bannerImg.filters[0].Apply();
  }
  bannerImg.src = bannerArr[currIdx].image.src;
  bannerImg.alt = bannerArr[currIdx].imageAlt;
  if(isIe) {
    bannerImg.filters[0].Play();
  }

  return 1;
}

function preloadBanner(idx)
{
  /* Load the next banner image */
  if (idx < bannerCount && 
      bannerArr[idx].image == null) {
    var newImage = new Image();
    newImage.onLoad = preloadComplete(idx);
    newImage.src = bannerArr[idx].imageSrc;
    bannerArr[idx].image = newImage;
  }
}
