/* cms Tools */


function CmsTools (session)
{
	var self = this;
	
	var __construct = function (session)
	{
		if (!window.cms) {
			window.cms = function () {};
			window.cms.session = session;
		}
		
		if (window.cms.activeSection) {
			replaceEmail ();
			activateTargets ();
		} else {
			window.attachEvent ("onload", replaceEmail);
			window.attachEvent ("onload", activateTargets);
		}
	}	
	
	// EMails der Form 'name at email dot de' in 'name@email.de' umwandeln
	var replaceEmail = function ()
	{
		var doc = window.cms.activeSection ? window.cms.activeSection : document;
		var link = doc.getElementsByTagName ("a");
		for (var i=0; i<link.length; i++) {
			var href = link[i].getAttribute ("href");
			var erg;
			if ((erg = /mailto:(\S+)( |%20|\[)at( |%20|\])(\S+)( |%20|\[)dot( |%20|\])(\S+)/.exec (href))) {
				href = erg[1]+"@"+erg[4]+"."+erg[7];
				link[i].setAttribute ("href", "mailto:"+href);
				if (link[i].innerHTML.match (/@|\[at\]/)) {
					link[i].innerHTML = href;
				}
			}
		}
	}
	
	// Target Bereiche suchen und aktivieren
	var activateTargets = function ()
	{
		var doc = window.cms.activeSection ? window.cms.activeSection : document;
		var target = doc.getElementsByTagName ("var");
		for (var i=0; i<target.length; i++) {
			var name = target[i].getAttribute ("name");
			if (name) {
				target[i].parentNode.id = name;
				var location = new CmsLocation (name).location;
				var src = target[i].parentNode.getAttribute ("src");
				if (src) {
					location.href = src;
				}
			}
		}
	}
	
	// Eventhandler hinzufügen
	if (!document.all) {
		if (!Node.prototype) {
			window["HTMLHtmlElement"] = document.createElement("html").constructor;
			window.__safari = true;
			
			function HTMLElement() {
			}
			
			HTMLElement.prototype = HTMLHtmlElement.__proto__.__proto__;
			var HTMLDocument = document.constructor;
			var Text = document.createTextNode("").constructor;
			Node = Text;
		}
		
		if (!window.attachEvent) {
			Node.prototype.attachEvent = function (event, action) { this.addEventListener(event.substr (2), action, false); };
			window.attachEvent = Node.prototype.attachEvent;
			try {
				top.attachEvent = Node.prototype.attachEvent;
			} catch (e) {};
		}
	}
	/*
	Node.prototype.addEvent = function (event, aktion) { if (this.attachEvent) { this.attachEvent (event, aktion); } else { this.addEventListener (event.substr (2), aktion, false); } };
	window.addEvent = Node.prototype.addEvent;
	top.addEvent = Node.prototype.addEvent;
	*/
	__construct (session);
}

function CmsLocation (nodeId)
{
	var self = this;
	var nodeId = nodeId;
 	this.location = null;
	
	var __construct = function (nodeId)
	{
		var node = document.getElementById (nodeId);
		
		if (node && !node.location) {
			var iframe = document.createElement("span");
			iframe.innerHTML = "<iframe name='"+nodeId+"' style='visibility:hidden;position:absolute;left:0;top:0;width:1;height:1;'></iframe>";
// 			iframe.innerHTML = "<iframe name='"+nodeId+"'></iframe>";
			iframe = iframe.firstChild;
			node.parentNode.appendChild (iframe);
			node.parentNode.lastChild.attachEvent ("onload", function () {
				if (top.cms) {
					top.cms.frametarget = nodeId;
				}
				window.cms.activeSection = node;
				var body = frames[nodeId].document.body;
				if (body.innerHTML) {
					// der IE braucht ein "&nbsp;" am Anfang, ansonsten werden am Anfang stehende Scripte nicht importiert
					node.innerHTML = "<var style='visibility:hidden;position:absolute;'>&nbsp;</var>" + ((body.firstChild.nodeName == "PRE") ? body.textContent : unescape (body.firstChild.value));
					// externe styles Aktivieren
					var nodes = node.getElementsByTagName ("link");
					for (var i=0; i<nodes.length; i++) {
						var link = document.createElement("link");
						link.setAttribute ('type', 'text/css');
						link.setAttribute ('rel', 'stylesheet');
						link.setAttribute ('href', nodes[i].getAttribute ('href'));
						document.getElementsByTagName ("head")[0].appendChild (link);
						nodes[i].parentNode.removeChild (nodes[i]);
					}
					// eingebettete styles Aktivieren
					if (!document.all) {
						// nicht IE
						var nodes = node.getElementsByTagName ("style");
						for (var i=0; i<nodes.length; i++) {
							var style = document.createElement("style");
							style.setAttribute ('type','text/css');
							style.innerHTML = nodes[i].innerHTML;
							document.getElementsByTagName ("head")[0].appendChild (style);
							nodes[i].parentNode.removeChild (nodes[i]);
						}
					}
					// scripte Aktivieren
					var nodes = node.getElementsByTagName ("script");
					for (var i=0; i<nodes.length; i++) {
						var script = document.createElement("script");
						script.setAttribute ('type','text/javascript');
						if (nodes[i].getAttribute ('src')) {
							script.setAttribute ('src', nodes[i].getAttribute ('src'));
						} else {
							if (nodes[i].text) {
								script.text = nodes[i].text;
							} else {
								// Safari
								script.innerHTML = nodes[i].innerHTML;
							}
						}
						nodes[i].parentNode.insertBefore (script, nodes[i]);
						nodes[i].parentNode.removeChild (nodes[i]);
					}
					// targets setzen
					var nodes = node.getElementsByTagName ("a");
					for (var i=0; i<nodes.length; i++) {
						if (nodes[i].getAttribute ("target")=="_self") {
							nodes[i].setAttribute ("target", nodeId);
						}
					}
					var nodes = node.getElementsByTagName ("form");
					for (var i=0; i<nodes.length; i++) {
						if (nodes[i].getAttribute ("target")=="_self") {
							nodes[i].setAttribute ("target", nodeId);
						}
					}
					// onload Aktion ausführen
					if (node.location.onload) {
						node.location.onload ();
					}
					if (node.getAttribute ('onload')) {
						eval (node.getAttribute ('onload'));
					} else if (node.onload) {
						node.onload ();
					}
				}
			});
 			node.location = iframe.contentWindow.location;
 			self.location = node.location;
		}
	}
	
	__construct (nodeId);
}
