/*jslint white: true, onevar: true, undef: true, nomen: true, eqeqeq: true, plusplus: true, bitwise: true, regexp: true, strict: true, newcap: true, immed: true */
/*global document: false */

"use strict";

/* 	restore protected e-mail addresses

	e-mails are protected in the html code. here we restore them for the browser to the 
	original format.
	
	new version for the new kas.de 2010.
	* break in email via css 'word wrap'
	* different email protection syntax
	20100709 car
*/
function secureMail_v2()
{
	var mails = document.getAncorElementsByClassName('secureMail'),
		i = 0,
		j,
		class_names = [],
		char_length,
		new_string = '';
	
	if (mails) {
		for (i; i < mails.length; i = i + 1) {
			if (mails[i].innerHTML) {
				class_names = mails[i].className.split(' ');
				char_length = null;
				for (j = 0; j < class_names.length; j = j + 1) {
					if (class_names[j].match(/char\d+/g)) {
						char_length = parseInt(class_names[j].replace('char', ''), 10);
					}
				}
				new_string = mails[i].innerHTML.replace('(a', '@');
				mails[i].href = 'mailto:' + new_string;
				mails[i].innerHTML = new_string;
				mails[i].style.visibility = 'visible';
			}
		}
	}
}

function secureMail()
{
	var mails = document.getAncorElementsByClassName('secureMail'),
		i = 0,
		j,
		class_names = [],
		char_length,
		new_string = '';
	
	if (mails) {
		for (i; i < mails.length; i = i + 1) {
			if (mails[i].innerHTML) {
				class_names = mails[i].className.split(' ');
				char_length = null;
				for (j = 0; j < class_names.length; j = j + 1) {
					if (class_names[j].match(/char\d+/g)) {
						char_length = parseInt(class_names[j].replace('char', ''), 10);
					}
				}
				new_string = mails[i].innerHTML.replace(' . ', '.').replace('[at]', '@');
				mails[i].href = 'mailto:' + new_string;
				if (char_length && new_string.length > char_length) {
					// leerzeichen vor @
					new_string = new_string.replace('@', ' @');
				}
				mails[i].innerHTML = new_string;
			}
		}
	}
}
