/*
	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;

	startGlider();
	
	prepare_menu();
	add_events_links();
	//menu();
	add_click_searchbox();
	//readspeaker();
	content_top();
	content_height();
	add_click_print();
	
	var items = new Array()
	items[0] = 'nieuws';
	items[1] = 'evenementen';
	
	highlight_menu_item(items);
	
	var bv = vIE();
	if( (bv < 7) && (bv > 0)  ){
		showZoom();
	}
	
}

function vIE()
{
	return (navigator.appName=='Microsoft Internet Explorer')?parseFloat((new RegExp("MSIE ([0-9]{1,}[.0-9]{0,})")).exec(navigator.userAgent)[1]):-1;
}

function showZoom()
{
	var zoom = document.getElementById('zoom');
	if (zoom)
	{
		zoom.onmouseover = function()
		{
			var zoomchild = zoom.getElementsByTagName('ul');
			if(zoomchild)
			{
				if (zoomchild[0].className == "") { zoomchild[0].className = 'active'; } else {	zoomchild.className = ''; }
			}
		}	
		
		zoom.onmouseout = function()
		{
			var zoomchild = zoom.getElementsByTagName('ul');
			if(zoomchild) { zoomchild[0].className = ''; }
		}
		
	}
}

function add_events_links()
{
	var links = document.getElementsByTagName('a');
	var img = '&nbsp;<img src="/site/internet/layout/images/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;
				
				var _parent = links[i].parentNode;
				
				if( _parent.parentNode.className != 'new_window_no_img')
				{
					links[i].innerHTML = links[i].innerHTML + img;
				}
				break;
		}
	}
}

function add_click_searchbox() 
{
	set_click('zoeken');
	set_click('q');
}

function set_click(id)
{
	var s_box = document.getElementById(id);
	

	if (s_box)
	{
		if(id == 'q')
		{
			s_box.value = 'Search'
		}
		s_box.onclick = function(){clear_sbox(this)};
	}
}

function clear_sbox(a_box)
{
	if (a_box.value == 'Search')
	{
		a_box.value = '';
	}
	if (a_box.value == 'Uw zoekopdracht')
	{
		a_box.value = '';
	}
	
	
}

function newwin()
{
	window.open(this.href,'_blank');
	return false;
}


function highlight_menu_item(items)
{
	var incomming_url= window.location.href;
	var aantal_characters_in_url = incomming_url.length - 1;

	if(incomming_url.charAt(aantal_characters_in_url) != '/')
	{
		incomming_url = incomming_url+'/';
	}

	var clean_url = incomming_url.split("http://");
	var url_array = clean_url[1].split("/");
	var needle = url_array[1].toLowerCase();

	if(InArray(needle,items))
	{
		var kids = get_children('subnavlist','li');
		
		for(a=0;a<kids.length;a++)
		{
			var kid_container = get_children(kids[a],'strong');
			
			for(b=0;b<kid_container.length;b++)
			{
				var link = new String(get_children(kid_container[b],'a'));
				
				if(link.toLowerCase().match('/'+needle))
				{
					var _element = get_children(kid_container[b],'a');
					
					_element[0].className = 'selected';
				}
			}
		}
		
		var kids = get_children('hoofdmenu','li');
		
		for(a=0;a<kids.length;a++)
		{
			var kid_container = get_children(kids[a],'strong');
			
			for(b=0;b<kid_container.length;b++)
			{
				var link = new String(get_children(kid_container[b],'a'));
				
				if(link.toLowerCase().match("/actueel/"))
				{
					var _element = get_children(kid_container[b],'a');
					
					_element[0].className = 'selected';
				}
			}
		}
	}
}

function InArray(needle, haystack) 
{
	for (h in haystack) 
	{
		if (haystack[h] == needle) 
		{
			return true;
		}
	}
	return false;
}

function get_children(_container,_child)
{
	var _containing_element = document.getElementById(_container);

	if(_containing_element == null)
	{
		_containing_element = _container;
	}

	if (_containing_element.hasChildNodes())
	{
		var children = _containing_element.childNodes;
	   	
	   	var match = 0;
	   	var matched_children = new Array();
	   	
	   	for (var i = 0; i < children.length; i++) 
	   	{
			if(children[i].tagName == _child.toUpperCase())
			{
				matched_children[match] = children[i];
				match++;
			}
	   	}
	   	if(matched_children.length > 0)
		{
			return matched_children;
		}
	}
}

/* ----- ---- - --- -- -- - ----- -- --- - ---- - -*/

