var txt_plus  = 'Показать';
var txt_minus = 'Скрыть';

function toggle_collapsable(event) {
	var a = $(event.target);
	a.next().slideToggle('fast', function() {
		a.toggleClass('minus');
		var txt = a.next().children().filter('.collapsable').html();
		if (txt.split('|')[1])
			a.html(a.next().is(':hidden') ? txt.split('|')[0] : txt.split('|')[1]);
		if (!txt)
			a.html(a.next().is(':hidden') ? txt_plus : txt_minus);
	});
	
}

$(document).ready(function() {
	$('.collapsable').each(function(n) {
		var coll = $($('.collapsable').parent()[n]);
		var expanded = $(this).hasClass('expanded');
		coll.wrap(document.createElement("div"));
		div = coll.parent();
		div.attr('className',coll.attr('className'));
		coll.attr('className','');
		if (expanded) {
			coll.before('<a class="toggle_link minus">'+txt_minus+'</a>');
		} else {
			coll.hide();
			coll.before('<a class="toggle_link">'+txt_plus+'</a>');
		}
		var title = coll.children().filter('.collapsable');
		var txt = title.html();
		if (title.html()) {
			i = expanded && title.html().split('|')[1] ? 1 : 0;
			coll.prev().html(title.html().split('|')[i]);
			title.hide();
		}
		coll.prev().click(toggle_collapsable);
	});
})
