/*
======================================================================
This script can be used to dynamically replace text nodes with images,
so that visual form can be maintained without search engine penalties
due to positioning text offscreen, or making it invisible.
======================================================================
Author:   Ceri Williams
Version:  05/06/06
======================================================================
*/

// set the parent document reference
var doc         = parent.document;
// set the font-sizes : default, H1, H2, H3, H4
var fontSizes 	= new Array( 12, 25, 18, 15, 14 );
// presence of valid ID selector
var hasId       = new Boolean();
// the image element
var img         = new Object();
// font size set to default
var size        = new Number( fontSizes[0] );
// create a temporary array for storage
var tmp         = new Array();
// the URL prefix for images
var url         = new String();

hasId = false;

/*
Function to replace text nodes with an image
Params: none
Return: none
*/
replaceText = function ( elementName, id )
{
	// check whether we can use the getElementById method:
	// if the id has been set
	if ( id != false )
	{
		// check whether the ID exists in the DOM
		if ( doc.getElementById(id) )
		{
			// set hasId to true if ID exists
			hasId = true;
		}
		// otherwise return false and exit
		else
		{
			return false;
		}
	}
	// get the text from the element in question
	switch ( hasId )
	{
		// the element has an ID
		case true:
			replaceNodeText( new Array (doc.getElementById(id)) );
			break;
		// the element has no ID or the declared ID is invalid
		case false:
			replaceNodeText( doc.getElementsByTagName(elementName) );
			break;
	}
}
/*
Function to replace the current text with an image for all elements of given type (e.g. h1)
Params:
Return:
*/
replaceNodeText = function ( targetElement )
{
	// count how many items we have to deal with
	totalItems  = targetElement.length;
	// now cycle through them
	for ( i = 0; i < totalItems; i++ )
	{
		// check that its a textNode before continuing
		// @todo add the ability to modify headings containing hyperlinks
		if ( targetElement[i].firstChild.nodeName == '#text')
		{
			targetElement[i].setAttribute('class','rendering');
			// get the current text from the node
			currentText = targetElement[i].firstChild.nodeValue;
			// create the string to use in the URL
			msgText = escape(currentText.replace(/ /g, '+'));
			
			// check which font size should be applied
			// @todo clean code to avoid temporary returns for H2-H4
			switch ( targetElement[i].nodeName.toLowerCase() )
			{
				// H1
				case 'h1':
				  size = fontSizes[1];
				  break;
				// H2
				case 'h2':
				  msgText = msgText.toLowerCase();
				  size = fontSizes[2];
				  break;
				// H3
				case 'h3':
				  msgText = msgText.toLowerCase();
				  size = fontSizes[3];
				  break;
				// H4
				case 'h4':
				  msgText = msgText.toLowerCase();
				  size = fontSizes[4];
				  break;
			}
			// build the URL
			//msgText=msgText.replace(/ /g, "+");
			url = 'lib/ir/'+size+'/'+msgText;
			// create an img element
			img = doc.createElement("img");
			// and set its src, alt text, height and width attributes too
			img.setAttribute('src', url);
			img.setAttribute('alt', currentText);
			// now remove the textNode
			targetElement[i].removeChild(targetElement[i].firstChild);
			// and replace it with the img
			targetElement[i].appendChild(img);
			// and remove the class set whilst rendering
			targetElement[i].removeAttribute('class','rendering');
		}
		// @todo check why images aren't inserted into heading>anchor
		else if ( targetElement[i].firstChild.nodeName.toLowerCase() == 'a')
		{
			targetElement[i].setAttribute('class','rendering');
			// get the current text from the node
			currentText = targetElement[i].firstChild.childNode[0].nodeValue;

			// create the string to use in the URL
			msgText = escape(currentText.replace(/ /g, '+'));

			// check which font size should be applied
			// @todo clean code to avoid temporary returns for H2-H4
			switch ( targetElement[i].nodeName.toLowerCase() )
			{
				// H1
				case 'h1':
				  size = fontSizes[1];
				  break;
				// H2
				case 'h2':
				  msgText = msgText.toLowerCase();
				  size = fontSizes[2];
				  break;
				// H3
				case 'h3':
				  msgText = msgText.toLowerCase();
				  size = fontSizes[3];
				  break;
				// H4
				case 'h4':
				  msgText = msgText.toLowerCase();
				  size = fontSizes[4];
				  break;
			}
			// build the URL
			//msgText=msgText.replace(/ /g, "+");
			url = 'lib/ir/'+size+'/'+msgText;
			// create an img element
			img = doc.createElement("img");
			// and set its src, alt text, height and width attributes too
			img.setAttribute('src', url);
			img.setAttribute('alt', currentText);
			// now remove the textNode
			targetElement[i].firstChild.removeChild(targetElement[i].firstChild);
			// and replace it with the img
			targetElement[i].firstChild.appendChild(img);
			// and remove the class set whilst rendering
			targetElement[i].firstChild.removeAttribute('class','rendering');
		}
	}
}
