/* --- JavaScript --- */
/* --- General --- */


/* --- add functions to onload event: addLoadEvent(functionName); --- */
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/remove className --- */
function addClass(thisNode,thisClass) {
	removeClass(thisNode,thisClass);	// make sure there won't be any doubles
	thisNode.className += " " + thisClass;
}

function removeClass(thisNode,thisClass) {
	if (thisNode.className.length == (thisClass.length)) {
		thisNode.className = thisNode.className.replace(thisClass,"");
	}
	else {
		thisNode.className = thisNode.className.replace(" " + thisClass,"");
	}
}

/* --- check if node has a certain class --- */
function hasClassName(thisNode,thisClass) {
	var nodeClass = thisNode.className;
	if (!thisClass && nodeClass != "") return true;
	if (thisClass && nodeClass.indexOf(thisClass) > -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] == thisClass) return true
		}
	}
	return false;
}

/* --- 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
}

/* --- place labels over corresponding form field --- */
overLabel = function() {
	var labels;
	if (!(labels = document.getElementsByTagName('label'))) return false;
	for (l=0; l<labels.length; l++) {
		var label = labels[l];
		if (!(hasClassName(label,"overLabel") && label.htmlFor)) continue;
		var overInput;
		if (!(overInput = document.getElementById(label.htmlFor))) continue;
		addClass(overInput.parentNode,"overLabelOn");
		label.forInput = overInput;	// make reference from label to corresponding input
		if (overInput.value === "") addClass(overInput.parentNode,"inactive");	// make sure label is only placed on top of input in case it has no value which is not always the case after a reload
		overInput.onfocus = function() {
			removeClass(this.parentNode,"inactive");
		}
		overInput.onblur = function() {
			if (this.value === "") addClass(this.parentNode,"inactive");
		}
		label.onclick = function() {	// needed for Safari
			this.forInput.focus();	// give focus to corresponding input
		}
	}
}

/* --- JS alternative for opening lectricGroep tooltip for browsers that don't support :hover on other elements than <a> --- */
lectricTooltip = function() {
	var ttAnchor;
	if (!(ttAnchor = document.getElementById('tooltipAnchor'))) return false;
	
	ttAnchor.onmouseover = function() {
		addClass(this,"jsHover");	
	}
	ttAnchor.onfocus = function() {
		addClass(this,"jsHover");
	}
	
	ttAnchor.onmouseout = function() {
		removeClass(this,"jsHover");
	}
	ttAnchor.onblur = function() {
		removeClass(this,"jsHover");
	}
	
	/* --- to make IE6 do it's job --- */
	var ttAnchorLinks;
	if (!(ttAnchorLinks = ttAnchor.getElementsByTagName('a'))) return false;
	for (l=0; l<ttAnchorLinks.length; l++) {
		var ttAnchorLink = ttAnchorLinks[l];
		ttAnchorLink.tooltip = ttAnchor;
		ttAnchorLink.onfocus = function() {
			addClass(this.tooltip,"jsHover");
		}
		ttAnchorLink.onblur = function() {
			removeClass(this.tooltip,"jsHover");
		}
	}
}


/* --- make the whole banner clickable, using the URL of header link --- */
clickableBanner = function() {
	var directContact, bannerUrl;
	if(!(directContact = document.getElementById('directContact'))) return false;
	if (!(bannerUrl = findBannerUrl(directContact))) return false;
	directContact.url = bannerUrl;
	addClass(directContact,"jsClickable");
	directContact.onclick = function() {
		window.location.href = this.url;
		return false;
	}
	directContact.onmouseover = function() {
		addClass(this,"jsHover");
	}
	directContact.onmouseout = function() {
		removeClass(this,"jsHover");
	}
}

function findBannerUrl(banner) {
	var bannerUrl;
	return (bannerUrl = banner.getElementsByTagName('a')[0]) ? bannerUrl.href : false; 
}


/* --- call functions only if the used methods are supported --- */
if (document.getElementById && document.getElementsByTagName) {
	if (cssSupport()) {
		addLoadEvent(overLabel);
		addLoadEvent(lectricTooltip);
	}
	addLoadEvent(clickableBanner);
}
