$(function(){

	//Get all the links that are same-page links (eg <a href="#somewhere">)
	$('a[href*=#]').bind('click', function(){

		if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname) {

			var $target = $(this.hash);
			
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
			
			if ($target.length) {
				var targetOffset = $target.offset().top;
				
				$('html, body').animate({scrollTop: targetOffset}, 750, function(){
				
					window.location.hash = $target.attr('id'); //This won't work for named anchors (<a name="name">)
				
				});
				
				return false;
			}
		}
			
	});

	//Hide the old blog archives for now.
	(function(){
		$('#blogarchives').hide();

		//Grab the old HTML and store it in a variable for use later...
		var oldHTML = $('#blogarchives').prev().html();
		$('#blogarchives').prev().html('<a href="#" id="blogarchivestrigger">Older archives&hellip;</a>').addClass('older');

		//Add the event handler for the "expand archives" link
		$('#blogarchivestrigger').bind('click', function(){
			$('#blogarchivestrigger').remove();
			$('p.older').html(oldHTML); //Put the old HTML value back in...
			$('#blogarchives').css('padding-top', '2px');
			$('#blogarchives').css('margin-top', '-2px');
			$('#blogarchives').slideDown(1000);
			return false;
		});
	})();
	
	//Check for any "back to the previous page" links and add JS functionality to create a link that will mimick the browser's back button
	$('.previouspage').each(function(){
		var oldHTML = $(this).html();
		$(this).html('<a href="javascript:history.go(-1);">'+oldHTML+'</a>');
	});

});
