
DD_ImageReplace = Class.create({
	initialize: function() {
		this.path = "/wp-content/themes/travbook/";
		this.scriptURL = "heading.php";
		this.testURL =  "images/test.png";

		this.doNotPrintImages = false;
		this.printerCSS = "replacement-print.css";

		this.hideFlicker = false;
		this.hideFlickerCSS = "replacement-screen.css";
		this.hideFlickerTimeout = 1000;
	},
	
	startup: function() {
		var test = new Image();
		var tmp = new Date();
		var suffix = tmp.getTime();
		test.src = this.path + this.testURL + '?' + suffix;
		this.canLoad = false;
		test.onload = function() {
			this.canLoad = true;
			document.fire('DD_ImageReplace:readyToReplace');
		};
	},
	
	findTextNodes: function(e) {
		var nodes = $A(e.childNodes);
		var textNodes = $A();
		nodes.each(function(node) {
			if (node.nodeName == '#text') {
				textNodes.push(node);
			} else {
				textNodes = textNodes.concat(this.findTextNodes(node));
			}
		}.bind(this));
		return textNodes;
	},
	
	replace: function(options) {
		var newImage = document.createElement('img');
		if (typeof(options) == 'string') {
			options = $A([{selector:options}]);
		}
		options.each(function(option) {
			var elements = $$(option.selector);
			elements.each(function(e) {
				var replaceTheseChildren = this.findTextNodes(e);
				replaceTheseChildren.each(function(node) {
					var y = newImage.cloneNode(true);
					var sizeString = (option.size) ? '&size=' + option.size : '';
					var colorString = (option.color) ? '&color=' + option.color : '';
					var fontString = (option.font) ? '&font=' + option.font : '';
					y.src = this.path + this.scriptURL + '?text=' + escape(node.nodeValue) + sizeString + colorString + fontString;
					y.alt = node.nodeValue;
					y.className = (option.font) ? 'replaced-heading ' + option.font : 'replaced-heading';
					node.parentNode.replaceChild(y,node);
				}.bind(this));
			}.bind(this));
		}.bind(this));

	}
	
});