/* --- JavaScript --- */
/* --- add functions to onload event: addLoadEvent(functionName); --- */
/* --- http://simonwillison.net/2004/May/26/addLoadEvent/ --- */
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

/* --- createElement() --- */
function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}

/* --- setAttributes() --- */
function setAttributes(element,attr) {	// format attr: [['class','actief'],['href','http://www.test.xx']]
	if (typeof element.setAttributeNS != 'undefined') {
		for (a=0; a<attr.length; a++) {
			element.setAttributeNS('http://www.w3.org/1999/xhtml',attr[a][0],attr[a][1]);
		}
	}
	if (typeof element.setAttribute != 'undefined') {
		for (a=0; a<attr.length; a++) {
			element.setAttribute(attr[a][0],attr[a][1]);
		}
	}
	return false;
}

/* --- add className --- */
function addClass(node, className) {
	removeClass(node, className);	// make sure there won't be any doubles
	node.className += " " + className;
}

/* --- remove className --- */
function removeClass(node, className) {
	var seperator = (node.className.length == className.length) ? "" : " ";
	node.className = node.className.replace(seperator + className,"");
}

/* --- check if node has className --- */
function hasClass(node, className) {
	var nodeClass = node.className;
	if (!className && nodeClass != "") return true;			// if no className is specified any className will do
	if (className && nodeClass.indexOf(className) > -1) {	// match, but not exact
		var nodeClasses = nodeClass.split(/\s+/);			// seperate class names (devided by one or more whitespaces)
		for (c=0; c<nodeClasses.length; c++) {
			if (nodeClasses[c] == className) return true
		}
	}
	return false;
}

/* --- get first ancestor that matches the property --- */
function getAncestor(node, property, value, levels) {	// levels is an optional argument
	var parent = node;
	var level = (levels) ? levels : 1;
	do {
		parent = parent.parentNode;
		if (!parent || parent.nodeName == "HTML") return false;	// there is no parent or parent is <html>
		
		if ((parent[property] == value) ? true : hasClass(parent, value)) return parent; // return parent if property matches value
			
		if (levels) level--;
	} while (parent.parentNode && parent.parentNode.nodeName != "HTML" && level > 0);
}

/* --- check for CSS support --- */
function cssSupport() {
	if (!document.styleSheets) return false;	// styleSheets object is not supported
	var css = document.styleSheets;
	for (s=0; s<css.length; s++) {
		if (s == 0) {
			if (!(css[0].cssRules || css[0].rules)) return false;	// both methods (cssRules/rules) are not supported
		}
		if (!css[s].disabled) return true;	// at least one of the stylesheets is not disabled
	}
	return false;	// stylesheets are all disabled or not supported at all
}


/* =================================== */
/* ===== site specific functions ===== */
/* =================================== */


///////////// feed /////////////

initFeed = function() {
	var feed = document.getElementById('feed');
	if (!feed) return;
	
	var answers = feed.getElementsByTagName('dd');
	if (!answers[0] || !answers[0].previousSibling) return;	// either the previousSibling property is not supported or the <dl> is corrupt (starts with a <dd>)
	
	for (a=0; a<answers.length; a++) {
		var answer = answers[a];
		
		var ddArr = [];
		var prevSibling = answer;
		
		do {	// find corresponding <dt>
			if (!prevSibling.done && prevSibling.nodeName == "DD") {
				ddArr[ddArr.length] = prevSibling;
				prevSibling.done = true;
			}
			var prevSibling = prevSibling.previousSibling;
		} while (prevSibling.nodeName == "DD" || prevSibling.nodeType == 3);
		
		if (prevSibling.nodeName != "DT" || ddArr.length == 0) continue;
		var feedTitel = prevSibling;
		
		var feedTitelLabel = feedTitel.getElementsByTagName('span')[1];
		feedTitelLabel.innerHTML = feedTitelLabel.innerHTML;	// add <a> in order to make it accessible for keyboard
				
		feedTitel.answers = ddArr;
		
		feedTitelLabel.onclick = function() {
			var dl = getAncestor(this.parentNode, "nodeName", "DL");
			if (!dl) return;
			
			if (dl.current && this.parentNode != dl.current) {
				for (c=0; c<dl.current.answers.length; c++) {
					removeClass(dl.current.answers[c], "open");
				}
				removeClass(dl.current, "open");
			}
			
			var openState = hasClass(this.parentNode, "open");
			for (n=0; n<this.parentNode.answers.length; n++) {
				openState ? removeClass(this.parentNode.answers[n], "open") : addClass(this.parentNode.answers[n], "open")
			}
			openState ? removeClass(this.parentNode, "open") : addClass(this.parentNode, "open")
			
			dl.current = this.parentNode;
		}
		
		feedTitelLabel.onmouseover = feedTitelLabel.onfocus = function() {
			addClass(this, "jsHover");
		}
		
		feedTitelLabel.onmouseout = feedTitelLabel.onblur = function() {
			removeClass(this, "jsHover");
		}
	}
	
	addClass(feed, "jsFeedOn");	// CSS hook to turn it on
}



/* =================================== */
/* === call functions on page load === */
/* =================================== */

/* --- call functions only if the used methods are supported --- */
if (document.getElementById && document.createElement) {
	if (cssSupport()) {
		addLoadEvent(initFeed);
	}
}
