function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function picturewidth() {
	var divs=document.getElementsByTagName('div');
	for (i=0;i<divs.length;i++) {
		if (divs[i].className=='caption') {
			var picture=divs[i].parentNode;
			var images=picture.getElementsByTagName('img');
			picture.style.width=images[0].offsetWidth+'px';
		}	
	}
}

var allowDivOnClick = true;
function locations() {
	var map=document.getElementById('locationmap');
	if(map) {
		var locations=map.getElementsByTagName('div');
		for (var i=0;i<locations.length;i++) {
			locations[i].onmouseover = function() {
				this.style.cursor="pointer";
			}
			locations[i].onclick = function() { 
				if(allowDivOnClick) {
					if (this.className.indexOf("locHover") < 0) {
						// alles sluiten
						var locations=map.getElementsByTagName('div');
						for (var i=0;i<locations.length;i++) {
							locations[i].className=locations[i].className.replace(new RegExp(" locHover\\b"), "");
						}
						// 1 openen
						this.className+=" locHover";
						this.style.cursor="default";
					}
				} else {
					allowDivOnClick = true;
				}
			}
			em=locations[i].getElementsByTagName('em')[0];
			closelink=em.getElementsByTagName('a')[0];
			if(closelink) { 
				closelink.onclick=function() {
					this.parentNode.parentNode.className=this.parentNode.parentNode.className.replace(new RegExp(" locHover\\b"), "");
					allowDivOnClick = false;
					return false;
				}
			}
		}
	}
	var legende=document.getElementById('legende');
	if(legende) {
		var checkboxes = legende.getElementsByTagName('input');
		for(i=0; i<checkboxes.length; i++) {
			checkboxes[i].onclick = function() {
				if(this.checked) {
					showLocations(this.value);
				} else {
					hideLocations(this.value);
				}
			}
		}
	}
}

function showLocations(type) {
	var map=document.getElementById('locationmap');
	if(map) {
		var locations=map.getElementsByTagName('div');
		for (var i=0;i<locations.length;i++) {
			if(locations[i].className.indexOf(type) >= 0) {
				locations[i].style.display = "block";
			}	
		}
	}
}


function hideLocations(type) {
	var map=document.getElementById('locationmap');
	if(map) {
		var locations=map.getElementsByTagName('div');
		for (var i=0;i<locations.length;i++) {
			if(locations[i].className.indexOf(type) >= 0) {
				locations[i].style.display = "none";
			}	
		}
	}
}

function fotos() {
	var fotoDiv = document.getElementById("fotos");
	if(fotoDiv) {
		var fotos=fotoDiv.getElementsByTagName('a');
		for(i=0; i<fotos.length; i++) {
			fotos[i].onclick = function() {
				winopts = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=auto,resizable=yes,copyhistory=0,width=700,height=525";
				openPopup(this.href,winopts);
				return false;
			}
		}
	}
}

function links() {
	var printlink=document.getElementById("printlink");
	if(printlink) {
		printlink.onclick=function() {
			print();
			return false;
		}
	}
	var links=document.getElementsByTagName("a");
	for(i=0;i<links.length;i++) {
		if (links[i].className.indexOf("extLink") >= 0) {
			links[i].onclick=function() {
				openPopup(this.href,"");
				return false;
			}
		}
	}
}

function openPopup(href,winopts) {
	smallwindow=window.open(href,"Popup",winopts);
	smallwindow.focus();
}

addLoadEvent(picturewidth);
addLoadEvent(locations);
addLoadEvent(fotos);
addLoadEvent(links);