function set(id,display)
{
	var obj	=	document.getElementById(id);
	if(obj)
	{
		obj.style.display	=	display;
	}
}

/* ----- ---- - --- -- -- - ----- -- --- - ---- - -*/

function prepare_menu()
{
	var menu = document.getElementById('submenu')
	if (menu)
	{
		collapse_all(menu);
		open_selected(menu);
		apply_events(menu);
	}
}

function menu()
{
	var _menu_actie = document.getElementById('menu_actie');
	
	if(_menu_actie != null)
	{
		_menu_actie.innerHTML = '<a href="javascript:void(0)" class="menu_actie">>Menu dicht</a>';
	}
	var _menu_open = document.getElementsByClassName('menu_actie');
	var _action = getcookie("menu");
	
	if(_menu_open != '')
	{
		_menu_open[0].onclick = function(){toggle('close');}
	}
	if(_menu_open != '')
	{
		if(_action == 'closed')
		{
			_menu_open[0].onclick = function(){toggle('open');}
			toggle('close');
		}
		else if(_action == 'open')
		{
			_menu_open[0].onclick = function(){toggle('close');}
		}
	}
}

function toggle(action)
{
	var _menu_open 		= document.getElementsByClassName('menu_actie');

	if(_menu_open != '')
	{
		if (action == 'close')
		{
			_menu_open[0].onclick = function(){toggle('open');}
			_menu_open[0].innerHTML = '>Menu open';			
			
			Element.addClassName("header","closed");
			Element.addClassName("header_menu","closed");
			Element.addClassName("header_img","closed");
			Element.addClassName("header_gradient","closed");
			Element.addClassName("hoofdmenu","closed");
			Element.addClassName("navigation","closed");
			Element.addClassName("klikpad","closed");
			Element.addClassName("main","closed");
			Element.addClassName("image_block","closed");
			Element.addClassName("right","closed");
			
			setCookie('menu', 'closed', 15, '/');
		}
		else if (action == 'open')
		{
			_menu_open[0].onclick = function(){toggle('close');}
			_menu_open[0].innerHTML = '>Menu dicht';
			
			Element.removeClassName("header","closed");
			Element.removeClassName("header_menu","closed");
			Element.removeClassName("header_img","closed");
			Element.removeClassName("header_gradient","closed");
			Element.removeClassName("hoofdmenu","closed");
			Element.removeClassName("navigation","closed");
			Element.removeClassName("klikpad","closed");
			Element.removeClassName("main","closed");
			Element.removeClassName("image_block","closed");
			Element.removeClassName("right","closed");
			
			setCookie('menu', 'open', 15, '/');
		}
	}
}

function setCookie(name, value, expires, path) 
{
 	var curCookie = name + "=" + escape(value) +
      
	((expires) ? "; expires=" + getexpirydate(expires) : "") +
      
    ((path) ? "; path=" + path : "");
  
  	document.cookie = curCookie; 	
}

function getcookie(cookiename) 
{

 	var cookiestring=""+document.cookie;
 	
 	var index1=cookiestring.indexOf(cookiename);
 	
 	if (index1==-1 || cookiename=="") return ""; 
 	
 	var index2=cookiestring.indexOf(';',index1);
 
 	if (index2==-1) index2=cookiestring.length; 
 	
 	return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}

