/*
 compress_archive()
 a function designed to modify the default layout of the Jogger.pl archive block
 by Michal Lukaszek, 2007
 requires a recent version of jQuery (http://jquery.com)
 licensed under GNU GPL v3
*/
function compress_archive(selector) {
	var seen;
	var root = (typeof selector == 'string')? selector+' ' : '';
	$(root+'ul:first li a').each(function(){
		current = $(this).text().replace(/[^0-9]+/g, '');
		if (current != seen) {
			seen = current;
			$(root+'ul:first li a[href*=/'+current+'/]').parent().wrapAll('<ul class="year" id="year'+current+'"></ul>');
		}
	});
	var years = $.makeArray($(root+'ul:first ul'));
	$(years.reverse()).each(function(){ $(this).insertAfter($(root+'ul:first')); });
	$(root+'ul:first').remove();
	$(root+'ul.year').each(function() {
		$(this).before('<div class="yearhead"><span>' + $(this).find('li:first a').text().replace(/[^0-9]+/g, '') + '</span></div>');
		$(this).find('li a').each(function() { x = $(this).text().replace(/\s*[0-9]+\s*/, ''); $(this).text(x); });
		$(this).prev().click(function() {
			if ($(this).hasClass('opened')) return;
			$(root+'ul.year.opened').slideUp();
			$(root+'ul.year.opened, '+root+'div.yearhead.opened').removeClass('opened');
			$(this).addClass('opened').next().slideDown().addClass('opened');
		});
	});
	$(root+'ul.year:not(:first)').slideUp();
        $(root+'ul:first').addClass('opened').prev().addClass('opened');
	var urlyear = location.href.match(/\/(20\d{2})\//);
	if (urlyear) $('#year'+urlyear[1]).prev().trigger('click');
}


