/**
 *	ToggleMore - V0.1 - Oct 11 2006
 *	---------------------------------------------
 *  Author - Jeroen van der Goorbergh
 *  Depends on: EventHandler.js, LinkListener.js, ClassName.js
 */

ShowMore = function(container, area, max) {
	
	if (!container || !area) return;

	this.aRE = /^a$/i;
	this.container = container;
	var self = this;

	if (area.offsetHeight>max) {
		this.anchor = document.createElement("a");
		this.anchor.appendChild(document.createTextNode("lees meer"));
		this.anchor.rel = "showmore";
		this.anchor.href = "#";
		this.anchor.className = "showmore";
		// fade
		var fade = document.createElement("div");
		fade.className = "articlefade";
		this.container.appendChild(fade);
		this.container.appendChild(this.anchor);

		// make click on anchors with reference to elements on same page open article
		addEventHandler(area, "click", function (e) {
			if(e.shiftKey || e.ctrlKey || e.altKey) return true;
			e = e||event;
			var target = e.target||e.srcElement;
			while (!target.nodeName || target.nodeType == 3) target = target.parentNode;
			if (!ClassName.contains(self.container, "lessarticle") && target.nodeName && self.aRE.test(target.nodeName) && self.isLocalLink(target)) {
				self.container.className = "lessarticle";
				self.anchor.style.display = "none";
			} else return true;			
        });

	} else {
		this.container.className = "lessarticle";
	}
}

ShowMore.prototype.isLocalLink = function(anchor) {
	if(anchor.href && anchor.href.indexOf("#") > -1) {
 		var target = anchor.href.split("#")[1];
		if (target && document.getElementById(target)) return true;
	}
	return false;
}

LinkListener.setHandler("showmore", function(link) {
	var showmore = document.getElementById("showmore");
	showmore.className = "lessarticle";
	link.style.display = "none";
	return false;
});


if (onloadHandler) onloadHandler.add(function() {
	window.showmore = new ShowMore(document.getElementById("showmore"),document.getElementById("area"),340);
});