/*
	graphical user interface scripts

*/

if (document.addEventListener) 
{
    document.addEventListener("DOMContentLoaded", init, null);
}

// fallback
window.onload = init;
	

function init()
{
	//make sure we only get here once
	if (arguments.callee.done)
	{
		return;
	}
	arguments.callee.done = true;

	prepare_menu();
	//startList();
	add_events_links();
	add_click_searchbox();
	add_anchor_events();
	highlite_results();
}

function highlite_results()
{
	var ref = new String(document.referrer);
	var re = /^http:\/\/.+\/zoeken\/([^\/]*)\/.+$/;
	var searchbox = document.getElementById('sq');
		
	if (ref.match(re) || searchbox)
	{
		var pagecontent = document.getElementById('content');
		var content = pagecontent.innerHTML;
		var words = '';

		if (searchbox)
		{
			words = searchbox.value;
			words = words.split(' ');
		}
		else
		{
			words = ref.replace(re, '$1');
			words = words.split('_');
		}
		
		for (var n = 0; n < words.length; n++)
		{
			var re = new RegExp('>([^<]*\\b)?('+words[n]+')(\\b[^>]*)?<', 'gi');
			content = content.replace(re, '>$1<span class="searchresult">$2</span>$3<');
		}
		pagecontent.innerHTML = content;
	}
}


function add_anchor_events()
{
	var content = document.getElementById('content');
	var links = content.getElementsByTagName('a');
	for (var n = 0; n < links.length; n++)
	{
		var l = new String(links[n].getAttribute('href'));
		if (l.indexOf('#') > -1)
		{
			links[n].onclick = function(){backlight(this)}
		}
	}
}

function backlight(alink)
{
	var re = /.*#(.*)/;
	var l = new String(alink.getAttribute('href'));
	name = l.replace(re, '$1');

	var content = document.getElementById('content');
	var links = content.getElementsByTagName('a');
	for (var n = 0; n < links.length; n++)
	{
		links[n].parentNode.style.backgroundColor = 'transparent';
		if (links[n].getAttribute('name') == name)
		{
			var ln = links[n]; 
			ln.parentNode.style.backgroundColor = '#FFDE8F';
		}
	}
	
}



function startList() 
{
	if (document.all && document.getElementById) 
	{
		navRoot = document.getElementById("menu");
		for (i = 0; i < navRoot.childNodes.length; i++) 
		{
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") 
			{
				node.onmouseover=function() 
				{
					this.className = this.className + ' over';

				}
				node.onmouseout=function() 
				{ 

					this.className=this.className.replace(" over", ""); 
				}
			}
		}
	}
}


function add_events_links()
{
	var links = document.getElementsByTagName('a');
	var img = '&nbsp;<img src="/style/images/icons/newwindow.png" alt="(Deze link opent in een nieuw venster)" />';
	for (var i = 0; i < links.length; i++)
	{
		switch (links[i].className)
		{
			case 'skip':  
				links[i].onfocus = function () { show_skippy(this,true) }; 
				links[i].onblur = function () { show_skippy(this,false) };
				links[i].style.position = 'absolute';
				links[i].style.left = '-999em';
				break;
			case 'newwindow':
				links[i].setAttribute('title', links[i].getAttribute('title') + ', opent in een nieuw venster.');  
				links[i].onclick = newwin;
				links[i].innerHTML = links[i].innerHTML + img;
				break;
		}
	}
}

function show_skippy(skiplink,display)
{
	if (display)
	{
		skiplink.style.zIndex = '1000';
		skiplink.style.left = 'auto';
	}
	else
	{
		skiplink.style.position = 'absolute';
		skiplink.style.left = '-999em';
	}
}


function newwin()
{
	window.open(this.href,'_blank');
	return false;
}

function add_click_searchbox()
{
	//set_click('sq');
	//set_click('q');
	set_click('zoek');
	set_click('zoeken');
}

function set_click(id)
{
	var s_box = document.getElementById(id);
	if (s_box)
	{
		s_box.onclick = function(){clear_sbox(this)};
		s_box.onblur = function(){fill_sbox(this)};
	}
}

