﻿// JScript File
// copyright Brazao Netwerks
// used under licence

// determine browser by fairly rudimentary method
bIE = (document.getElementById && document.all);
bNS = (document.getElementById && !bIE);

bFade = bIE || bNS;

iID = 0;
iShow = 0;
iInterval = 0;
iCurrentOpacity = 0;

shows = new Array();
currentshow = null;

oNew = null;
oOld = null;

function RegisterSlideShow(sDiv)
{
  shows[iID] = new SlideShow(sDiv, iID); 
  iID++;
}

function SlideShow(sDiv, iNumber)
{
  this.oDiv = document.getElementById(sDiv);
  
  this.Number = iNumber;
  
  this.Images = this.oDiv.getElementsByTagName('img');
  this.ImageCount = this.Images.length;
  this.CurrentImage = 0;
  this.NextImage = 0;
  
  this.SetNextImage = SetNextImage;
  this.SwapComplete = SwapComplete;
}

function SetNextImage()
{
  this.NextImage = this.CurrentImage + 1;
  if (this.NextImage == this.ImageCount) this.NextImage = 0;
}

function SwapComplete()
{
  this.CurrentImage = this.NextImage;
}

function StartSlideShow()
{
  window.setTimeout(SwapImage, iHoldDuration);
}

function SwapImage()
{
  if (iID == 0) return; // don't start if there aren't any shows
  
  currentshow = shows[iShow];
  
  if (currentshow.ImageCount < 2)
  {  
    NextSwapImage();
    return;
  }
  
  currentshow.SetNextImage();
  
  if (oOld) oOld.style.zIndex = 0;
  
  oOld = currentshow.Images[currentshow.CurrentImage]; 
  oNew = currentshow.Images[currentshow.NextImage];
  
  if (bFade) setOpacity(oNew, 0);
  
  oNew.style.zIndex = 100;
  oOld.style.zIndex = 99;
  
  if (bFade) 
  {
    iCurrentOpacity = 0;
    iInterval = window.setInterval('doFade()', iFadeDelay);    
  }
  else 
  {
    NextSwapImage();
  }

  return true;
}

function NextSwapImage()
{
  // set current image
  currentshow.SwapComplete();
  
  iShow++;
  if (iShow == iID) iShow = 0;
  
  window.setTimeout(SwapImage, iHoldDuration);
}

function doFade() 
{
    // increment opacity
    iCurrentOpacity += iStep;

    // apply opacity to object
    setOpacity(oNew, iCurrentOpacity);

    // remove interval if over/equal to 100
    if (iCurrentOpacity >= 100) 
    {
      window.clearInterval(iInterval); 
	  NextSwapImage();
    }
}

function setOpacity(oImg, iOpacity) 
{
    if (iOpacity >= 100)
    {
      if (bIE) oImg.style.filter = '';
	  if (bNS) oImg.style.opacity = 1;
	  return;
    }  

    if (bIE) oImg.style.filter = 'alpha(Opacity=' + iOpacity.toString() + ')';
    if (bNS) oImg.style.opacity = (iOpacity / 100);
}
