// If user agent understands DOM, call site functions
if (document.getElementById && document.createTextNode)
{
	//addLoadEvent(hide_us);
	//addLoadEvent(function() { set_opacity('site_logo', 8);} );
	addLoadEvent(unobtrusive_links);
	//addLoadEvent(stripe_us);
}


/**
* 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();
		}
	}
}


/**
* Alters desired element's opacity. Element identified by id.
* Modified from original setOpacity by Peter-Paul Koch @ http://www.quirksmode.org/js/opacity.html
*/

function set_opacity(element_id, value)
{
	var element = document.getElementById(element_id);
	if (element)
	{
		element.style.opacity = value / 10;
		element.style.filter = 'alpha(opacity = ' + value * 10 + ')';
	}
}


















/**
* Hides all div elements with class 'hide_me'
*/
function hide_us()
{
	var divs, i;
	divs = document.getElementsByTagName('div');
	
	for (i = 0; i < divs.length; i++)
	{
		if (/hide_me/.test(divs[i].className))
		{
			divs[i].style.display = 'none';
		}
	}
}


/**
* Stripe all list elements with class 'stripe_me'
*/
function stripe_us()
{
	var uls = document.getElementsByTagName('ul');
	var ols = document.getElementsByTagName('ol');
	
	for (var i = 0; i < uls.length; i++)
	{
		if(/stripe_me/.test(uls[i].className))
		{
			var id = uls[i].getAttribute('id');
			stripe(id);
		}
	}
	
	for (var j = 0; j < ols.length; j++)
	{
		if(/stripe_me/.test(ols[j].className))
		{
			var id = ols[j].getAttribute('id');
			stripe(id);
		}
	}
}


/**
* 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 (/toggle_in_group/.test(as[i].className)) // Toggle visibility for elements in link # and siblings with class name 'toggle_in_group'
		{
			as[i].onclick = function() { return toggle_group_content(this); }
			// as[i].onkeypress = function() { return toggle_visibility(this); }
		}
		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; }
		}
		
		if (/ujs_back/.test(links[i].className))
		{
			links[i].onclick = function() { history.back(-1); return false; }
		}
	}
}


/**
* 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 = 'box_active';
	}
	
	return false;
}


/**
* Loop through list element's items and add "odd" to their class name(s).
* Modified from Jop de Klein's code @ http://validweb.nl/artikelen/javascript/better-zebra-tables/
* which modified David F. Miller's original @ http://www.alistapart.com/articles/zebratables
*/
function stripe(id)
{
	// Flag that keeps track of whether the current item is odd or even
	var even = false;
	
	// Get desired element. If not found, abort.
	var element = document.getElementById(id);
	if (!element)
	{
		return;
	}
	
	// Get all children, loop through them and add class name 'odd' to them
	var lis = element.getElementsByTagName('li');
	for (var i = 0; i < lis.length; i++)
	{
		if (!even)
		{
			lis[i].className += ' odd';
		}
		
		// Flip from odd to even, or vice-versa
		even = !even;
	}
}