function htmlSubstr(htmlString,maxLength) {
// htmlSubstr is XHTML compliant.
// It is case sensitive and empty HTML elements like <br> should be written <element/>.

	if (htmlString != null && htmlString != "" && maxLength != null && maxLength != "") {        
		isText = true;
		var r = "";
		var i = 0;
		
		var currentChar = "";
		var lastSpacePosition = -1;
		var lastChar = "";
		
		var tagsArray = new Array();
		var currentTag = "";
		var tagLevel = 0;
	
		var noTagLength = 0;
		
		// Calculate string length without tags
		for (j=0;j<htmlString.length;j++) {
			currentChar = htmlString.charAt(j)
			if (currentChar == "<") { isText = false; }
			if (isText) { noTagLength++ }
			if (currentChar == ">") { isText = true; }    
		}
		
		// Parser loop
		for (j=0;j<htmlString.length;j++) {
			currentChar = htmlString.charAt(j);
			r += currentChar; 		
			if (currentChar == "<") { isText = false; }
			if (isText) {
				if (currentChar == " ") { lastSpacePosition = j; } else { lastChar = currentChar; }
				i++;
			} else {
				currentTag += currentChar;
			}
			if (currentChar == ">") {
				isText = true
				// Opening tag handler
				if (currentTag.indexOf("<") != -1 && currentTag.indexOf("/>") == -1 && currentTag.indexOf("</") == -1) {	
					// Tag has attribute(s)
					if (currentTag.indexOf(" ") != -1) {
						currentTag = currentTag.substr(1,currentTag.indexOf(" ")-1);
					} else {
						// Tag doesn't have attribute(s)
						currentTag = currentTag.substr(1,currentTag.length-2);
					}
		
					tagsArray[tagLevel] = currentTag;
					tagLevel++;
		
				} else if (currentTag.indexOf("</") != -1) {
					// Closing tag handler
					tagsArray[tagLevel-1] = null;
					tagLevel--;
				}
				currentTag = "";
			}
			if (i == maxLength) { break; }
		}
					
		// Cut HTML string at last space position
		if (maxLength < noTagLength) {	      
			if (lastSpacePosition != -1) {
				r = htmlString.substr(0,lastSpacePosition);
			} else {
				r = htmlString.substr(0,j);
			}
		 }
	
		// Close broken XHTML elements
		for (a=tagsArray.length-1;a>=0;a--) {
			if(a == 0) {
				if (maxLength < noTagLength) {	      
					if (lastChar != ".") { r += "..."; } else { r += ".."; }
				}
			} else if (tagsArray[a] != null) { r += "</" + tagsArray[a] + ">"; }
		}
	
		return r;
	}
}

function spacify(string, delim) {
	var result = '';
	if(!delim) { delim = '_'; }
	var regex = new RegExp ("\\s+", 'g') ;
	var length = string.length;
	string = string.replace(regex, delim);
	for(var i = 0; i < length; i++) {
		if(string.substr(i, 1) != ' ') {
			result += ' ' + string.substr(i, 1);	
		}
	}
	return result;
}

function pause(ms) {
	var d = new Date(); //today's date
	while (1) {
		var mill = new Date(); // Date Now
		var diff = mill-d; //difference in milliseconds
		if(diff > ms) { break; }
	}
}

/*** specific functions ***/

var contentInitDone = false

function initContent() {
	if(!contentInitDone) {
		var elem = document.getElementsBySelector('div#linkedtext div.section');
		if(elem.length < 1) { return false; }
		for(var i = 0; i < elem.length; i++) {
			if(elem[i].id) {
				var para = elem[i];
				var morelink = document.getElementById('show_' + para.id);
				if(para && morelink) {
					var clone = morelink.cloneNode(true);
					para.removeChild(morelink);		
					if(para.innerHTML.length > 500) {
						para.oldHTML = para.innerHTML;
						para.innerHTML = htmlSubstr(para.innerHTML, 500);		
						clone.para = para;
						clone.para.full = false;
						clone.onclick = function() {
							if(this.para.full) {
								this.para.innerHTML = htmlSubstr(this.para.innerHTML, 500);				
								this.innerHTML = showmore + ' &raquo;';
								this.para.appendChild(this);								
							} else {
								this.para.innerHTML = this.para.oldHTML;
								this.innerHTML = '&laquo; ' + showless;
								this.para.appendChild(this);
							}
							this.para.full = !this.para.full;
							return false;
						}		
						para.appendChild(clone);
					}
				}
			}
		}
		contentInitDone = true;
		return true;
	}
	return false;
}

function toggleView() {
	var fs = document.getElementById('fullscreen');
	if(fs) {	
		var main = document.getElementById('main');
		if(main) { 
			if(main.style.display == 'none') {
				main.style.display = 'block';
			} else {
				main.style.display = 'none';
			}
		}
		
		var tools = document.getElementById('tools');
		if(tools) { 
			if(tools.className == '') {
				tools.className = 'centered';
			} else {
				tools.className = '';
			}
		}
		
		var link_zoom = document.getElementById('link_zoom');
		if(link_zoom) {
			if(link_zoom.className == '') {
				link_zoom.className = 'active';
			} else {
				link_zoom.className = '';
			}
		}
		
		if(fs.style.display == 'block' || fs.style.display == null) {
			fs.style.display = 'none';	
		} else {
			fs.style.display = 'block';	
		}
	}
	return false;
}

