/*
	Last change date:	Dec 09, 2005
*/

/*
	For accessibility and other reasons this Javascript is supposed to work 'unobtrusive'.
	It's also seperated from the HTML-page as far as possible.
	On separating behaviour and structure: http://www.digital-web.com/articles/separating_behavior_and_structure_2/
	On 'modern' javascript: http://www.naarvoren.nl/artikel/modern_javascript/
	On memory leakage: http://novemberborn.net/javascript/event-cache/.
*/


/*
	Global variables, i.e. constants
*/
	var glb_directnaar_classname_show	= "actief";
	var glb_breadcrumb_classname_show	= "showbc";
	var glb_breadcrumb_classname_hide	= "hidebc";
	var glb_breadcrumb_show_li			= "showLI";
	var glb_breadcrumb_hide_li			= "hideLI";
	var glb_button_classname_mouseover	= "button_mouseover";
	var glb_button_classname_mouseout	= "button_mouseout";
	
	var CONST_POPUP_SMALL	= 1
	var CONST_POPUP_DEFAULT	= 2

/*
	startApplication()
	Starts Javascript application
*/
function startApplication() {
	//  Include a stylesheet with styles that are only visible to clients with javascript enabled
	document.write('<link rel="stylesheet" type="text/css" href="./css/js_hide.css" />');
	addEvent(window, 'load', init, false);
	addEvent(window, "unload", EventCache.flush, false);
}

/*
	addEvent()
	Adds event-listeners to objects (html-elements)
*/
function addEvent(obj, evType, fn, useCapture){
	var result;
	if (obj.addEventListener){
		obj.addEventListener(evType, fn, useCapture);
		result = true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent('on' + evType, fn);
		result = r;
	} else {
		elm['on' + evType] = fn;
	}
    EventCache.add(obj, evType, fn, useCapture);
    return result;
}

/*
	init()
	Determine objects with listeners
	N.B. These directly correspond to ID's in HTML page!
*/
function init() {
	if (!document.getElementById) return;
	var directto = document.getElementById('directtoclickzone');
	var uldirectto = document.getElementById('uldirectto');
	if (directto && uldirectto) {
		addEvent(directto, 'click', toggleDirectTo, false);
		addEvent(directto, 'mouseover', function() {clearTODirectTo(uldirectto);}, false);
		addEvent(directto, 'mouseout', function() {hideDirectTo(uldirectto);}, false);
		addEvent(uldirectto, 'mouseout', function() {hideDirectTo(uldirectto);}, false);
		addEvent(uldirectto, 'mouseover', function() {showDirectTo(uldirectto);}, false);
	}
	var directnaar = document.getElementById('directnaar');
	if (directnaar) {
		addEvent(directnaar, 'change', handleDirectTo, false);
		addEvent(directnaar, 'focus', function() {changeDirectTo(glb_directnaar_classname_show);}, false);
		addEvent(directnaar, 'blur', function() {changeDirectTo('');}, false);
	}
	var breadwrapper = document.getElementById('breadwrapper');
	var breadcrumb = document.getElementById('breadcrumb');
	var breadcrumbimg = document.getElementById('breadcrumbimg');
	var breadcrumblist = document.getElementById('breadcrumblist');
	if (breadwrapper && breadcrumb && breadcrumbimg && breadcrumblist) {
		addEvent(breadcrumb, 'click', toggleBreadcrumb, false);
	}
	if (breadcrumb && breadcrumbimg) {
		addEvent(breadcrumb, 'mouseover', function() {if(breadcrumb.className == glb_breadcrumb_classname_show) {breadcrumbimg.style.marginTop = "-90px"} else {breadcrumbimg.style.marginTop = "-30px"} }, false);
		addEvent(breadcrumb, 'mouseout', function() {if(breadcrumb.className == glb_breadcrumb_classname_show) {breadcrumbimg.style.marginTop = "-60px"} else {breadcrumbimg.style.marginTop = "0px"} }, false);
	}
	var printpage = document.getElementById('printpage');
	if (printpage) {
		addEvent(printpage, 'click', printPage, false);
	}
	var closewindow = document.getElementById('closewindow'); /* close button in popup window */
	if (closewindow) {
		addEvent(closewindow, 'click', function() {if (window.close) window.close();}, false);
	}
	var closewindow = document.getElementById('closewindow'); /* close button in popup window */
	if (closewindow) {
		addEvent(closewindow, 'click', function() {if (window.close) window.close();}, false);
	}
	var zoektussenpersoon = document.getElementById('zoektussenpersoon');
	if (zoektussenpersoon) {
		addEvent(zoektussenpersoon, 'mouseover', function() {changeClassName(zoektussenpersoon, true);}, false);
		addEvent(zoektussenpersoon, 'mouseout', function() {changeClassName(zoektussenpersoon, false);}, false);
	}
}

/*
	changeClassName()
	Change classname of given object
*/
function changeClassName(obj, bln_onmouseover) {
	if (bln_onmouseover) {
		obj.className = glb_button_classname_mouseover;
	} else {
		obj.className = glb_button_classname_mouseout;
	}
}

/*
	changeDirectTo()
	Change appearance of directNaar select box
*/
function changeDirectTo(classnaam) {
	document.getElementById('directnaar').className = classnaam;
}

/*
	handleDirectTo()
	Redirect client to selected website
*/
function handleDirectTo() {
	var directnaar = document.getElementById('directnaar');
	var selurl = directnaar.options[directnaar.selectedIndex].value;
	if (selurl != "") location.href = selurl;
}

