/*
Generic functions
Copyright (c) 2003-2009 Ylab, www.ylab.nl
*/

//generic event handling
window.onerror = reportError;
function reportError(msg, url, line){
	window.status = "Er is een fout opgetreden. Meld dit a.u.b. aan de webmaster. | " + line + ": " + msg;
	return true;
}

var scrollFunctions = new Array();
function addScrollFunction(f){scrollFunctions[scrollFunctions.length] = new Function(f);}
window.onscroll = function(){for(var i=0; i<scrollFunctions.length; i++){scrollFunctions[i]();}}

var loadFunctions = new Array();
function addLoadFunction(f){loadFunctions[loadFunctions.length] = new Function(f);}
window.onload   = function(){for(var i=0; i<loadFunctions.length; i++){loadFunctions[i]();}}

var isOpera = (navigator.userAgent.indexOf("Opera") > -1);
var isNav = ((!isOpera) && (navigator.appName == "Netscape"));
var isIE  = ((!isOpera) && (navigator.appName.indexOf("Explorer") > -1 ));
var versionMajor = parseInt(navigator.appVersion);
if(isOpera){ var versionMinor = parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf("Opera")+6));}
else if(isIE){ var versionMinor = parseFloat(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5));}
else{ var versionMinor = parseFloat(navigator.appVersion);}
var hasFirebug = (window.console && console.firebug);

//hide horizontal scrollbar
if((isIE) && (versionMinor >= 6)){addLoadFunction("document.documentElement.style.overflowX = 'hidden';");}
else if((isIE) && (versionMinor >= 5.1)){addLoadFunction("document.body.style.overflowX = 'hidden';");}
//----------------------------------------------------------------------

//preload hover images when document is loaded
var hoverImages = new Array();
addLoadFunction("if(hoverImages.length==0){return;} for(var i=0; i<hoverImages.length; i++){(new Image()).src = hoverImages[i];}");

/*Prepare image for hover effect
	Sample use:
	<img onload="imgHover('img01')">
	<img onload="imgHover(this)">
	<img onload="imgHover(this, 'hover.gif')">
	<img onload="imgHover(this, 'text=image')">

	img: image object or image id;
	srcHover: url of hover image [optional]
					 : when omitted, the postfix "-hover" is added to the original image path
					 : when containing an is-character (=) a part of the original url is altered (e.g. 'text=image')
*/
function imgHover(img, srcHover, hotspot){
	//Initialize only once, and not when debugging
	img = id2object(img);
	if(img.initialized || hasFirebug){return;}

	//Set properties
	img.initialized = true;//been here
	img.srcNormal = img.src;
	if(srcHover){
		if(srcHover.indexOf("=") != -1){
			//replace part of original url
			var pair = srcHover.split("=");
			img.srcHover=img.src.replace(pair[0],pair[1])
		}
		else{img.srcHover = srcHover;}
	}
	else{
		//add -hover by default
		var extpos = img.src.lastIndexOf(".");
		img.srcHover = img.src.substring(0,extpos) + "-hover" + img.src.substring(extpos);
	}
	hoverImages[hoverImages.length] = img.srcHover;
	//Assign hotspot events
	if(arguments.length < 3){var el=img;}
	else{var el = id2object(hotspot)}
	el.onmouseover = function(){img.src = img.srcHover;}
	el.onmouseout  = function(){img.src = img.srcNormal;}

}
//display mailto link
function printMail(username, linktext){
	var hostLevels = location.host.split('.');
	var len = hostLevels.length;

	username = username.toLowerCase() + '@' + hostLevels[len-2] + '.' + hostLevels[len-1];
	if(!linktext){
		linktext = username;
	}
	document.write(linktext.link("mailto:" + username));
}

//Convert id into object
function id2object(el){
	if(typeof(el)=="string"){el = document.getElementById(el);}
	return el;
}

//set a style property or fail gracefully
function setStyle(objectId, prop, value){
	var obj = document.getElementById(objectId);
	if((obj) && (obj.style[prop] != value)){
		obj.style[prop] = value;
	}
}

