/*
	Javascript functions for the graphical user interface
	Let's go unobstrusive ;)
*/

if (document.addEventListener) 
{
    document.addEventListener("DOMContentLoaded", init, null);
}

window.onload = init;

function init()
{
	//make sure we only get here once
	if (arguments.callee.done)
	{
		return;
	}
	arguments.callee.done = true;
	add_click_searchbox();
	add_events_links();
	add_focus_skiplinks()
	do_build_date();
	prepare_menu();
}

/*	-------------------------------------------------------------------------*/
function add_click_searchbox()
{
	set_click('q');
}

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 add_events_links()
{
	var links = document.getElementsByTagName('a');
	var img = '&nbsp;<img src="/site/wormerland_website/layout/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 '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 newwin()
{
	window.open(this.href,'_blank');
	return false;
}

/*	-------------------------------------------------------------------------*/
function add_focus_skiplinks()
{
	var p_list = document.getElementsByTagName('p');
	for (i = 0; i < p_list.length; i++)
	{
		var p = p_list[i];
		if (p.className == "skip")
		{
			a_link = p.getElementsByTagName('a')[0];
			if (a_link)
			{
				a_link.onfocus =  function(){show_skip(this)};
				a_link.onblur =  function(){hide_skip(this)};
			}
		}
	}
}

function show_skip(a_link)
{
	a_link.style.left = 'auto';
}

function hide_skip(a_link)
{
	a_link.style.left = '-999em';
}

/*	-------------------------------------------------------------------------*/
function do_build_date()
{
	span = document.createElement('span');
	span.innerHTML = formattoday();
	span.setAttribute('id','date');
	var h2_news	=	document.getElementById('h2_news');
	if(h2_news)
	{
		h2_news.appendChild(span);
	}
}

function formattoday()
{
	var mon = new Array('januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december');
	var d = new Date();
	var curr_date = d.getDate();
	var curr_month = mon[d.getMonth()];
	return (curr_date + ' ' + curr_month);
}

/*	-------------------------------------------------------------------------*/
function prepare_menu()
{
	var menu = document.getElementById('subnavlist')
	if (menu)
	{
		collapse_all(menu);
		open_selected(menu);
		apply_events(menu);
	}
	var rubr = document.getElementById('nav');
	if (rubr)
	{
		hilite_selected(rubr);
	}
}

function hilite_selected(rubr)
{
	var as = rubr.getElementsByTagName('a');
	var loc = new String(document.location);
	//get
	var sel = loc.replace(/(http:\/\/[^\/]+\/[^\/]+\/).*/,'$1');
	var sel_plus	=	sel+'/';
	//find
	for (var i = 0; i < as.length; i++)
	{
		a = as[i];
		if (a.href == sel || a.href == sel_plus)
		{
			a.className = 'selected';
		}		
	}
}

function toggle(a_item, docollapse)
{
	var uls = a_item.getElementsByTagName('ul')[0];
	var a = a_item.getElementsByTagName('a')[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);
	for (var n = 0; n < lis.length; n++)
	{
		var href = lis[n].getElementsByTagName('a')[0];
		if (href == loc)
		{
		
			href.className = 'selected';
			p = lis[n].parentNode;

			// open all parents
			while(p.getAttribute('id') != 'subnavlist')
			{
				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
			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('ul').length > 0)
		{
			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)
						};
			
			// make sure people using a keyboard can access the links
			a.onkeyup = function()
						{
							toggle(this.parentNode.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);
	}
}
