/*
 * Alphabet Replacer
 * 
 * version: 1.0
 * since: 25/04/2010
 * author Thiago Carvalho <thiago.carvalho@teadigital.com.br>
 * 
 * This script replaces an element's innerHTML characters by mapped code.
 * Useful for using custom fonts that aren't supported by default on browsers.
 * 
 * This is free software. You can use it under the MIT License.
 */


function ar_debug(x) {
	//console.log('DEBUG :: ' + x);
}


/**
 * 
 * To use, simply call AlphabetReplacer.replace( elements ), where elements is an
 * array of HTMLElements, which will be searched in its .innerHTML property for
 * replacements.
 * 
 * Before using you must define these vars:
 * 	_charMapping:
 *  	as an array of pair : value, example: {'c' : 'c.gif', 'd' : 'd.gif'}
 * 	_defaultReplacementCode:
 * 		a code that will receive the mapped value, at %item%, example:
 * 		'<img src="%item%" />' will be '<img src="c.gif" />'  when replacing 'c'.
 * 	
 */
var AlphabetReplacer = {

	/**
	 * Default replacement code.
	 * Strings and characters mapped for replacement will be replaced
	 * by this code, having %item% turned into the mapped replacement value.
	 */
	_charMapping : {'c' : 'c_value'},
	
	/**
	 * Maps a character into a replace value to be inserted into _defaultReplacementCode.
	 */
	_defaultReplacementCode : '<some code="with/%item%"/>',
	
	/**
	 * Searches document for elements of class 'replace_this' and does the magic.
	 * @return
	 */
	replace : function (elements) {
		if(elements.length) {
			for(var i = 0; i < elements.length; i++) {
				this.replaceOne(elements[i]);
			}
		} else {
			this.replaceOne(elements);
		}
	},
	
	/**
	 * Replaces each character within the element.innerHTML property
	 * by a generated code, based on configuration and mapping.
	 * If no replacement is found in the mapping, the original char is kept.
	 * @param element to be 
	 * @return
	 */
	replaceOne : function (element) {
		ar_debug('replaceOne(' + element + ')');
		var source = element.innerHTML.replace('&amp;', '&');
		var replaced = '';
		
		ar_debug('replaceOne() :: source tem length = #' + source.length);
		for(var i = 0; i < source.length; i++) {
			var replace = source[i];
			var replacement;
			
			replacement = this.getReplacementFor(replace);
	
			if(!replacement) {
				replacement = replace;
			}
			replaced = replaced + replacement;
			ar_debug('replaceOne() :: replace: ' + replace + ', replacement: ' + replacement);
		}
		
		element.innerHTML = replaced;
	},
	
	/**
	 * Gets the replacement for the specified character.
	 * @param character to be replaced
	 * @return the replacement string
	 */
	getReplacementFor : function (source) {
		if(this._charMapping[source]) {
			return this.buildReplacementCode(source, this._charMapping[source]);
		}
	},
	
	/**
	 * Build a replacement code (possibly HTML) using the replacement string as part of it.
	 * @param sourceChar character being replaced
	 * @param replaceString string of replacement  
	 * @return the replacement code
	 */
	buildReplacementCode : function (source, replaceString) {
		ar_debug('buildReplacementCode(' + source + ', ' + replaceString + ')');
		var replacementCode = this._defaultReplacementCode.replace('%item%', replaceString);
		ar_debug('buildReplacementCode() <-- replacementCode: ' + replacementCode);
		return replacementCode;
	}
};


