/*
	*** Modified: 2007/11/07 by Jakob Givoni,
		- Added check for child elements before inserting text into alt tag (if not, a warning is printed to console.error)
		- Changed: Image is only inserted if it is found (the image onload event will insert the image)
		
	*** Modified: 2007/05/28 by Anton Prowse, to change the affected element to "h2",
	    and removed onload event handling which must be controlled centrally, not here!
	    The following JavaScript should go in the header:
   			//jsimagereplacement
			ipath = '/images/titles/'; // or change to any suitable path
	    while the following tag should directly follow the last (and only the last)
	    occurrance of a h2.title* in the body.
			<script type="text/javascript">replaceTitles()</script>
	    Yes it's nasty, but it's the most
	    effective way of preventing a pre-replacement flash of header text.
	    Also removed the "image id is set to h1 id" behaviour.

	    ORIGINAL FILE METADATA FOllOWS
	***

	replacetitles.js	Version 1.1 6-May-2007
	----------------------------------------
	Copyright (c) 2007 Conrad Consulting, Inc.
	Author: Charles J. Conrad
	
	Description:
	Replaces content titles (h1 tags with class 'title*') with images
	
	h1 tag id is image filename.
	No replacement occurs if id is missing.
	Replacement images are assumed to be stored in directory defined by variable ipath.
	
	image src is set to ipath + id
		Note: xhtml does not allow '/' in an id name, even though browsers may support it.
	image class is set to h1 class
	image id is set to h1 id

	Usage:
	include as external script
	activated by onload event

	Acknowledgements:
	prior image replacement advocates

	replacetitles.js is free software; you can redistribute it and/or
	modify it under the terms of the GNU Lesser General Public
	License as published by the Free Software Foundation; either
	version 2.1 of the License, or (at your option) any later version.

	replacetitles.js is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
	Lesser General Public License for more details.

	You should have received a copy of the GNU Lesser General Public
	License along with this library; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

function _replaceTitle(node)
{
	if (node.id)
	{
			var image = document.createElement('img');
			image.className=node.className;
			image.src = ipath + node.id;
			
			if (!node.firstChild)
			{
				console.error("Title contains no child elements (jsimagereplacement)");
			}
			else
			{
				if (node.firstChild.nodeValue) {
					image.alt = node.firstChild.nodeValue;
				}
			}
			image.title_node = node;
			image.onload = function()
			{
				if (this.title_node.firstChild)
					this.title_node.replaceChild(this, this.title_node.firstChild);
				else
					this.title_node.appendChild(this);
			}
	}
}

function replaceTitles()
{
/* Check for DOM support */
    if (!(document && document.implementation && document.implementation.hasFeature))
    	return;

/* Check for image support */
	if (!document.images)
		return;

	var pattern = /(^|\s)title/;
	var titles = document.getElementsByTagName('h2');
	for (var i=0; i < titles.length; i++)
		if (pattern.test(titles[i].className)) 
			_replaceTitle(titles[i]);
}
