var coll_numShown = 4;
var coll_hideText = "Piilota";
var coll_showText = "Näytä koko teksti";

function initTexts()
{
    if (language == 'sv-FI')
    {
         coll_showText = "Visa text";
         coll_hideText = "Göm texten";
    } 
    else if (language == 'en-GB')
    {
        coll_showText = "Show more"
        coll_hideText = "Show less";
    }
}

function addCollapsable(elem) {
	var elements;
	//if this is the first run
	if (!elem) {
		//hide
		$(".collapsable").find(".text>*:gt(" + (coll_numShown - 1) + ")").each(function() {
			$(this).toggle();
		});
		
		//add the toggle links
		$(".collapsable").append($('<p><a class="showLink" href="#">' + coll_showText + '</a></p>'));

		elements = $(".collapsable>p>a");
	} else elements = $(elem);

	//core functionality
	elements.click(function(e) {
		$(this).unbind('click');
		var anchestor = $(this).parent().parent();
		if ($(this).hasClass("showLink")) {
			//show
			$(this).text(coll_hideText).removeClass("showLink").addClass("hideLink");
			anchestor.find('.text>*:hidden').each(function() {
				$(this).show();
			});
		} else {
			//hide
			$(this).text(coll_showText).removeClass("hideLink").addClass("showLink");
			anchestor.find('.text>*:gt(' + (coll_numShown - 1) + ')').each(function() {
				$(this).toggle();
			});
		}
		e.preventDefault();
		addCollapsable(this);
	});
}
$(document).ready(function() {
	addCollapsable();
});
