$(document).ready(function(){
    $("#GlobalNavigation a").each(function() {
        if (this.href == window.location || this.href == document.location.protocol + "//" + window.location.hostname + window.location.pathname)
            $(this).addClass("active");
    });
  
	$("a[href*='http://']:not([href*='"+location.hostname+"'])")
	.attr({target:"_blank",title:"External link - opens in a new window"});
	
	$("a[href*='pdf']")
	.attr({target:"_blank",title:"PDF document - opens in a new window"});
	
	$('ol.sitemap').sitemap();
	
});


jQuery.fn.sitemap = function() {
    return this.each(function() {

        //set variables
        var $map = $(this);
        var $roots = $map.find('li');

        //add classes
        $map.find('li:last-child').addClass('last');
        $map.addClass('sitemap');

        //hide all lists apart from the root list by defaolt
        $map.find('ol').hide();

        //repeat through all lists
        $roots.each(function() {
            //if list-item contains a child list
            if ($(this).children('ol').length > 0) {
                //add expand/contract control add anchor for keyboard navigation
                $(this).addClass('root').prepend('<span class="expand"><a href="#" title="expand/collapse" /><span class="hidden">expand/collapse list</span></a/></span>');  
            }
        });

        //when expand is clicked
        $('span.expand').toggle(
			function() {
			    // if clicked once, find all lists inside and expand
        $(this).toggleClass('contract').nextAll('ol').slideDown("slow");
			},

            function() {
                //if it's clicked again, find all inside lists and hide them
			$(this).toggleClass('contract').nextAll('ol').slideUp("slow");
            }
        );
    });
};
