var slugID; // used in partial script to fetch content

$(document).ready(function(){

	// we have javascripts!
	// close the accordion items and add 'open' links
	$('.accordion>li>ul').hide();
	$('.accordion>li span').after('<a href="#">open</a>');
	
	// get the slug
	var slug = location.href.split('/').pop();
	
	// try to find the slug in the accordion and open that part of the accordion
	$('.accordion ul li a').each(function(){
		if ($(this).attr('href') === slug) {
			$(this).addClass('current');
			$(this).parents('ul:first').slideDown().parent('li').addClass('open').find('a:first').text('close');
			slugID = $(this).attr('href');
		}
	});
	
	// if the slug is not found in the accordion, ignore it and open the first accordion item
	if (!slugID) {
		$('.accordion>li>ul>li>a:first').addClass('current');
		$('.accordion>li>ul:first').slideDown().parent('li').addClass('open').find('a:first').text('close');
		slugID = $('.accordion>li>ul>li>a:first').attr('href');
	}
	
	// click events!
	$('.accordion>li>a').click(function(){
		$(this).parent('li').toggleClass('open').find('ul:first').slideToggle('fast');
		$(this).text() === 'open' ? $(this).text('close') : $(this).text('open');
		return false;
	});
	
});
