$(document).ready(function(){
	selem = $(this).find('dl.meta abbr.dtstart');
	/* Can't be bothered to support browsers which don't understand ISO 8601 dates */
	if (isNaN(Date.parse($(selem).attr('title')))){
		$('#newstoggle').remove();
		return false;
	} else {
		all_news_func = function() {
			/*All*/
			$('#news>li').each(function(index, elem) {
				$(this).css('display', 'inherit');
			});
			$.cookie({'all_news': '1'});
			return false;
		}
	
		recent_news_func = function() {
			/*Recent*/
			$('#news>li').each(function(index, elem) {
				selem = $(this).find('dl.meta abbr.dtstart');
				d = Date.parse($(selem).attr('title'));
				now = new Date();
				now.setFullYear(now.getFullYear() - 1);
				if(d < now) {
					$(this).css('display', 'none');
				} else {
					$(this).css('display', 'inherit');
				}
			});
			$.cookie({'all_news': '0'});
			return false;
		}
	
		elems = $('#newstoggle').text().split('|');
		all_text = $.trim(elems[0]);
		recent_text = $.trim(elems[1]);
	
		var all_news = $.cookie('all_news');
	
		if ($.isEmptyObject(all_news)) {
			all_news = '0';
			$.cookie('all_news', all_news, {expires: 31, domain: 'sffjunkie.co.uk', path: '/'});
		}
		
		if (all_news == '0') {
			all_class = '';
			recent_class = ' selected';
			recent_news_func();
		} else {
			all_class = ' selected';
			recent_class = '';
			all_news_func();
		}
		
		h = '<span class="all' + all_class + '">' + all_text + '</span>' +
			'<span class="recent' + recent_class + '">' + recent_text + '</span>';
			
		$('#newstoggle').html(h);
		$('#newstoggle').css('display', 'block');	
	
		$('#newstoggle').click(function() {
			$('span', this).each(function(index,elem) {
				$(elem).toggleClass('selected');
				if ($(elem).hasClass('selected')) {
					if ($(elem).hasClass('recent')) {
						recent_news_func();
					} else {
						all_news_func();
					}
				}
			});
		});
		return false;
	}
});

