/*
	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();
	setbulleted();
	add_events_links();
	add_click_searchbox();
	add_balloons();
}

function add_events_links()
{
	var links = document.getElementsByTagName('a');
	var img = '&nbsp;<img src="http://www.gemeenteoplossingen.nl/site/www.gemeenteoplossingen.nl/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 newwin()
{
	window.open(this.href,'_blank');
	return false;
}

/* ----- ---- - --- -- -- - ----- -- --- - ---- - -*/

function add_click_searchbox()
{
	set_click('sq');
	set_click('q');
}

function set_click(id)
{
	var s_box = document.getElementById(id);
	if (s_box)
	{
		s_box.onclick = function(){clear_sbox(this)};
	}
}

function clear_sbox(a_box)
{
	if (a_box.value == 'Uw zoekopdracht')
	{
		a_box.value = '';
	}
}

function add_balloons()
{
	var quick	=	document.getElementById("quickswitch");
	if(quick)
	{
		var lis		=	quick.getElementsByTagName("li");
		for(var n=0;n < lis.length; n++)
		{
			var first_a	=	lis[n].getElementsByTagName("a")[0];
			first_a.setAttribute("id","balloon"+n);
			first_a.onmouseover		=	function() { set(this.id+"img","block"); }
			first_a.onmouseout		=	function() { set(this.id+"img","none"); }
			
			var href	=	first_a.getAttribute("href");
			var alt		=	lis[n].getElementsByTagName("img")[0].getAttribute("alt");
			var idnm	=	"balloon"+n+"img";
			
			var img		=	document.createElement("img");
			
			img.setAttribute("src","http://www.gemeenteoplossingen.nl/site/www.gemeenteoplossingen.nl/images/skin/balloon"+n+".png");
			img.setAttribute("id",idnm);
			img.setAttribute("alt",alt);
			
			lis[n].appendChild(img);
		}
	}
}

function set(id,display)
{
	var obj	=	document.getElementById(id);
	if(obj)
	{
		obj.style.display	=	display;
	}
}

/* ----- ---- - --- -- -- - ----- -- --- - ---- - -*/

function prepare_menu()
{
	activemainmenu();

	var menu = document.getElementById('subnavlist')
	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);
	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';
				}
				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)');
			}
			
			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 setbulleted()
{
	var newsbox = document.getElementById('news');
	if (newsbox)
	{
		var items = newsbox.getElementsByTagName('a')
		for (var n = 0; n < items.length; n++)
		{
			var parent = items[n].parentNode; 
			if (parent && (parent.tagName == 'H3' || parent.tagName == 'H2'))
			{
				items[n].className = 'bulleted';
			}
		}
	}

}

function activemainmenu()
{
	rubr = document.getElementById('navigation');

	var as = rubr.getElementsByTagName('a');
	var loc = new String(document.location);
	//get
	var sel = loc.replace(/(http:\/\/[^\/]+\/[^\/]+)\/.*/,'$1');
	sel = sel.toLowerCase();
	//find


	for (var i = 0; i < as.length; i++)
	{
		a = as[i].href
		a = a.toLowerCase();
		
		if (a == sel)
		{
			as[i].parentNode.className = 'active';
		}		
	}
}

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);
	}
}
