/* --- JavaScript --- */
/* --- General --- */

/* --- 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
}



///////////// CLICKABLE ITEMS /////////////

/* --- clickableItems --- make entire itemes clickable --- */
clickableItems = function() {
	var home = document.getElementById('container');
	if (!home) return;
	
	/* publications section -|- add section for every clickable item type */
	var uls = home.getElementsByTagName('ul');
	for (u=0; u<uls.length; u++) {
		var ul = uls[u];
		if (!hasClass(ul, "blokkenitems")) continue;
		
		var lis = ul.getElementsByTagName('li');
		for (l=0; l<lis.length; l++) {
			var li = lis[l];
			if (!hasClass(li, "linkHover")) continue;
			clickAllOver(li);
			//alert(clickAllOver(li));
		}
	}
	/* end publications section */
}

/* --- clickAllOver --- */
clickAllOver = function(node) {

	node.getUrl = function() {
		var url = this.getElementsByTagName('a')[0];
		return (url) ? url.href : false; 
	}
	
	node.url = node.getUrl();
	
	if (!node.url) return;
	
	node.onclick = function() {
		window.location.href = this.url;
	}
	
	node.onmouseover = function() {
		addClass(this, "jsHover");
	}
	
	node.onmouseout = function() {
		removeClass(this, "jsHover");
	}
}
/* =================================== */
/* === call functions on page load === */
/* =================================== */

/* --- call functions only if the used methods are supported --- */
if (document.getElementById && document.createElement) {
	addLoadEvent(clickableItems);
}