function toggleContent(link) {
	var content = document.getElementById('content');
	var submenu = document.getElementById('submenu');
	if(content && submenu) { 
		if(content.style.display == 'block' || content.style.display == null) {
			if(link) { link.parentNode.className = ''; }
			content.style.display = 'none';		
		} else {		
			if(link) { link.parentNode.className = 'active'; }		
			if(initContent()) { 
				// initially let user wait on purpose
				submenu.className = 'concat wait';
				setTimeout(delayedContent, 1200);
			} else {
				content.style.display = 'block';
			}
			return true;
		}
	}
	return false;
}

function delayedContent() {
	var content = document.getElementById('content');
	var submenu = document.getElementById('submenu');
	if(content && submenu) { 
		submenu.className = 'concat';
		content.style.display = 'block';	
	}
}

function toggleContrast(obj, over) {
	if(obj.className == 'contrast' && !over) {
		obj.className = '';
	} else {
		obj.className = 'contrast';
	}
}

browser = new browserCheck();

function initPage() {	
	if(document.getElementById) {
		// browser supports cookies
		var lang_nl = document.getElementById('elem_lang_nl');
		var lang_en = document.getElementById('elem_lang_en');
		if(lang_nl && lang_en) {
			var splash_lang = document.getElementById('splash_lang');			
			delCookie('checkcookie');	
			setCookie('checkcookie', 1, 1);
			if(getCookie('checkcookie')) {
				lang_nl.onclick = function() {
					document.location =  '/view/nl.html';
					return false;
				}
				lang_en.onclick = function() {
					document.location =  '/view/en.html';
					return false;
				}				
				if(splash_lang && splash_lang.className) {
					splash_lang.onclick = function() {
						document.location =  '/view/' + splash_lang.className + '.html';
						return false;
					}
				}			
			}
		}	
				
		// toggle fullscreen or content initially if requested
		var elem = document.getElementsBySelector('div#fullscreen');
		if(elem.length > 0) {
			if(document.location.hash == '#fullscreen') {
				toggleView();
			} else if(document.location.hash == '#content') {
				toggleContent(document.getElementById('link_info'));
			}
		}
		
		// spacify the nav elements, except for opera
		if(!browser.opera) {
		  var curr = document.getElementById('elem_current');
		  if(curr) { curr.innerHTML = spacify(curr.innerHTML, '.'); }
			var elem = document.getElementsBySelector('div.concat ul li a');
			for(var i = 0; i < elem.length; i++) {
				if(elem[i].innerHTML) {
					elem[i].innerHTML = spacify(elem[i].innerHTML, '.');
				}
			}
		}
		
		// if done show the container
		var elem = document.getElementsBySelector('div.concat');
		for(var i = 0; i < elem.length; i++) {
			elem[i].style.visibility = 'visible';
		}
			
		// attach toggleView() to zoom link and main image
		var elem = document.getElementsBySelector('.togglefs');
		for(var i = 0; i < elem.length; i++) {
			elem[i].onclick = function() {				
				return toggleView();
			}
		}
		
		// attach toggleContent() to info link
		var link_info = document.getElementById('link_info');
		if(link_info) {
			link_info.onclick = function() {
				return toggleContent(this);
			}
		}
		
		// attach toggleView() to zoom link and main image
		var elem = document.getElementById('content');
		if(elem) {
			elem.onmouseover = function() {				
				toggleContrast(this, true);
			}
			elem.onmouseout = function() {				
				toggleContrast(this);
			}
		}
		
		// attach switch behaviour to tools
		var elem = document.getElementsBySelector('div#tools ul li.link_img a');
		for(var i = 0; i < elem.length; i++) {
			elem[i].onclick = function() {
				if(document.getElementById('fullscreen').style.display == 'block') {
					document.location = this.href + '#fullscreen';
					return false;
				} else if(document.getElementById('content').style.display == 'block') {
					document.location = this.href + '#content';
					return false;
				}
				return true;
			}
		}
		
		// attach openWin to external links
		var elem = document.getElementsBySelector('a.external');
		for(var i = 0; i < elem.length; i++) {			
			elem[i].onclick = function() {
				openWin(this.href, this.title, 800, 600);
				return false;
			}
		}
	}		
}

addLoadEvent(initPage);

if(!browser.opera) { 
	// write the rest of the css through javascript, so it works OK when disabled
	document.write('<style type="text/css" media="all">');
	if(/Konqueror|Safari|KHTML/.test(navigator.userAgent)) {
		document.write('div.concat ul, div.concat ul li { display: inline; margin-right: 0.25ex; }');
		document.write('div.concat { visibility: hidden; word-spacing: -.75ex; text-align: justify; }');
	} else {
		document.write('div.concat ul, div.concat ul li { display: inline; }');
		document.write('div.concat { visibility: hidden; word-spacing: -1ex; text-align: justify; }');
	}
	document.write('div#submenu { word-spacing: -.5ex; }');
	document.write('* html div.concat { word-spacing: -.6ex; }');
	document.write('* html div#submenu{ word-spacing: -.3ex; }');
	document.write('body.action_project_show div#content, body.action_publications_show div#content { display: none; }');
	document.write('div.section a.more { display: inline; }');
	document.write('</style>');
}