/*
*** Usage:
	In HTML <head>:
	<script type="text/javascript" src="#templateBaseHREF/libs/jsimagereplacement/jsimagereplacement.js"></script>

	In the HTML <body>:
	<h2 id="replacement.png" class="title">Replace me!</h2>

	Once DOM has loaded:
	<script type="text/javascript">
	//<![CDATA[
		var jsimagereplacement = new JsImageReplacement('#templateBaseHREF/images/text-replacement/', 'h2', 'title');
		jsimagereplacement.replace();
	//]]>
	</script>

*** Modified: 2008/05/05 by Anton Prowse
	- Made code object-oriented

*** 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's onload event will insert the image
	
*** Modified: 2007/05/28 by Anton Prowse
    	- Removed the "image id is set to h1 id" behaviour.
	- Changed 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.


*** 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
*/

/**
 * Modifies the dom to replace the text content of each of a specified set of tags with the image
 * specified by the tag's id attribute.  The new image is given the same class as the 
 *
 * @param {String} iPath the absolute or relative path to the directory in which the images lie
 * @param {String} tagName the node name of the HTML DOM element nodes to be targeted, or null if all nodes are to be targeted
 * @param {String} className the class name of the HTML DOM element nodes to be targeted
 */
function JsImageReplacement(ipath, tagName, className) {

	var that = this;
	var tagName = tagName || '*';
	var className = className;

	function _replace(node) {
		if (!node.id) {
			throw new Error('Node has no id and hence no image path');
		}
		var image = document.createElement('img');
		image.className=node.className;
		image.src = ipath + node.id;

		if (!node.firstChild && window.console) {
			console.error("Node contains no child elements (jsimagereplacement)");
		} else {
			if (node.firstChild.nodeValue) {
				image.alt = node.firstChild.nodeValue;
			}
		}
		image.parent_node = node;
		image.onload = function() {
			if (this.parent_node.firstChild) {
				this.parent_node.replaceChild(this, this.parent_node.firstChild);
			}
			else {
				this.parent_node.appendChild(this);
			}
		}
	}

	this.replace = function() {
		// Check for DOM support
		if (!(document && document.implementation && document.implementation.hasFeature)) {
	  	  	return;
		}
		// Check for image support
		if (!document.images) {
			return;
		}

		var pattern = new RegExp('(^|\\s)' + className);
		var replaceables = document.getElementsByTagName(tagName);
		for (var i=0; i < replaceables.length; i++) {
			if ( pattern.test(replaceables[i].className) ) {
				try {
					_replace(replaceables[i]);
				} catch(e) {
				}
			}
		}
	}

}
