
/**
 * This script decode all base64 encoded strings like "[".base64_encode("b64tag:".$string)."]"
 * directly after loading the whole DOM.
 *
 * you have to insert prototype into your html header before this script.
 *
 * @requirements prototype 1.6
 * @requirements base64 0.beta
 *
 * @author Helmut Wandl <helmut@wandls.net>
 * @version 0.beta.1
 *
 * @copy STERNWERK 2008
 *
 */
document.observe('dom:loaded', function()
{
	var reg=/\[([ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+\/=]+)\]/;
	var e;

	var obj=document.firstChild;
	while(obj)
	{
		if (obj.nodeName == 'A')
		{
			while ((e=reg.exec(obj.getAttribute('href'))) != null && (e=base64.decode(e[1])) && e.substr(0,7) == 'b64tag:')
			{
				if (e.substr(7,6) == 'mailto')
					obj.href=e.substr(7);
				else
					obj.setAttribute('href', obj.getAttribute('href').replace(reg, e.substr(7)));
			}
		}

		if (obj.nodeName == 'IMG')
		{
			while ((e=reg.exec(obj.getAttribute('alt'))) != null && (e=Base64.decode(e[1])) && e.substr(0,7) == 'b64tag:')
				obj.setAttribute('alt', obj.getAttribute('alt').replace(reg, e.substr(7)));
		}

		while (obj.title && (e=reg.exec(obj.title)) != null && (e=Base64.decode(e[1])) && e.substr(0,7) == 'b64tag:')
			obj.title=obj.title.replace(reg, e.substr(7));

		if (obj.nodeName == 'TEXTAREA' || obj.nodeName == 'INPUT')
		{
			while ((e=reg.exec(obj.value)) != null && (e=base64.decode(e[1])) && e.substr(0,7) == 'b64tag:')
				obj.value=obj.value.replace(reg, e.substr(7));
		}

		if (obj.nodeName == '#text')
		{
			while ((e=reg.exec(obj.nodeValue)) != null && (e=base64.decode(e[1])) && e.substr(0,7) == 'b64tag:')
				obj.nodeValue=obj.nodeValue.replace(reg, e.substr(7));
		}

		obj=nextSiblingX(obj);
	}

	function nextSiblingX(obj)
	{
		if (obj.firstChild) return obj.firstChild;
		while (!obj.nextSibling && obj.parentNode) obj = obj.parentNode;
		if (obj.nextSibling) return obj.nextSibling;
		return false;
	}
});

