﻿if (document.all) {
	window.attachEvent('onload', new Function('var e = new TEditorElementLink(document,null,\'E-Mail-Link mit SPAM-Schutz\');e.unprotectDocument();'));
}
else {
	window.addEventListener('load', new Function('var e = new TEditorElementLink(document,null,\'E-Mail-Link mit SPAM-Schutz\');e.unprotectDocument();'), false);
}



//possible values for protection property of TEditorElementLink
var eelProtectionHref = 1;
var eelProtectionText = 2;
var eelProtectionTitle = 4;

function TEditorElementLink(document, currentObject, textProtected) //todo, mozilla compatibility
{
	//private
	var self = this;
	var _document = document;
	var _textProtected = textProtected; // this could be problematic because detection depends on this text and text is not the same for all languages

	function addEvent(object, eventName, handler, fireImmediatelly) {
		if (document.all) {
			object.attachEvent('on' + eventName, handler);

			if (fireImmediatelly) {
				object.fireEvent('on' + eventName);
			}
		}
		else {
			//mozilla style
			object.addEventListener(eventName, handler, false);

			if (fireImmediatelly) {
				var e = document.createEvent('MouseEvents');
				e.initMouseEvent(eventName, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
				object.dispatchEvent(e);
			}
		}
	}


	//public
	this.protection = 0;
	this.href = '';
	this.subject = '';
	this.address = '';
	this.title = '';
	this.text = '';
	this.target = '';
	this.object = currentObject;
	this.valid = false;
	this.version = '0.2';
	this.enclosingOtherElement = false;

	this.unprotectDocument = function() {
		var c = 0;
		var eList = _document.getElementsByTagName('a');

		while (c <= eList.length - 1) {
			e = eList[c];

			var d = new TEditorElementLink(_document, e, _textProtected);

			if (d.valid && !d.enclosingOtherElement) {
				if (_document.all) { e.innerText = d.text; } else { e.textContent = d.text; }
				e.onmouseout = null;
			}

			c++;
		}
	}

	this.hasProtection = function(eelProtectionValue) {
		return ((this.protection & eelProtectionValue) == eelProtectionValue);
	}

	this.create = function() {
		if (this.object) {
			this.valid = (this.object.tagName ? this.object.tagName.toLowerCase() == 'a' : false);

			if (this.valid) {
				this.enclosingOtherElement = false;

				var c = 0;
				while (c <= this.object.childNodes.length - 1) {
					this.enclosingOtherElement = this.enclosingOtherElement || (this.object.childNodes[c].tagName ? true : false);
					c++;
				}

				this.text = (document.all ? this.object.innerText : this.object.textContent);
				this.href = this.object.href;
				this.title = this.object.title;
				this.target = this.object.target;

				this.protection += (this.href.toLowerCase().indexOf('javascript://sendmail') >= 0 ? eelProtectionHref : 0);
				this.protection += (this.text == _textProtected ? eelProtectionText : 0);
				this.protection += (this.object.onmouseover ? (('' + this.object.onmouseover).toLowerCase().indexOf('this.title ') >= 0 ? eelProtectionTitle : 0) : 0);

				editorElement_temporaryLink = _document.createElement('a'); //we dont use "var" here because it should be a global variable so that event handler can access it
				editorElement_temporaryLink.href = this.object.href;
				editorElement_temporaryLink.title = this.object.title;
				editorElement_temporaryLink.innerText = this.object.innerText;

				if (this.hasProtection(eelProtectionHref) || this.hasProtection(eelProtectionText) || this.hasProtection(eelProtectionTitle)) {
					var onmouseoverHandler = this.object.attributes.getNamedItem('onmouseover');
					if (onmouseoverHandler) {
						addEvent(editorElement_temporaryLink, 'mouseover', new Function(onmouseoverHandler.value.replace(/this./gi, 'editorElement_temporaryLink.')), true);

						this.title = editorElement_temporaryLink.title;
						this.text = (document.all ? editorElement_temporaryLink.innerText : editorElement_temporaryLink.textContent);
						this.href = editorElement_temporaryLink.href;
					}
				}

				i = this.href.toLowerCase().indexOf('mailto:');
				if (i >= 0) {
					s = this.href.substr(i + 7, this.href.length);

					i = s.toLowerCase().indexOf('?subject=');
					if (i >= 0) {
						this.address = s.substr(0, i);
						this.subject = s.substr(i + 9, s.length);
					}
					else {
						this.address = s;
						this.subject = '';
					}
				}
			}
		}
	}

	this.assignTo = function(object, surrounding) {
		object.target = this.target;

		if (this.hasProtection(eelProtectionTitle)) {
			object.attributes.removeNamedItem('title');
		}
		else {
			object.title = this.title;
		}

		var text_initialvalue = (this.hasProtection(eelProtectionText) ? _textProtected : this.text);

		if (!surrounding) {
			object.innerText = text_initialvalue;
			//.innerText = ...replace(/#/g,String.fromCharCode(38)+'#'); //this dont work because ie converts & always to &amp; due to html standard, http://www.astro.washington.edu/owen/ROFM_CGI/Documentation/SpecialChars.html
		}

		if (this.hasProtection(eelProtectionHref) || this.hasProtection(eelProtectionText) || this.hasProtection(eelProtectionTitle)) {
			var onmouseover = _document.createAttribute('onmouseover');
			onmouseover.value = "function c(a) { return String.fromCharCode(a) }; ";

			if (this.hasProtection(eelProtectionHref)) {
				if (!this.hasProtection(eelProtectionText)) {
					onmouseover.value += "s = (document.all?this.innerText:this.textContent); ";
				}

				var href_encoded = "";

				for (var i = 0; i < this.href.length; i++) {
					href_encoded = href_encoded + '#' + this.href.charCodeAt(i) + ';';
				}

				onmouseover.value += "this.href=''" + href_encoded.replace(/#/g, '+c(').replace(/;/g, ')') + "; ";

				if (!this.hasProtection(eelProtectionText) && !surrounding) {
					onmouseover.value += "if (document.all) { this.innerText = s } else { this.textContent = s }; ";
				}

				object.href = "javascript://sendmail;";
			}

			if (this.hasProtection(eelProtectionText) && !surrounding) {
				var text_encoded = "";

				for (var i = 0; i < this.text.length; i++) {
					text_encoded = text_encoded + '#' + this.text.charCodeAt(i) + ';';
				}

				onmouseover.value += "s = ''" + text_encoded.replace(/#/g, '+c(').replace(/;/g, ')') + "; if (document.all) { this.innerText = s; } else { this.textContent = s; };";
			}

			if (this.hasProtection(eelProtectionTitle)) {
				var title_encoded = "";

				for (var i = 0; i < this.title.length; i++) {
					title_encoded = title_encoded + '#' + this.title.charCodeAt(i) + ';';
				}

				onmouseover.value += "this.title = ''" + title_encoded.replace(/#/g, '+c(').replace(/;/g, ')') + ";";
			}

			object.attributes.setNamedItem(onmouseover);

			if (this.hasProtection(eelProtectionText) && !surrounding) {
				var onmouseout = _document.createAttribute('onmouseout');
				onmouseout.value = "s = '" + text_initialvalue + "'; if (document.all) { this.innerText = s; } else { this.textContent = s; }";
				object.attributes.setNamedItem(onmouseout);
			}
			else {
				object.attributes.removeNamedItem('onmouseout');
			}
		}
		else {
			object.attributes.removeNamedItem('onmouseover');
			object.attributes.removeNamedItem('onmouseout');
		}
	}

	self.create();
}