// If user agent understands DOM, call site functions
if (document.getElementById && document.createTextNode)
{
	addLoadEvent(hide_us);
	addLoadEvent(unobtrusive_links);
}


/**
* By Simon Willison @ http://simonwillison.net/2004/May/26/addLoadEvent/
*/
function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload = function()
		{
			if (oldonload)
			{
				oldonload();
			}
			func();
		}
	}
}


/**
* Add JavaScript functionality to all a tags.
* Functionality depends on the class name
*/
function unobtrusive_links()
{
	var as, i;
	as = document.getElementsByTagName('a');
	for (i = 0; i < as.length; i++)
	{
		if (/pop_me/.test(as[i].className)) // Pop up links with class name 'pop_me'
		{
			as[i].onclick = function() { return popup(this); }
			// as[i].onkeypress = function() { return popup(this) }
		}
		else if (/ujs_back/.test(as[i].className)) // Back
		{
			as[i].onclick = function() { history.back(-1); return false; }
		}
		else if (/ujs_out_of_use/.test(as[i].className)) // Out of use
		{
			as[i].onclick = function() { alert('Tämä ominaisuus ei ole vielä käytössä.'); return false; }
		}
		else if (/ujs_toggle_group_content/.test(as[i].className)) // Toggle group content
		{
			as[i].onclick = function() { return toggle_group_content(this); }
		}
	}
}


/**
* Toggles element and its sibling's visibility.
* Element in question will become visible and all of its siblings hidden.
* Also alters the navigation styles to create visual clue as to where the user is.
*/
function toggle_group_content(o)
{
	// Get link address and extract id from it
	var link = o.href;
	var element_id = link.substring(link.lastIndexOf('#') + 1);
	
	// Check if the desired element exists
	var element = document.getElementById(element_id);
	if (element)
	{
		// Retrieve its parent and hide all or her children
		var parent = element.parentNode;
		var all_children = parent.getElementsByTagName('div');
		for (var i = 0; i < all_children.length; i++)
		{
			all_children[i].style.display = 'none';
		}
		
		// Display the desired element
		element.style.display = 'block';
	}
	
	// Finally change link's style
	// First get all links in current navigation
	var link_mother = o.parentNode.parentNode;
	if (link_mother)
	{
		// Retrieve all links in navigation and clear class names
		var link_siblings = link_mother.getElementsByTagName('li');
		for (var i = 0; i < link_siblings.length; i++)
		{
			link_siblings[i].className = '';
		}
		
		// Finally change the class name of the link that was clicked
		o.parentNode.className = 'luettelolista_active';
	}
	
	return false;
}


/**
* Hides all div elements with class 'ujs_hide'
*/
function hide_us()
{
	var divs, i;
	divs = document.getElementsByTagName('div');
	
	for (i = 0; i < divs.length; i++)
	{
		if (/ujs_hide/.test(divs[i].className))
		{
			divs[i].style.display = 'none';
		}
	}
}