var a78 = {
	lightbox: {
		initialize: function() {
			var me = this;
			setTimeout(function(){
				var links = document.getElementsByTagName('a'), rel;
				var firstFound = null;
				for (var i = 0; i < links.length; i++) {
					rel = links[i].getAttribute('rel');
					if (rel && rel.match(/^lightbox\[\w+\]$/)) {
						if (!firstFound)
							firstFound = links[i];
						links[i].onclick = function() {
							me.show(this);
							return false;
						};
					}
				}
				if (firstFound) {
					firstFound.onclick();
				}
			}, 0); // run when idle
		},
		originalElements: [],
		//lightboxEnd: null,
		show: function(element) {
			//if (!this.lightboxEnd)
				//myLightbox.end();
			if (!core) {
				var src = element.getAttribute('href');
				var img = document.createElement('img');
				img.setAttribute('alt', '');
				img.setAttribute('src', src.replace(/\/version0\./,'/version1.'));

				aroundImage = document.createElement('a');
				aroundImage.style.cursor = 'pointer';
				var me = this;
				aroundImage.onclick = function() {
					myLightbox.start(element);
/*					if (!me.lightboxEnd) {
						me.lightboxEnd = myLightbox.end;
						myLightbox.end = function() {
							me.endCallback();
						}
					}*/
				};
				aroundImage.appendChild(img);
				var span = document.createElement('span');
				span.className = 'moreIcon';
				aroundImage.appendChild(span);

				var container = document.getElementById('imageArea');

				if (!this.originalElements.length) {
					while (container.firstChild) {
						this.originalElements.push(container.removeChild(container.firstChild));
					}
				}
				while (container.firstChild) {
					container.removeChild(container.firstChild);
				}
				container.appendChild(aroundImage);
			}
		},

		endCallback: function() {
			myLightbox.end = this.lightboxEnd;
			this.lightboxEnd = null;
			myLightbox.end();
			this.rollback();
		},

		rollback: function() {
			var container = document.getElementById('imageArea');
			while (container.firstChild)
				container.removeChild(container.firstChild);
			for (var i = 0; i < this.originalElements.length; i++) {
				container.appendChild(this.originalElements[i]);
			}
			this.originalElements = [];
		}
	},

	contact: {
		toggle: function(elm) {
			var node = elm.parentNode;
			var nodes = [];
			do {
				node = node.nextSibling;
				while (node && node.nodeType != 1)
					node = node.nextSibling;
				if (node && node.className == 'medarbejder')
					nodes.push(node);
			} while (node && node.className == 'medarbejder');

			if (nodes.length) {
				var display = 'block'
				var className = 'open';
				var replace = / ?closed ?/;
				var text = 'Skjul medarbejdere';
				if (nodes[0].style.display == 'block') {
					display = 'none';
					className = 'closed';
					replace = / ?open ?/;
					text = 'Vis medarbejdere';
				}
				for (i = 0; i < nodes.length; i++) {
					nodes[i].style.display = display;
				}
				node = elm.parentNode;
				node.className = node.className.replace(replace, '') + ' ' + className;
				elm.replaceChild(document.createTextNode(text), elm.firstChild);
			}
		}
	},

	// returns true if opening, false if closing
	unfold: function(node, steps, milisec, finalize) {
		var startValue = node.clientHeight, endValue = 0;
		if (!startValue) {
			node.style.height = 'auto';
			endValue = node.clientHeight;
			node.style.height = '0';
		}
		var increment = (endValue - startValue) / steps;
		var step = 0;
		var intervalId = setInterval(function(){
			if (step < steps) {
				startValue += increment;
				node.style.height = startValue + 'px';
				step++;
			} else {
				node.style.height = endValue + 'px';
				clearInterval(intervalId);
				if (finalize) {
					finalize();
				}
			}
		}, milisec);
		return !startValue;
	},

	news: {
		__getElms: function(topNode) {
			var header, content, node;
			for (var i = 0; i < topNode.childNodes.length; i++) {
				node = topNode.childNodes[i];
				if (node.nodeType == 1) {
					if (node.className == 'newsTitle') {
						header = node;
					} else if (node.className == 'newsContent') {
						content = node;
					}
				}
			}
			return {title: header, content: content};
		},

		show: function(elm) {
			elm = elm.parentNode.parentNode;
			var e = this.__getElms(elm);
			if (e.title && e.content) {
				a78.unfold(e.title, 10, 10);
				e.content.style.display = 'block';
				a78.unfold(e.content, 10, 10);
				elm.className = 'newsItem newsVisible';
			}
		},

		hide: function(elm) {
			elm = elm.parentNode.parentNode.parentNode;
			var e = this.__getElms(elm);
			if (e.title && e.content) {
				a78.unfold(e.title, 10, 10);
				a78.unfold(e.content, 10, 10, function(){
					e.content.style.display = '';
					elm.className = 'newsItem';
				});
			}
		}
	}
};

lightCore.registerInit(a78.lightbox);
resizeDuration = 0.45; // lightbox setting