// If user agent understands DOM, call these functions after page load
if (document.getElementById && document.createTextNode)
{
	//addLoadEvent(hide_us);
	//addLoadEvent(stripe_us);
	addLoadEvent(unobtrusive_links);
	addLoadEvent(unobtrusive_form_elements);
}


/**
* 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();
		}
	}
}


/**
* Unobtrusively handle certain links
* ujs_back: history.back(-1)
* ujs_out_of_use: alert
* ujs_back_to_top: scrollTo(0, 0);
*/
function unobtrusive_links()
{
	var links = document.getElementsByTagName('a');
	
	for (var i = 0; i < links.length; i++)
	{
		if (/ujs_back/.test(links[i].className))
		{
			links[i].onclick = function() { history.back(-1); return false; }
		}
		if (/ujs_out_of_us/.test(links[i].className))
		{
			links[i].onclick = function() { alert("TÃ¤mÃ¤ ominaisuus ei ole vielÃ¤ kÃ¤ytÃ¶ssÃ¤"); return false; }
		}
		if (/ujs_back_to_top/.test(links[i].className))
		{
			links[i].onclick = function() { scrollTo(0, 0); return false; }
		}
	}
}


/**
* Everything related to form styling.
* Alter input and textarea borders for 'ujs_form_text' (focus/blur)
*/
function unobtrusive_form_elements()
{
	var inputs = document.getElementsByTagName('input');
	for (var i = 0; i < inputs.length; i++)
	{
		if (/ujs_form_text/.test(inputs[i].className))
		{
			inputs[i].onfocus = function() { return style_form_text_element(this); }
			inputs[i].onblur = function() { return style_form_text_element(this); }
		}
	}
	
	var textareas = document.getElementsByTagName('textarea');
	for (var j = 0; j < textareas.length; j++)
	{
		if (/ujs_form_text/.test(textareas[j].className))
		{
			textareas[j].onfocus = function() { return style_form_text_element(this); }
			textareas[j].onblur = function() { return style_form_text_element(this); }
		}
	}
}


/**
* Alter elements border colour
* 
* onFocus: dark border colour
* onBlur: light, default border colour
*/
function style_form_text_element(o)
{
	var border_color = o.style.borderColor;
	
	if (border_color == '#909090')
	{
		o.style.border = '1px solid #c6c6c6';
		o.style.color = '#898d8d';
	}
	else
	{
		o.style.border = '1px solid #909090';
		o.style.color = '#656567';
	}
	
	return true;
}


/**
* Hides all div elements with class 'ujs_hide'
*/
function hide_us()
{
	var divs, spans, i;
	divs = document.getElementsByTagName('div');
	spans = document.getElementsByTagName('span');
	
	for (i = 0; i < divs.length; i++)
	{
		if (/ujs_hide/.test(divs[i].className))
		{
			divs[i].style.display = 'none';
		}
	}
	
	for (var j = 0; j < spans.length; j++)
	{
		if (/ujs_hide/.test(spans[j].className))
		{
			spans[j].style.display = 'none';
		}
	}
}


/**
* Stripe all list elements with class 'ujs_stripe'
*/
function stripe_us()
{
	var uls = document.getElementsByTagName('ul');
	var ols = document.getElementsByTagName('ol');
	
	for (var i = 0; i < uls.length; i++)
	{
		if(/ujs_stripe/.test(uls[i].className))
		{
			var id = uls[i].getAttribute('id');
			stripe(id);
		}
	}
	
	for (var j = 0; j < ols.length; j++)
	{
		if(/ujs_stripe/.test(ols[j].className))
		{
			var id = ols[j].getAttribute('id');
			stripe(id);
		}
	}
}


/**
* 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;
	}
}