/*
	toggleBreadcrumb()
	Show or hide the breadcrumb path
*/
function toggleBreadcrumb() {
	var breadwrapper = document.getElementById('breadwrapper');
	var breadcrumb = document.getElementById('breadcrumb');
	var breadcrumbimg = document.getElementById('breadcrumbimg');
	var breadcrumblist = document.getElementById('breadcrumblist');
	
	if (breadcrumb.className == glb_breadcrumb_classname_show) {
		breadwrapper.className = glb_breadcrumb_classname_hide;
		breadcrumb.className = glb_breadcrumb_classname_hide;
		toggleListItems(breadcrumblist, glb_breadcrumb_show_li, glb_breadcrumb_hide_li);
/*
		// Nasty hack to fix gecko-bug that won't resize main document:
		document.getElementById('main').style.display = "none";
		document.getElementById('main').style.display = "block";
*/
		breadcrumbimg.style.marginTop = "0px";
	} else {
		breadwrapper.className = glb_breadcrumb_classname_show;
		toggleListItems(breadcrumblist, glb_breadcrumb_hide_li, glb_breadcrumb_show_li);
		breadcrumb.className = glb_breadcrumb_classname_show;
		breadcrumbimg.style.marginTop = "-60px";
	}
}

/*
	toggleListItems()
	Change the className of certain LI-tags
*/
function toggleListItems(ullist, oldClassName, newClassName) {
	var list = ullist.childNodes;
	for (var i = 0; i < list.length; i++) {
		if (list[i].nodeName.toLowerCase() == "li") {
			if (list[i].className == oldClassName) {
				list[i].className = newClassName;
			}
		}
	}
}

/*
	moveBreadcrumbImg()
	Move the breadcrumb image so it seems like another image is loaded
*/
function moveBreadcrumbImg() {
	var bcimg = document.getElementById("breadcrumbimg");
}

/*
	openTargetInOpener()
	When popup contains hyperlinks these should be opened in the main window:
*/
function openTargetInOpener(new_url) {
	if (window.opener && (!opener.closed)) {
		window.opener.location.href = new_url;
		return false;
	}
	return true;
}

/*
	openWindow()
	Opens new window or pop-up window
	Mandatory parameters: target_url
	Optional parameters: popup_type, custom_width, custom_height, custom_toolbar (booleans)
	Note:
	- With no given popup_type just a new window will be opened
*/
function openWindow(target_url, popup_type, custom_width, custom_height, custom_toolbar) {
	var str_options = "";
	switch (popup_type) {
		case CONST_POPUP_SMALL:
			if (!custom_height) custom_height = 300;
			if (!custom_width) custom_width = 400;
			if (!custom_toolbar) custom_toolbar = 0;
			str_options = "height=" + custom_height + ",menubar=0,resizable=1,scrollbars=1,status=0,left=200,top=50,toolbar=" + custom_toolbar + ",width=" + custom_width;
			break;
	}
	return !window.open(target_url, "", str_options);
}

/*
	printPage()
	Prints the current page
*/
function printPage() {
	if (window.print) window.print();
	return false;	
}

/*
	toggleDirectTo()
	Show or hide the direct to link box
*/
function toggleDirectTo() {
	var directto = document.getElementById('uldirectto');
	if (directto.style.display == "block") {
		directto.style.display = "none";
	} else {
		directto.style.display = "block";
	}
}

/*
	hideDirectTo()
	Hide the "direct to" link box
*/
function hideDirectTo(el) {
	if (!el) return;
	el.outTimeout = setTimeout(function() { hideElement(el); }, 300);
}

/*
	hideElement()
	Hide element
*/
function hideElement(el) {
	el.style.display = "none";
}

/*
	showDirectTo()
	Show (open) the "direct to" link box
*/
function showDirectTo(el) {
	if (!el) return;
	clearTimeout(el.outTimeout);
	el.style.display = "block";
}

/*
	clearTODirectTo()
	Clears the time-out of the "direct to" link box
*/
function clearTODirectTo(targetElement) {
	if (!targetElement) return;
	clearTimeout(targetElement.outTimeout);
}


/*	EventCache Version 1.0
	Copyright 2005 Mark Wubben

	Provides a way for automagically removing events from nodes and thus preventing memory leakage.
	See <http://novemberborn.net/javascript/event-cache> for more information.
	
	This software is licensed under the CC-GNU LGPL <http://creativecommons.org/licenses/LGPL/2.1/>
*/

/*	Implement array.push for browsers which don't support it natively.
	Please remove this if it's already in other code */
if(Array.prototype.push == null){
	Array.prototype.push = function(){
		for(var i = 0; i < arguments.length; i++){
			this[this.length] = arguments[i];
		};
		return this.length;
	};
};

/*	Event Cache uses an anonymous function to create a hidden scope chain.
	This is to prevent scoping issues. */
var EventCache = function(){
	var listEvents = [];
	
	return {
		listEvents : listEvents,
	
		add : function(node, sEventName, fHandler, bCapture){
			listEvents.push(arguments);
		},
	
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				
				/* From this point on we need the event names to be prefixed with 'on" */
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				
				item[0][item[1]] = null;
			};
		}
	};
}();

/*
	startApplication()
	start javascript application
	Note: Call must be placed at the end of the script file, to be sure that all JS-code is loaded!
*/
startApplication();