// this script hides all images, replacing them with their alt text

var origText;

// if an element has a background image set, it stores it in here
// so it can be restored later.
// each element has an id set, and the background is referenced by id
var bgObject = new Object(); 

// elements are created as siblings to images, so, when the images are
// hidden the new elements contain the image's alt text.
// the following object holds the value "true" referenced by all
// the 'alt' element's ids
var altObject = new Object();

// holds the value of the style.display for all hidden images
// se we can restore the value after display is set to 'none'
var imgObject = new Array();

// function replaces all images with divs. we'll call 'alt elements'
function replaceThem()
{
	// x contains all img nodes
	var x = document.getElementsByTagName('img');
	
	// create a div which we will make copies of and append after img elements
	var myReplace = document.createElement('div');
	myReplace.style.display = "inline";
	myReplace.style.backgroundColor = "#FAFCFA";
	for (var i=0;i<x.length;i++)
	{
		// set ids for all elemnts without ids
		if (!x[i].id) {
			x[i].id = "img_" + i;
		}
		
		// only act on non-spacer images (not used in this site!)
		if (x[i].src != "spacer.gif") {
			
			// create the alt node
			var y = myReplace.cloneNode(true);
			
			// store the display property in the object so we can restore it later
			imgObject[x[i].id] = x[i].style.display;
			
			// give the alt node set properties
			y.style.width = x[i].scrollWidth;
			y.style.height = x[i].scrollHeight;
			y.id = "alt_" + i;

			// set the text in the node to the alt text of the image
			y.innerHTML = x[i].getAttribute( "alt" );
			
			// register the alt node in an object for later reference
			altObject[ y.id ] = "true";
			
			// add the alt node before the image
			x[i].parentNode.insertBefore(y,x[i]);
			
			// hide the image
			x[i].style.display = "none";
		}
	}
}

// function removes all backgrounds from nodes
function removeBG() {
	var x = document.getElementsByTagName('*');
	for (var i=0;i<x.length;i++)
	{
		if (x[i].style.backgroundImage ){
			// if node has no id, then give it an id
			if (!x[i].id) {
				x[i].id = "bg_" + i;
			}
			
			// register the node's background in an object so we can restore it later
			bgObject[x[i].id] = x[i].style.backgroundImage;
			
			// remove the background
			x[i].style.backgroundImage = "none";
		}
	}
}

// restores the background to all elements that had backgrounds
function restoreBG() {
	for (i in bgObject) {
		document.getElementById( i ).style.backgroundImage = bgObject[i];
	}
}

// function restores the images
function restoreThem()
{
	// restore all image display properties
	for (i in imgObject) {
		document.getElementById( i ).style.display = imgObject[i];
	}

	// remove all alt nodes
	for (i in altObject) {
		document.getElementById( i ).parentNode.removeChild( document.getElementById( i ) );
		altObject[i] = null;
	}
}

// called to hide all images and backgrounds
function imageReplacement() {
	replaceThem();
	removeBG();
}

// called to retore all images and backgrounds
function altReplacement() {
	restoreThem();
	restoreBG();
}

// toggles all images and backgrounds
function toggleImages(shown) {
	showText = "View with images";
	var stringReturn = "";
	if (shown != showText) {
		origText = shown;
		imageReplacement();
		stringReturn = showText;
	} else {
		altReplacement();
		stringReturn = origText;
	}
	return stringReturn;
}