/* --- 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
}


/* =================================== */
/* ===== site specific functions ===== */
/* =================================== */


///////////// LECGROEP /////////////

lecGroep = function() {
	var lectricGroep = document.getElementById('lectricGroep');
	var tooltip = document.getElementById('tooltip');
	if (!lectricGroep || !tooltip) return;
		
	tooltip.onmouseover = function() { showTip(true) };
	tooltip.onmouseout = function() { showTip(false) };
	
	var lecGroepLinks = lectricGroep.getElementsByTagName('a');
	for (a=0; a<lecGroepLinks.length; a++) {
		var lecGroepLink = lecGroepLinks[a];
		
		var tipParent = getAncestor(lecGroepLink, "nodeName", "DIV", 2);
		if (!tipParent) continue;
		
		lecGroepLink.onmouseover = function() { showTip(true) };
		lecGroepLink.onmouseout = function() { showTip(false) };
		break;
	}
}

function showTip(show) {
	var parent = document.getElementById('lectricGroep');
	show ? addClass(parent, "showTooltip") : removeClass(parent, "showTooltip");
}


///////////// OVERLABEL /////////////

/* --- overLabel --- place labels over corresponding form control --- */
/* --- based on http://www.alistapart.com/articles/makingcompactformsmoreaccessible/ ---*/
overLabel = function() {
	var labels;
	if (!(labels = document.getElementsByTagName('label'))) return;
	
	for (l=0; l<labels.length; l++) {
		var label = labels[l];
		if (!(hasClass(label, "overLabel") && label.htmlFor)) continue;
		
		var overControl = document.getElementById(label.htmlFor);
		if (!overControl) continue;
		
		label.forControl = overControl;	// make reference from label to corresponding control
		
		if (overControl.value === "") addClass(overControl.parentNode, "inactive");	// make sure label is only placed on top of control in case it has no value which is not always the case after a reload
		
		overControl.onfocus = function() {
			removeClass(this.parentNode, "inactive");
		}
		
		overControl.onblur = function() {
			if (this.value === "") addClass(this.parentNode, "inactive");
		}
		
		label.onclick = function() {	// needed for Safari
			this.forControl.focus();	// give focus to corresponding control
		}
	}
	
	addClass(document.body, "jsLabelsOn");	// CSS hook to turn it on
}
		
clickEmpty = function() {
	var parentSelect = this.parentNode;
	parentSelect.options[0] = parentSelect.labelOption;
	parentSelect.options[0].selected = "selected";
}


///////////// EASY POPUPS /////////////

easyPopups = function() {
	var links = document.getElementsByTagName('a');
	pop(links);
	
	var areas = document.getElementsByTagName('area');
	pop(areas);
}

function pop(pops) {
	for (p=0; p<pops.length; p++) {
		var pop = pops[p];
		if (pop.rel && pop.rel == "extern")
		{
			pop.onclick = function() {
				window.open(this.href, '_blank');
				return false;
			}
		}
	}
}


///////////// ADD JS BUTTONS /////////////

addJSbuttons = function() {
	addJSprint();
}

function addJSprint() {
	/*var pageTools = document.getElementById('pageTools');
	if (!pageTools) return;
	
	var toolMenu = pageTools.getElementsByTagName('ul')[0];
	if (!toolMenu) return;
	
	var printButton = '<li class="print"><a href="javascript:window.print();">Print</a></li>';
	toolMenu.innerHTML = printButton + toolMenu.innerHTML;*/
}


/* =================================== */
/* === call functions on page load === */
/* =================================== */

/* --- call functions only if the used methods are supported --- */
if (document.getElementById && document.createElement) {
	addLoadEvent(addJSbuttons);
	if (cssSupport()) {
		addLoadEvent(lecGroep);
		addLoadEvent(overLabel);
	}
	
	/*THIS IS DISABLED BECAUSE IT DOES NOT OCCUR ANYMORE*/
	//addLoadEvent(easyPopups);
}