function getexpirydate(minutes)
{
	var UTCstring;
	
	Today = new Date();

	nomilli=Date.parse(Today);

	Today.setTime(nomilli+minutes*60*1000);

	UTCstring = Today.toUTCString();

	return UTCstring;
}

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) || (loc.match(href)))
		{
		
			href.className = 'selected';
			p = lis[n].parentNode;

			// open all parents
			while(p.getAttribute('id') != 'submenu')
			{
				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('onclick','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 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);
	}
}

function hide_reactform()
{
	if(String(document.location).indexOf('eageer/') == -1)
	{
		var obj	=	document.getElementById('reactform');
		if(obj)
		{
			obj.style.display	=	'none';
		}
	}
}

function show_reactform()
{
	var obj	=	document.getElementById('reactform');
	if(obj)
	{
		if(obj.style.display == 'block')
		{
			hide_reactform();
		}
		else
		{
			obj.style.display	=	'block';
		}
	}
}

function hilite_table()
{
	if(document.getElementById && document.createTextNode)
 	{
   		var tables	=	document.getElementsByTagName('table');
   		for (var i=0;i<tables.length;i++)
   		{
    		if(tables[i].className=='hilite')
    		{
     			var trs=tables[i].getElementsByTagName('tr');
     			for(var j=0;j<trs.length;j++)
     			{
					if(trs[j].parentNode.nodeName=='TBODY' && trs[j].parentNode.nodeName!='TFOOT')
					{
						trs[j].onmouseover=function()
						{
							this.className += ' active';
							return false;
						}
       					trs[j].onmouseout=function()
       					{
       						this.className = this.className.replace(/active/g,'');
       						return false;
       					}
     				}
    			}
   			}
  		}
 	}
}

/* ----- ---- - --- -- -- - ----- -- --- - ---- - -*/

function focus_login()
{
	var obj	=	document.getElementById('name');
	if (obj)
	{
		setfocus_only('name');
	}
}

function setfocus_only(id)
{
	var field = document.getElementById(id);
	if (field)
	{
		field.focus();
	}
}

/* ----- ---- - --- -- -- - ----- -- --- - ---- - -*/
function startGlider(){
	
	id = "scroller"
	
	if((document.getElementById) && ( document.getElementById(id))){
		
		$("#scroller").scrollable({
		
			size: 1,
			clickable: false,
			loop: false,
			speed: 600
			
			}).circular().navigator({

			navi: "#scrollerNav",
			naviItem: 'a',
			activeClass: 'current'

		}).autoscroll( {
			autoplay: true,
			interval: 9000,
			steps: 1
			
		});		
	}
	
}

function content_top()
{	
	if((document.getElementById) && ( document.getElementById("layer2"))){
		
		var top = document.getElementById("layer2").clientHeight;
		if(top > 0)
		{
			//Als klikpad hoger is dan 1 regel (36px)
			if (document.getElementById("klikpad").clientHeight > 36)
			{
				//bereken hoeveel het klikpad hoger is t.o.v. 1 regel)
				var dif = document.getElementById("klikpad").clientHeight - 37;
				//tel dat bij het geheel op
				document.getElementById("content").style.marginTop = (top + 189 + dif)+'px';
			}
			else
			{
				document.getElementById("content").style.marginTop = (top + 189)+'px';
			}
		}
	}
}

function content_height()
{
	if((document.getElementById) && ( document.getElementById("metacol"))){
	
		var height = document.getElementById("metacol").clientHeight;
		var bodyHeight = document.getElementById("content").clientHeight + 50;
		
		if(height > 500 && bodyHeight < height)
		{
			document.getElementById("content").style.height = (height + 20)+'px';
		}
	
	
	}
}

function add_click_print()
{
	if (document.getElementById('knop_print'))
	{
		$('#knop_print a').text('Afdrukken');
		$('#knop_print a').click(function() {
			print();
		});
	}
}





