

// Code is adapted from http://www.clagnut.com/sandbox/imagefades/

// The lower the number, the faster the picture renders.
var refreshInterval = 60;

// The lower the number, the smoother the picture renders.
var opacityIncrement = 6;

function makeOpaque(imageId) {
  image = document.getElementById(imageId);
  setOpacity(image, 99.999);
}
function makeTransparent(imageId) {
  image = document.getElementById(imageId);
  setOpacity(image, 0);
}

function fade(elementId, startOpacity, finishOpacity) 
{
	objDiv = document.getElementById(elementId);
    objDiv.xOpacity = startOpacity;
	fadeTo(elementId, finishOpacity);
}

function setOpacity(obj, opacity) {
  opacity = (opacity > 100)?99.999:opacity;
  
  var os = obj.style;
  var op = opacity/100;
  
  obj.xOpacity = opacity;
  
  // IE/Win
  os.filter = "alpha(opacity:"+opacity+")";
  
  // Safari<1.2, Konqueror
  os.KHTMLOpacity = op;
  
  // Older Mozilla and Firefox
  os.MozOpacity = op;
  
  // Safari 1.2, newer Firefox and Mozilla, CSS3
  os.opacity = op;
}


function fadeInImage(image) {
  if (image != null) {
    //setOpacity(image, 0);
    image.style.visibility = 'visible';
    fadeTo(image.id,100);
  }
}
function fadeOutImage(image) {
  if (image != null) {
    image.style.visibility = 'visible';
    fadeTo(image.id,0);
  }
}


// given a div, fade it to the given opacity... starting with whatever opacity it has.
function fadeTo(objId, endOpacity) 
{
    var objDiv = document.getElementById(objId);
    
	if (objDiv.xOpacity >= 0) {
	    currOp = objDiv.xOpacity;
	} else {
	    currOp = 50; // is this right???
	    objDiv.xOpacity = 50;
	    printToStatusBar(objDiv.id + " !!!!!! opacity MISSING... setting to 50");
	}
	
    printToStatusBar("fading " +objId + " from " + currOp + " to " + endOpacity);
    
	if (currOp > endOpacity) {
		fadeOut2(objId,currOp,endOpacity)
	}
	else {
		fadeIn2(objId,currOp,endOpacity);
	}
}

// fade out from curr to end.
function fadeOut2(objId,curr,end)
{
	if (document.getElementById) {
		if (curr > end) {
		    setOpacity(document.getElementById(objId), curr);
		    curr -= opacityIncrement;
		    window.setTimeout("fadeOut2('"+objId+"',"+curr+","+end+")", refreshInterval);
		}
	}
}

// fade in from current opacity to end.
function fadeIn2(objId,curr,end)
{
	if (document.getElementById) {
		if (curr < end) {
		    setOpacity(document.getElementById(objId), curr);
		    curr += opacityIncrement;
		    window.setTimeout("fadeIn2('"+objId+"',"+curr+","+end+")", refreshInterval);
		}
	}
}


//window.onload = function() {initImage()}
