// Original code taken from http://clagnut.com/sandbox/imagefades/
// altered by Jason Morrill
// December 2005

/* The initImage function makes the photo completely tranpsarent 
 * by using the setOpacity function to set its opacity to zero. 
 * The photo can then be made visible and faded in using the 
 * fadeIn function
 */
 
function InitImage(imageId) {
  image = document.getElementById(imageId);
  SetOpacity(image, 0);
  image.style.visibility = 'visible';
  FadeIn(imageId,0);
}


/* The setOpacity function is passed an object and an 
 * opacity value. It then sets the opacity of the supplied 
 * object using four proprietary ways. It also prevents a 
 * flicker in Firefox caused when opacity is set to 100%, 
 * by setting the value to 99.999% instead.
 */

function SetOpacity(obj, opacity) {
  opacity = (opacity == 100)?99.999:opacity;
  
  // IE/Win
  obj.style.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  obj.style.KHTMLOpacity = opacity/100;
  
  // Older Mozilla and Firefox
  obj.style.MozOpacity = opacity/100;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  obj.style.opacity = opacity/100;
}

function GetOpacity(obj) {
 	// IE/Win
	if (obj.style.filter.alpha.opacity) { return obj.style.filter.alpha.opacity; }
 	// Safari<1.2, Konqueror
	if (obj.style.KHTMLOpacity) { return obj.style.KHTMLOpacity; }
 	// Older Mozilla and Firefox
	if (obj.style.MozOpacity) { return obj.style.MozOpacity; }
 	// Safari 1.2, newer Firefox and Mozilla, CSS3
	if (obj.style.opacity) { return obj.style.opacity; }
	return 0;
}

/* The fadeIn and FadeOut function uses a Timeout to call itself every 
 * 100ms with an object Id and an opacity. The opacity is 
 * specified as a percentage and increased 10% at a time. 
 * The loop stops once the opacity reaches 100%:
 */

function FadeIn(objId, opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity <= 100) {
      SetOpacity(obj, opacity);
      opacity += 10;
      window.setTimeout("FadeIn('"+objId+"',"+opacity+")", 100);
    }
  }
}

function FadeOut(objId, opacity) {
  if (document.getElementById) {
    obj = document.getElementById(objId);
    if (opacity >= 0) {
      SetOpacity(obj, opacity);
      opacity -= 10;
      window.setTimeout("FadeOut('"+objId+"',"+opacity+")", 100);
    }
  }
}


/* 
 * Crossfade from the current image to the next
 */
function CrossFade(currId, nextId, opacity) {
  if (document.getElementById) {
  	if (opacity == null) { opacity = 100; }
    currObj = document.getElementById(currId);
    nextObj = document.getElementById(nextId);
	SetOpacity(currObj, opacity);
	SetOpacity(nextObj, (100-opacity));
	if (opacity > 0) {
	    window.setTimeout("CrossFade('"+currId+"','"+nextId+"',"+(opacity-10)+")", 100);	
	}
  }
}