function Viewport(){
	this.x = 0;
	this.y = 0;
	if(self.innerHeight){
		// all except Explorer
		this.x = self.innerWidth;
		this.y = self.innerHeight;
	}
	else if(document.documentElement && document.documentElement.clientHeight){
		// Explorer 6 Strict Mode
		this.x = document.documentElement.clientWidth;
		this.y = document.documentElement.clientHeight;
	}
	else if(document.body){
		// other Explorers
		this.x = document.body.clientWidth;
		this.y = document.body.clientHeight;
	}
}

function slideSheet(id){
	var sheet = id2object("sheet"+id);
	if(sheet.style.display=='block'){
		return;//nothing to do
	}
	var slider = id2object("slider");
	if(!slider){return;}
	var sheets = slider.getElementsByTagName('div');
	for(var i=0; i<sheets.length; i++){
		if(sheets[i].className == 'sliderSheet' && sheets[i].style.display != 'none'){
			sheets[i].style.overflow = 'hidden';
			sheets[i].newHeight = sheets[i].defaultHeight;
			decreaseHeight(sheets[i].id);
		}
	}
	sheet.newHeight = 0;
	sheet.style.height = 0;
	sheet.style.display = 'block';
	sheet.style.overflow = 'hidden';
	restoreHeight("sheet"+id);
}

function decreaseHeight(objId){
	var obj = id2object(objId);
	if(obj.newHeight < 10){
		obj.style.display = 'none';
		return;
	}
	obj.newHeight -= 10;
	obj.style.height = obj.newHeight + 'px';
	window.setTimeout('decreaseHeight("'+objId+'");', 10);
}

function restoreHeight(objId){
	var obj = id2object(objId);
	if(obj.newHeight > obj.defaultHeight){
		obj.style.overflow = 'auto';
		return;
	}
	obj.newHeight += 10;
	obj.style.height = obj.newHeight + 'px';
	window.setTimeout('restoreHeight("'+objId+'");', 10);
}

function setSlideHeigth(n){
	//n: number of sliders
	var viewport = new Viewport();
	var margin = 360 + (n * 18);
	var minimalHeight = margin + (n * 18);
	var h = Math.max(viewport.y, minimalHeight) - margin;
	var slider = id2object("slider");
	if(!slider){return;}
	var sheets = slider.getElementsByTagName('div');
	for(var i=0; i<sheets.length; i++){
		if(sheets[i].className == 'sliderSheet'){
			sheets[i].style.overflow = 'auto';
			sheets[i].style.height = h + 'px';
			sheets[i].defaultHeight = h;
		}
		else if(sheets[i].className == 'index selected' && !window.onresize){
			if(sheets[i].scrollIntoView){
				sheets[i].scrollIntoView();
			}
			if(document.documentElement && document.documentElement.scrollTop){
				document.documentElement.scrollTop=0;
			}
			else if(document.body.scrollTop){
				document.body.scrollTop=0;
			}
		}
	}
	if(!window.onresize){window.onresize = function(){setSlideHeigth(n);}}
}

function openWindow(url, name, width, height, resizable, toolbar, menubar){
	var args = "";
	if(width){
		width = Math.min(width, screen.availWidth);
		args += "width=" + width + ",left=" + (screen.width - width)/2 + ",";
	}
	if(height){
		height = Math.min(height, screen.availHeight);
		args += "height=" + height + ",top=" + (screen.height - height)/2 + ",";
	}
	if(resizable){args += "resizable,scrollbars,";}
	if(toolbar){args += "toolbar,";}
	if(menubar){args += "menubar,";}
	var newwin = window.open(url, name, args);
	if(!newwin){
		alert("Er kan geen nieuw venster geopend worden.\nEr is waarschijnlijk een 'popup-killer' actief.");
		return false;
	}
	if(newwin.focus){newwin.focus();}
	return newwin;
}

function openModalWindow(url, width, height, resizable, toolbar, menubar){
	window.modalwindow = openWindow(url, "modalwindow", width, height, resizable, toolbar, menubar);
	if(window.modalwindow){
		try{window.modalwindow.focus();}
		catch(ex){window.modalwindow = false; window.onfocus = null;}
	}
}

function closeModalWindow(refresh){
	if(window.opener){
		window.opener.modalwindow = false;
		window.onfocus = null;
		if(refresh){window.opener.location.reload();}
		window.opener.focus();
	}
	window.close();
}
