/**
 * SEmail serves to secure email adresses against spam.
 * @author NOSE
 * @author SpamSpan (www.spamspan.com)
 * 
 * @example <script type="text/javascript" src="semail.js"></script>
 * @before
 	<span class="semail"><span class="username">test1.email</span>[at]<span class="domain">domain1.com</span></span>
 * 
 * 
 * @option sEmailClass The class name of the surrounding span.
 * @option sEmailUsernameClass The class name of the username span.
 * @option sEmailDomainClass The class name of the domain span.
 * 
 * @version 1.1.0 event listener
 * @version 1.0.0 initial version
 */
var SEmail = {	
	
	/**
	 * SEmail Parameters.
	 */
	sEmailClass: "semail",
	sEmailUsernameClass: "username",
	sEmailDomainClass: "domain",
	
	/**
	 * Initializes the tracker.
	 */
	initialize: function() {
		// elements
		var elements = SEmail.getElementsByClassName(SEmail.sEmailClass,"span",document);
		
		// init
		for (var i = 0; i < elements.length; i++) {
			// vars
			var e = elements[i];
			var username = SEmail.getSpanValue(SEmail.sEmailUsernameClass,e);
			var domain = SEmail.getSpanValue(SEmail.sEmailDomainClass,e);
			var at = String.fromCharCode(32*2);
			
			// anchor
			var email = SEmail.cleanSpan(username) + at + SEmail.cleanSpan(domain);
			var mto = String.fromCharCode(109,97,105,108,116,111,58);	
			
			// text
			var anchorTagText = document.createTextNode(email);
			if (e.getAttribute("rel") != null) {
				var ce = e.getAttribute("rel");
				anchorTagText = document.createTextNode(ce);
			}
			var anchorTag = document.createElement('a');
			anchorTag.className = SEmail.sEmailClass;
			anchorTag.setAttribute('href', mto + email);
			anchorTag.appendChild(anchorTagText);
			
			// replace the span with anchor
			e.parentNode.replaceChild(anchorTag, e);			
		}
	},
	
	
	/*
	 * Gets elements of a class.
	 */
	getElementsByClassName: function(searchClass,tag,scope) {
		// init
		var elements = new Array();
		if (tag == null) {
			tag = '*';
		}
		if (scope == null) {
			scope = document;
		}
		// search
		var tags = scope.getElementsByTagName(tag);
		var pattern = new RegExp("(^|\s)"+searchClass+"(\s|$)");
		for (var i = 0; i < tags.length; i++) {
			if ( pattern.test(tags[i].className) ) {
				elements.push(tags[i]);
			}
		}
		return elements;
	},
	/*
	 * Gets the span's value.
	 */
	getSpanValue: function(searchClass, scope) {
		var span = SEmail.getElementsByClassName(searchClass, 'span',scope);
		if (span[0]) {
			return span[0].innerHTML;
		}
		else return false;
	},
	/*
	 * Cleans a span.
	 */
	cleanSpan: function(val) {
		// replace comments <!-- bla --> text <!-- bla -->
		var cleaned = val.replace(/\<!\s*--(.*?)(--\s*\>)/g, '');
		
		// replace white space
		var cleaned = cleaned.replace(/\s+/g, '');
		return cleaned;
	},
	
	/*
	 * Adds an event listener.
	 * @el The element.
	 * @type The event type.
	 * @fn The function to invoke.
	 */
	 addListener: function() {
	 	// firefox, etc.
	 	if ( window.addEventListener ) {
        	return function(el, type, fn) {
        		el.addEventListener(type, fn, false);
        	};
    	} 
    	// ie
    	else if ( window.attachEvent ) {
        	return function(el, type, fn) {
            	var f = function() {
                	fn.call(el, window.event);
            	};
            	el.attachEvent('on'+type, f);
        	};
    	}
    	// other 
    	else {
        	return function(el, type, fn) {
            	element['on'+type] = fn;
        	}
    	}
	 }()

}

/**
 * Initialize on window load.
 */
SEmail.addListener(window, 'load', function(){SEmail.initialize()});
	
