// Fade in script

var imgVector = new Array();
var hrefVector = new Array();
function SetOpacity(object,opacityPct)
{
  // IE.
  object.style.filter = 'alpha(opacity=' + opacityPct + ')';
  // Old mozilla and firefox
  object.style.MozOpacity = opacityPct/100;
  // Everything else.
  object.style.opacity = opacityPct/100;
}
function ChangeOpacity(id,msDuration,msStart,fromO,toO)
{
  var element=document.getElementById(id);
  var opacity = element.style.opacity * 100;
  var msNow = (new Date()).getTime();
  opacity = fromO + (toO - fromO) * (msNow - msStart) / msDuration;
  if (opacity<0) 
    SetOpacity(element,0)
  else if (opacity>100)
    SetOpacity(element,100)
  else
  {
    SetOpacity(element,opacity);
    element.timer = window.setTimeout("ChangeOpacity('" + id + "'," + msDuration + "," + msStart + "," + fromO + "," + toO + ")",1);
  }
}
function FadeInImage(backgroundID, foregroundID, newImage, fadeMS)
{
  var foreground=document.getElementById(foregroundID);
  if (backgroundID)
  {
    var background=document.getElementById(backgroundID);
    if (background)
    {	// swap the foreground image to the background
      background.src = foreground.src;
    }
  }
  SetOpacity(foreground,0);	// make the foreground invisible
  foreground.src = newImage.src;	// load up the new image
  if (foreground.timer) window.clearTimeout(foreground.timer); 
  var startMS = (new Date()).getTime();
  foreground.timer = window.setTimeout("ChangeOpacity('" + foregroundID + "'," + fadeMS + "," + startMS + ",0,100)",1);

}
function cycleImages (backImg,foreImg,fadeMS,steadyMS,foreGroundIndex) {
	if (++foreGroundIndex >= imgVector.length) foreGroundIndex=0;
	FadeInImage(backImg,foreImg,imgVector[foreGroundIndex],fadeMS);
	cycleTimer = window.setTimeout("cycleImages('" + backImg + "','" + foreImg + "'," + fadeMS + "," + steadyMS + "," + foreGroundIndex + ")",steadyMS+fadeMS);
  	// set the new href
  	document.getElementById('imglink').href = hrefVector[foreGroundIndex];
}
function imageCycleStart(backImg,foreImg,imageBase,imageNum,fadeMS,steadyMS) {
  	var startMS = (new Date()).getTime();
	for (i = 0; i < imageNum; i++) {
		imgVector[i] = new Image();
		imgVector[i].src = imageBase + (i+1) + ".jpg";
	}
	var elapsedMS = (new Date()).getTime() - startMS;
	cycleTimer = window.setTimeout("cycleImages('" + backImg + "','" + foreImg + "'," + fadeMS + "," + steadyMS + ",0)",elapsedMS+10>=steadyMS?10:steadyMS-elapsedMS);
}

//
// Called as:
// imageCycleStart('backgroundId', 'foregroundId', imagebase, imageNum, fadeMS, steadyMS)
// where
// fadeMS = time to fade the image in milliseconds
// steadyMS = time to keep image steayd before next fade
// (so total cycle time is fadeMS + steadyMS)
//  <img id='backgroundId' alt='description'>
//  <img id='foregroundId' src='../practices/practice/img1.jpg' alt='description'>
// imagebase is the base name for the image e.g. "../practices/mypractice/img"
// imageNum is the number of images. Image names are then constructed as imagebase + imagenumber + .jpg
// with image number 1 as the first image.
