/* --- 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();
		}
	}
}

/* --- 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;
}

function withClass( name, sel, sel2 ) {														// sel and sel2 are both optional and can be either an element or a tag (one of each)
	var haveClass = [];
	var regExp = new RegExp( "(^|\\s)" + name + "(\\s|$)" );								// allows for multiple class names
	var tagName = sel.constructor == String ? sel : sel2;
	var elem = sel.constructor == String ? sel2 : sel;
	var elements = ( elem || document).getElementsByTagName( tagName || "*" );		// use document if no element has been specified and get all elements if no tag name has been specified
	for ( var e = 0; e < elements.length; e++) {
		var element = elements[ e ];
		if ( hasClass( element, name )) haveClass[ haveClass.length ] = element;
	}
	return haveClass;																					// -> Array
};

/* --- get first ancestor that matches the property --- */
function getAncestor(node, property, value) {
	var parent = node;
	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
			
	} while (parent.parentNode && parent.parentNode.nodeName != "HTML");
}

/* --- 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 === */
/* =============================== */


///////////// INIT CLICKABLE ITEMS /////////////

/* --- clickableItems --- make entire itemes clickable --- */
function initClickableItemsMainContent() {
	var items;
	
	// content section -|- add section for every clickable item type
	var content = document.getElementById( "mainContent" );
	if ( content ) {
		items = withClass( "clickableItem", "div", content );
		if ( items && items.length > 0 ) clickAllOver( items );
	}
	// end content section
}

function initClickableItemsSubContent() {
	var items;
	
	// content section -|- add section for every clickable item type
	var content = document.getElementById( "subContent" );
	if ( content ) {
		items = withClass( "clickableItem", "div", content );
		if ( items && items.length > 0 ) clickAllOver( items );
	}
	// end content section
}

/* --- clickAllOver --- */
function clickAllOver( elems ) {													// elem can be a single element or an array of elements
	var items = ( elems.constructor == Array ) ? elems : [ elems ];	// if elem is a single element, put it in an array

	for ( var i = 0; i < items.length; i++ ) {
		var item = items[ i ];
		
		item.getUrl = function() {
			var url = this.getElementsByTagName( "a" )[0];
			return ( url ) ? url.href : false; 
		}
		
		item.url = item.getUrl();
		if ( !item.url ) return;
		
		item.onclick = function() { window.location.href = this.url };
		item.onmouseover = function() { addClass( this, "jsHoverItem" ) };
		item.onmouseout = function() { removeClass( this, "jsHoverItem" ) };
		
		addClass( item, "jsClickable" );
	}
}


/* --- call functions only if the used methods are supported --- */
if (document.getElementById && document.createElement) {
	///// voeg hier functies toe aan de window.onload-event met de volgende aanroep: addLoadEvent(functienaam); /////
	if (cssSupport()) {
		addLoadEvent(initClickableItemsSubContent);
		addLoadEvent(initClickableItemsMainContent);
		
	}
}