function clear_sbox(a_box)
{
	if (a_box.value == 'Uw zoekopdracht')
	{
		a_box.value = '';
	}
}

function fill_sbox(a_box)
{
	if (a_box.value == '')
	{
		a_box.value = 'Uw zoekopdracht';
	}
}


/* ----- ---- - --- -- -- - ----- -- --- - ---- - -*/

function prepare_menu()
{
	var menu = document.getElementById('subnavigation')
	if (menu)
	{
		collapse_all(menu);
		open_selected(menu);
		apply_events(menu);
	}
}

function toggle(a_item, docollapse)
{
	var uls = a_item.getElementsByTagName('ul')[0];
	var a = a_item.getElementsByTagName('a')[0];

	if (a.href == 'javascript:void(0)')
	{
		if (uls.style.display == 'none' || uls.style.display == '')
		{
			uls.style.display = 'block';
			a_item.className = 'opened';
		}
		else if (docollapse)
		{
			uls.style.display = 'none';
			a_item.className = 'closed';
		}
	}
}

function collapse_all(menu)
{
	var uls = menu.getElementsByTagName('ul');
	for (var n = 0; n < uls.length; n++)
	{
		uls[n].style.display = 'none';
	}
}

function open_selected(menu)
{
	var lis = menu.getElementsByTagName('li');
	var loc = String(document.location).toLowerCase();
	
	/*
		code om ervoor te zorgen dat het menu uitklapt
		als deze niet eindigt op een '/' of als de
		laatste url-fragmenten overeenkomen.
	*/
	var parts = loc.split('/');
	if (parts[parts.length - 1] != '')
	{
		parts.push('');
	}
	if (parts[parts.length-2] == parts[parts.length - 3])
	{
		parts[parts.length - 2] = '';
		parts.pop();
	}

	var loc = parts.join('/');

	for (var n = 0; n < lis.length; n++)
	{
		var href = String(lis[n].getElementsByTagName('a')[0]).toLowerCase();
		if (href == loc || href + '/' == loc)
		{
		
			href.className = 'selected';
			var p = lis[n].parentNode;

			// open all parents
			while(p.getAttribute('id') != 'subnavigation')
			{
				if (p.className == 'closed')
				{
					p.className = 'opened';
				}
				if (p.className ==  'folder index')
				{
					p.className = p.className + ' opened';
				}
				p.style.display='block';
				p = p.parentNode;
			}
			
			// open children
			var ul = lis[n].getElementsByTagName('ul')[0];
			if (ul)
			{
				ul.style.display = 'block';
			}
			break;
		}
	}

}


function apply_events(menu)
{
	var lis = menu.getElementsByTagName('li');
	for (var n = 0; n < lis.length; n++)
	{
		if (lis[n].getElementsByTagName && lis[n].getElementsByTagName('ul').length > 0)
		{
			var a = lis[n].getElementsByTagName('a')[0];
			if (lis[n].className.indexOf('index') == -1)
			{
				a.setAttribute('href','javascript:void(0)');
			}
			if (lis[n].className.indexOf('opened') == -1)
			{
				lis[n].className = 'closed';
			}
			// nasty, but neccesary for IE...
			a.onclick = function()
						{
							// toggle(this.parentNode.parentNode,true)
							toggle(this.parentNode,true)
						};
			
			a.onkeyup = function()
						{
							// toggle(this.parentNode.parentNode,false)
							toggle(this.parentNode,false)
						};
		}
	}
}

function setfocus(id)
{
	var field = document.getElementById(id);
	if (field)
	{
		field.focus();
		flash(id, 1);
	}
}

function flash (id, pass)
{
	var field = document.getElementById(id);
	border = '2px solid red';
	if (pass % 2 == 0)
	{
		border = '2px dashed white';
	}
	field.style.border = border;
	if (pass < 15)
	{
		pass ++
		setTimeout("flash('" + id+ "','" + pass + "')",50);
	}
}
