// -----------------------------------------------------------------------------------
//
//	Email_Convert
//
// -----------------------------------------------------------------------------------
//
//	Email_Convert Class Declaration
//	- initialize()
//	- change_email_link()
//
//	Structuring of code taken from Lightbox v2.03.2 by Lokesh Dhakar - http://www.huddletogether.com
//
var Email_Convert = Class.create();

Email_Convert.prototype = {
	
	// initialize()
	// Constructor runs on completion of the DOM loading. Loops through anchor tags looking for 
	// 'mailto:' in href and converts the link to appropriate spam proof links to emails.
	//
	initialize: function() {	
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor_tag = anchors[i];
			
			var relAttribute = String(anchor_tag.getAttribute('href'));
			// use the string.match() method to catch 'mailto:' references in the href attribute
			if (relAttribute.toLowerCase().match('mailto:')){
				convert_email(anchor_tag);
			}
		}
	}
}

function convert_email(tag_link) {
	
txt =   ''
address = tag_link.href;
address = address.substring(7);

	for (j=0; j<address.length; j++) {
		txt += '%' + Dec2Hex(address.charCodeAt(j))
	}
	
	tag_link.href= 'mailto:'+txt;
	
	//tag_link.href= 'mailto:' + convertToUnicode(address);
	tag_link.innerHTML = "" + convertToUnicode(address);
}

function Dec2Hex(Decimal) {
	var hexChars = "0123456789ABCDEF";
	var a = Decimal % 16;
	var b = (Decimal - a)/16;
	hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
	return hex;
}

function convertToUnicode(source) { 
  result = ''; 
  for (i=0; i<source.length; i++) 
    result += '&#' + source.charCodeAt(i) + ';'; 
  return result; 
}

function init_Email_Convert() { doConvert = new Email_Convert(); }
Event.observe(window, 'load', init_Email_Convert, false);