/*******************************************************************************************************************************
Icjoo Image Replacement 1.4
Image, CSS, Javascript on only image replacement
Use:			To replace text with an image only when images, CSS and Javascript are on.
Author:			David Smith, 2006 July 26th
More information:	http://www.zaidesign.net/development/javascript/icjoo_image_replacement/
*******************************************************************************************************************************/
var ir = {
	textToReplace : {},	// Hash table of the text to replace and the corresponding image url that replaces it. e.g. ir.textToReplace = {"My Header" : "/images/header.jpg"}
	className : "ir",	// Class name that identifies image replacement elements
	els : [],		// Element store
	init : function() {
		if(document.createElement && document.getElementsByTagName && ((document.defaultView && document.defaultView.getComputedStyle) || document.documentElement.currentStyle)) {
			for(var els = document.documentElement.all || document.getElementsByTagName("*"), i = 0; i < els.length; i++) // Collect elements with class="ir"
				if(els[i].className && els[i].className.match(new RegExp("\\b" + ir.className + "\\b")))
					ir.els[ir.els.length] = els[i];
			if(ir.els.length != 0) {
				ir.imageReplacement();
				var _timer = setInterval(ir.imageReplacement, 500);
			}
		}
	},
	imageReplacement : function() {
		if(ir.getCSSProperty(ir.els[0], "overflow") == "auto") {
			if(ir.els[0].className.match(new RegExp("\\b" + ir.className + "\\b")))
				ir.replaceText();
		}
		else if(ir.els[0].className.match(new RegExp("\\b" + ir.className + "_switched\\b"))) {
				ir.replaceImage();
		}
	},
	replaceText : function() {
		var replace = document.createElement("img");
		loopy: for(var i = 0; i < ir.els.length; i++) { // Go through elements with class="ir"
			var y = replace.cloneNode(true);
			for(var j in ir.textToReplace) { // j equals index of ir.textToReplace hash table
				var textMatch = j.match(new RegExp("[^\\?]+", "g"));
				var tMatch = (textMatch[1]) ? textMatch[1] : "";
				if(textMatch[0] == ir.els[i].firstChild.nodeValue && (tMatch == "" || tMatch.toLowerCase() == ir.els[i].tagName.toLowerCase())) {
					y.src = ir.textToReplace[j];
					y.alt = ir.els[i].firstChild.nodeValue;
					ir.els[i].replaceChild(y,ir.els[i].firstChild);
					ir.els[i].className = ir.els[i].className.replace(new RegExp("\\b" + ir.className + "\\b"), ir.className + "_switched");
					continue loopy;
				}
			}
		}
	},
	replaceImage : function() {
		var replace = document.createTextNode("");
		for(var i = 0; i < ir.els.length; i++) {
			var y = replace.cloneNode(true);
			y.nodeValue = ir.els[i].firstChild.alt;
			ir.els[i].replaceChild(y,ir.els[i].firstChild);
			ir.els[i].className = ir.els[i].className.replace(new RegExp("\\b" + ir.className + "_switched\\b"), ir.className);
		}
	},
	getCSSProperty : function(el, property) { // Thanks to Pepejeria's post on http://www.sitepoint.com/forums/showpost.php?p=2215947&postcount=24
		if(document.defaultView && document.defaultView.getComputedStyle)
			return document.defaultView.getComputedStyle(el, null).getPropertyValue(property);
		else if(document.documentElement.currentStyle)
			return el.currentStyle[property.replace(/-\D/gi, function(match) {return match.charAt(match.length - 1).toUpperCase();})];
		else return null;
	}
}