(function() {
	if (!document.body) document.body = document.getElementsByTagName("body")[0];
	var anchors = document.body.getElementsByTagName("a"),
		images = document.body.getElementsByTagName("img"),
		//lightwindow_anchors = {},
		rel;
	for (var i = 0; i < anchors.length; i ++) {
		if (/\.(?:gif|jpeg|jpg|png)$/i.test(anchors[i].href)) {
			if (!anchors[i].getAttribute("title") && !anchors[i].title) anchors[i].setAttribute("title", anchors[i].title = jsapi.string.trim(jsapi.dom.getTextContent(anchors[i])) || "Bild");
			rel = (anchors[i].getAttribute("rel") || anchors[i].rel);
			
			// Highslide JS
			anchors[i].setAttribute("rel", anchors[i].rel = rel = "highslide");
			
			// Lightwindow
			/*addClass.apply(anchors[i], ["lightwindow"]);
			if (!rel) anchors[i].setAttribute("rel", anchors[i].rel = rel = "Bildergallerie");
			if (!lightwindow_anchors[rel]) lightwindow_anchors[rel] = {
				length: 0
			};
			if (!lightwindow_anchors[rel][anchors[i].href]) {
				lightwindow_anchors[rel][anchors[i].href] = anchors[i];
				lightwindow_anchors[rel].length ++;
			}*/
		}
		else if (hasClass.apply(anchors[i], ["toggleDisplay"])) {
			anchors[i].onclick = function() {
				if (hasClass.apply(this, ["displayToggled"])) {
					removeClass.apply(this, ["displayToggled"]);
					addClass.apply(document.getElementById(this.hash.substr(1)), ['displayNone']);
				}
				else {
					addClass.apply(this, ["displayToggled"]);
					removeClass.apply(document.getElementById(this.hash.substr(1)), ['displayNone']);
				};
				return false
			}
		}
		else if (anchors[i].hasChildNodes()) DOM.walk(anchors[i], function(node) {
			if (node.nodeValue) {
				var innerText;
				innerText = node.nodeValue.split(/\s+/);
				if (innerText.length == 3 && innerText[1] == "‹AT›") {
					// recipient ‹AT› domain -> recipient@domain
					node.parentNode.replaceChild(document.createTextNode(innerText[0] + "@" + innerText.slice(2).join("")), node);
				}
			}
		});
	};
	/*for (var rel in lightwindow_anchors) {
		if (lightwindow_anchors[rel] && lightwindow_anchors[rel].length > 1) {
			for (var href in lightwindow_anchors[rel]) {
				if (lightwindow_anchors[rel][href] && lightwindow_anchors[rel][href].href)
					lightwindow_anchors[rel][href].setAttribute("rel", lightwindow_anchors[rel][href].rel = rel + "[]");
			}
		}
	};*/
	var placeHolderImage = new Image();
	placeHolderImage.src = "/006/gfx/noise-anim.gif"
	for (var i = 0; i < images.length; i ++) {
		var image = images[i];
		image.originalImage = new Image();
		image.originalImage.src = image.src;
		if (!image.originalImage.complete) {
			image.originalImage.image = image;
			image.originalImage.onload = function() {
				//if (jsapi.util && typeof jsapi.util.log == "function") jsapi.util.log("Image: " + this + " (" + this.src + ")")
				if (this != window) { // Safari 2 bug: "this" in onload event handler always refers to the global object (but not if the event handler is called as method)
					this.image.src = this.src;
					/*this.image.style.width = this.width + "px";
					this.image.style.height = this.height + "px";*/
					this.image.style.visibility = "visible";
					clearInterval(this.interval);
				}
			};
			image.originalImage.setInterval = function() {
				this.time = 0;
				this.wait = 250;
				var originalImage = this;
				this.interval = setInterval(function() {
					originalImage.time += originalImage.wait;
					if (originalImage.complete || originalImage.time > 10000) { // give each image 10 seconds max
						if (originalImage.complete && originalImage.width > 0) originalImage.onload();
						else if (originalImage.image.getAttribute("alt") || originalImage.image.getAttribute("title") || (originalImage.image.parentNode && /^a$/i.test(originalImage.image.parentNode.tagName))) { // only replace the image if it has an alt or title attribute or the parent node is a link. otherwise recover space used by image by setting display: none
							if (placeHolderImage.complete && placeHolderImage.width > 0) {
								clearInterval(originalImage.interval);
								originalImage.image.src = placeHolderImage.src;
								/*originalImage.image.style.width = placeHolderImage.width + "px";
								originalImage.image.style.height = placeHolderImage.height + "px";*/
								jsapi.dom.attributeRemoveWord(originalImage.image, "class", "supportsTransparency");
								originalImage.image.style.visibility = "visible"
							}
						}
						else {
							clearInterval(originalImage.interval);
							originalImage.image.style.display = "none"
						}
					}
				}, this.wait);
			};
			image.originalImage.setInterval();
			image.style.visibility = "hidden"
			/*if (image.width > 1) image.style.width = image.width + "px";
			if (image.height > 1) image.style.height = image.height + "px";*/
			image.src = "/gfx/dummy.gif";
		}
	};
})();

/*jsapi.dom.append(document.body, jsapi.dom.parseString('<div id="controlbar" class="highslide-overlay controlbar">' +
	'<a href="#" class="previous" onclick="return hs.previous(this)" title="Previous (left arrow key)"></a>' +
	'<a href="#" class="next" onclick="return hs.next(this)" title="Next (right arrow key)"></a>' +
    '<a href="#" class="highslide-move" onclick="return false" title="Click and drag to move"></a>' +
    '<a href="#" class="close" onclick="return hs.close(this)" title="Close"></a>' +
'</div>'));*/