//  Form --  http://brainerror.net/scripts/javascript/blendtrans/

function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

// Change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

// If an element is invisible, make it visible, else make it invisible
function shiftOpacity(id, millisec) {
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

// Original blend where new image fades in over the old image - if new image
// is smaller, the effect may be jaggy depending on the timing
function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image (foreground) transparent
	changeOpac(0, imageid);
	
	//set new image
	document.getElementById(imageid).src = imagefile;

	//fade in new image over old image in background
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
	
	// RF: Added
	//set the new image as its own background in case they new image is 
	//smaller than the previous image - don't let the old background show
	//delay it so the fade effect will be shown
	//document.getElementById(divid).style.backgroundImage = "url(" + //document.getElementById(imageid).src + ")";
//	var delayedResetBkgrnd = 
//		"document.getElementById('"+divid+"').style.backgroundImage = "
//			+ "\"url('" + document.getElementById(imageid).src + "')\"";
	// Or, better, set the background to none	- not needed until the next blend
	// Ideally, we'd fade out the background, but can't do that	
	var delayedResetBkgrnd = 
		"document.getElementById('"+divid+"').style.backgroundImage ="+"'none'";
	//alert(delayedResetBkgrnd);
	setTimeout(delayedResetBkgrnd, millisec);
}

// RF: Clear the current image and fade in the new one - better when images are 
// different sizes - no mucking with the background
function fadeimage(divid, imageid, imagefile, millisec) {
	//make image (foreground) transparent
	changeOpac(0, imageid);
	
	//set new image
	document.getElementById(imageid).src = imagefile;

	// fade the opacity from invisible to visible
	opacity(imageid, 0, 100, millisec);
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}
