/*

	gui.js
	
	javascript functions for the graphical user interface
	
	(c)2005 NIC

*/


// 	trigger onload when DOM is ready. for IE see ie_onload.js and the conditional 
//	comments in templates

//	(moz browsers)
if (document.addEventListener) 
{
    document.addEventListener("DOMContentLoaded", init, null);
}

// others
window.onload = init;

function init()
{

	//make sure we only get here once
	if (arguments.callee.done)
	{
		return;
	}
	arguments.callee.done = true;
	
	prepare_menu();
	add_events_links();
	add_click_searchbox();
	
	//jubileum();
}

function jubileum()
{
	// zorg ervoor dat het eerste nieuwsbericht niet onder het jubileum
	// logo valt.
	
	var c = document.getElementById('current');
	if (c)
	{
		var litems = c.getElementsByTagName('li');
		var hitems = c.getElementsByTagName('h3');
		var li = litems[0];
		var he = hitems[0];
		if (li)
		{
			li.style.position = 'relative';
			li.style.width = '17em';
			li.style.paddingTop = '.6em';
		}
		
		if (he)
		{
			he.style.width = '17em';
		}
	}
}

function initpreview()
{
	draw_printbutton();
	draw_pagedividers();
}

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);
	}
}


var page_height = 990;
var page_inner_height = 1090;

function draw_pagedividers()
{	
	var cnt = document.getElementById('content');
	if (cnt.clientHeight)
	{	
		var pages = Math.ceil(cnt.clientHeight / page_height);
		for (i = 1; i < pages; i++)
		{
			cnt.innerHTML += '<hr class="divider" style="left:0;top:' + (i * page_height) + 'px;" />'
		}
		cnt.style.height = pages * page_inner_height + 'px';
	}
}

function draw_printbutton()
{
	var pr = document.getElementById('print');
	pr.innerHTML = '<a href="javascript:print()">afdrukken</a>';			

}


function add_events_links()
{
	var links = document.getElementsByTagName('a');
	var host = location.hostname;
	var img = '&nbsp;<img src="/site/stadskanaal_website/layout/images/newwindow.png" alt="externe website" title="deze link verwijst naar een externe website" />';
	for (var i = 0; i < links.length; i++)
	{
		
		if (links[i].className == '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';
		}
		else
		{
			links[i].onfocus = function () { dofocus(this) }; 
			links[i].onblur = function () { doblur(this) };
		}

		var href = String(links[i].getAttribute('href'));
		if (href.indexOf('http') > -1 && href.indexOf(host) == -1)
		{
			links[i].setAttribute('title','deze link verwijst naar een externe website');  
			links[i].innerHTML = links[i].innerHTML + img;
		}
	}
}

function dofocus(alink)
{
	alink.oldcolor = alink.style.color;

	alink.style.color = 'black';
	alink.style.backgroundColor = '#FFF29F';
}

function doblur(alink)
{
	alink.style.color = alink.oldcolor;
	alink.style.backgroundColor = 'transparent';
}

function newwin()
{
	window.open(this.href,'_blank');
	return false;
}

function show_skippy(skiplink,display)
{
	if (display)
	{
		skiplink.style.position = 'relative';
		skiplink.style.zIndex = '1000';
		skiplink.style.left = 'auto';
	}
	else
	{
		skiplink.style.position = 'absolute';
		skiplink.style.left = '-999em';
	}
}

function prepare_menu()
{
	var menu = document.getElementById('subnavlist')
	if (menu)
	{
		collapse_all(menu);
		apply_events(menu);
		open_selected(menu);
	}
	else
	{
		var nav = document.getElementById('navigation');
		if (nav)
		{
			//nav.style.display = 'none';
			nav.parentNode.removeChild(nav);
		}
		
	}
	var rubr = document.getElementById('rubrieken');
	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');
	//find
	for (var i = 0; i < as.length; i++)
	{
		a = as[i];
		if (a.href == sel)
		{
			a.parentNode.parentNode.className = 'selected';
		}		
	}
}

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, true)
						};
			
			// make sure people using a keyboard can access the links
			a.onkeyup = function()
						{
							toggle(this.parentNode, 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 toggle(a_item, docollapse)
{
	var uls = a_item.getElementsByTagName('ul')[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);
	
	var last = loc.split('/');
	last = last[last.length -1];
	var altloc = String(document.location) + '/' + last; 
	
	for (var n = 0; n < lis.length; n++)
	{
		var href = lis[n].getElementsByTagName('a')[0];
		if (href == loc || href == altloc)
		{
			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 clear_sbox(a_box)
{
	if (a_box.value == 'Uw zoekopdracht')
	{
		a_box.value = '';
	}
}
