// print address to html
function writeAddress(id) {
	var cssClass = 'Address'+id;
	document.write('<span class="'+cssClass+'"></span>');
	getAddress(id, function(address) {
		$('span.'+cssClass).each(function() {
			$(this).replaceWith(address);
		});
	});
}

// manipulate mailto href
function writeTo(link, id) {
	var link = $(link);
	var href = link.attr('href');
	if (href.substring(0, 7) == 'mailto:') {
		return true;
	}
	
	getAddress(id, function(address) {
		link.attr('href', 'mailto:'+address);
		window.location.href = 'mailto:'+address;
	});
	
	return false;
}

// get address from server
function getAddress(id, callback) {
	$.post('includes/ezajax/ezajax.php', {
		className: 'EzChainsPlugin_Core_MaskEMails',
		method: 'getEMail',
		hash: id
	}, function(response) {
		if (response != 'error' && response != '') {
			callback(response);
		}
	});
}

