var thumbstoshow = Array();
thumbstoshow[0] = 0;
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_setTextOfLayer(objName,x,newText) { //v4.01
  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (document.layers) {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
Array.prototype.inArray = function (value) {
	for (var i=0; i < this.length; i++) {
		if (this[i] == value) return true;
	}
	return false;
};
function createRequestObject() {
	if (window.XMLHttpRequest)
		return new XMLHttpRequest();
	else if (window.ActiveXObject)
		return new ActiveXObject("Microsoft.XMLHTTP");
	else
		return null;
}
function ajaxProcessImages(file) {
	var ro = createRequestObject();
	ro.onreadystatechange = function() {
		if (ro.readyState == 4) {
			processImages(ro.responseXML);
		}
	}
	ro.open ('GET', file, true);
	ro.send ('');
}
function ajaxProcessList(file) {
	var ro = createRequestObject();
	ro.onreadystatechange = function() {
		if (ro.readyState == 4) {
			processList(ro.responseXML);
		}
	}
	ro.open ('GET', file, true);
	ro.send ('');
}
function ajaxProcessText(file) {
	var ro = createRequestObject();
	ro.onreadystatechange = function() {
		if (ro.readyState == 4) {
			processText(ro.responseXML);
		}
	}
	ro.open ('GET', file, true);
	ro.send ('');
}
function show_props(obj, obj_name) {
	var result = ""
	for (var i in obj)
		result += obj_name + "." + i + " = " + obj[i] + "<br />\n"
	return result
}
function getmin(n1,n2) {
	if (Number(n1) < Number(n2)) return n1;
	return n2;
}
function processImages(obj) {
	var dataArray = obj.getElementsByTagName('image');
	var start = thumbstoshow[thumbstoshow.length-1];
	var theminmax = getmin(start+999,dataArray.length);

	if (! document.getElementById("thumbtable")) {
		return;
	}
	// remove all images currently present
	for (var i = document.getElementById("thumbtable").getElementsByTagName("img").length; i > 0; i--) {
		var curr = document.getElementById("thumbtable").getElementsByTagName("img")[i-1];
		document.getElementById("thumbtable").removeChild(curr);
	}

	var availableforthumbnails = screen.width;
	if (self.innerWidth) // all except Explorer
		availableforthumbnails = self.innerWidth;
	else if (document.documentElement && document.documentElement.clientWidth) // IE6 strict
		// Explorer 6 Strict Mode
		availableforthumbnails = document.documentElement.clientWidth;
	else if (document.body) // other Explorers
		availableforthumbnails = document.body.clientWidth;

	// subtract 16px to allow for vertical scrollbar
	availableforthumbnails -= 16;
	// reserve 166 pixels for the possible prevnext
	availableforthumbnails -= 150;

	// create new thumbnail images and table
	var stilltoset = true; // trick to make sure we can always safely pop twice
	for (var i = start; i < theminmax; i++) {
		var plaatje = dataArray[i];
		var thumb    = plaatje.getElementsByTagName("thumb")[0].childNodes[0].nodeValue;
		var bigimage = plaatje.getElementsByTagName("bigimage")[0].childNodes[0].nodeValue;
		var desc = '&nbsp;';
		try {
			desc = plaatje.getElementsByTagName("desc")[0].childNodes[0].nodeValue;
		} catch(e) {};
		desc = desc.replace(/[\r\n]/g,"<br/>");
//		desc = desc.replace(/[\r\n]+/g,"<br/>");
		var thewidth = plaatje.getElementsByTagName("thumb")[0].getAttribute('width');

		availableforthumbnails -= parseInt(thewidth);
		availableforthumbnails -= 16;

		if (availableforthumbnails < 0) {
			theminmax = 0;
			thumbstoshow.push(i);
			stilltoset = false; // trick should not be applied !
			continue;
		}
		var theimg = document.createElement("img");
		theimg.style.display = 'inline';
		theimg.src = thumb
		theimg.width = thewidth;
		theimg.height = plaatje.getElementsByTagName("thumb")[0].getAttribute('height');
		theimg.onmouseover = function(y1,y2) {
			return function() {
				document.getElementById('grootplaatje').src = y1;
				document.getElementById('desc').innerHTML = y2;
			}
		} (bigimage, desc);
		// theimg.style.borderLeft = '16px solid black';
		var showprevnext = document.getElementById("prevnext");
		document.getElementById("thumbtable").insertBefore(theimg,showprevnext);
		// make sure that the first image in the row is now displayed
		if (i == start) {
			document.getElementById("grootplaatje").src = bigimage;
//			document.getElementById("desc").nodeValue = desc;
			document.getElementById("desc").innerHTML = desc;
		}
	}
	if (stilltoset) thumbstoshow.push(0); // trick to make sure we can always safely pop twice
	// prev should be active when the start value is greater than 0
	if (start <= 0)
		document.getElementById("showprevthumbs").style.visibility = 'hidden';
	else
		document.getElementById("showprevthumbs").style.visibility = 'visible';
	// next should be active until dataArray.length wins the minmax comparison
	if (theminmax >= dataArray.length)
		document.getElementById("shownextthumbs").style.visibility = 'hidden';
	else 
		document.getElementById("shownextthumbs").style.visibility = 'visible';
	// don't show if there is no prev and no next
	if (start <= 0 && theminmax >= dataArray.length)
		document.getElementById("prevnext").style.display = 'inline';
	else
		document.getElementById("prevnext").style.display = 'inline';
	document.getElementById("thumbtable").style.display = 'block';
}
function processText(obj) {
	var dataArray = obj.getElementsByTagName('para');
	var out = '';
	if (! document.getElementById("textinclusion")) {
		return;
	}
	// remove all children currently present
	for (var i = document.getElementById("textinclusion").getElementsByTagName("ul").length; i > 0; i--) {
		var curr = document.getElementById("textinclusion").getElementsByTagName("ul")[i-1];
		document.getElementById("textinclusion").removeChild(curr);
	}
	for (var i = 0; i < dataArray.length; i++) {
		var thepara = dataArray[i];
		out += thepara.childNodes[0].nodeValue.replace(/\n/g, "<br/>");
	}
	document.getElementById("textinclusion").innerHTML = out;
}
function processList(obj) {
	var dataArray = obj.getElementsByTagName('subject');
	var out = '';
	if (! document.getElementById("textinclusion")) {
		return;
	}

	// remove all children currently present
	for (var i = document.getElementById("textinclusion").getElementsByTagName("ul").length; i > 0; i--) {
		var curr = document.getElementById("textinclusion").getElementsByTagName("ul")[i-1];
		document.getElementById("textinclusion").removeChild(curr);
	}
	for (var i = 0; i < dataArray.length; i++) {
		var thesubject = dataArray[i];
		out += '<p class="listheader">' + thesubject.getElementsByTagName("subjectname")[0].childNodes[0].nodeValue + "</p>";
		var thesubjectchildren = thesubject.childNodes;
		var testout = '';
		for (var j = 0; j < thesubjectchildren.length; j++) {
			if (thesubjectchildren[j].nodeType == 3) continue;
			if (thesubjectchildren[j].nodeName == 'item') {
				try { // suppress ie error...
					out += '-&nbsp;' + thesubjectchildren[j].childNodes[0].nodeValue.replace(/\n/g, "<br/>") + "<br/>";
				} catch(e) {}
			}
			else if (thesubjectchildren[j].nodeName == 'subdivision') {
				var thesubdivision = thesubjectchildren[j];
				var theyear = thesubdivision.getAttribute('year');
				out += '<span class="listsub">' + theyear + '</span><br/>\n';
				for (var k = 0; k < thesubdivision.getElementsByTagName("item").length; k++) {
					out += '-&nbsp;' + thesubdivision.getElementsByTagName("item")[k].childNodes[0].nodeValue.replace(/\n/g, "<br/>") + "<br/>";
				}
				out += "<br/>";
			}
		}
	}
	document.getElementById("textinclusion").innerHTML = out;
}
function showthumbs() {
	ajaxProcessImages(xmlinfofile);
}
function showList() {
	ajaxProcessList(xmlinfofile);
}
function showText() {
	ajaxProcessText(xmlinfofile);
}
function showprevnext(prevornext) {
	if (prevornext < 0) {
		thumbstoshow.pop(); thumbstoshow.pop();
	}
	showthumbs();
}
navHover = function() {
	var lis = document.getElementById("navmenu").getElementsByTagName("LI");
	for (var i=0; i<lis.length; i++) {
		lis[i].onmouseover=function() {
			this.className +=" iehover";
		}
		lis[i].onmouseout=function() {
			this.className = this.className.replace(new RegExp(" iehover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", navHover);
