$(function() {
	// De-obfuscate e-mail addresses
	function correctEmailAddress(obfuscatedEmailAddress) {
		return obfuscatedEmailAddress.replace(/\(at\)/, "@").replace(/\u00B7/g, ".");
	}
	$("a[href^='mailto:']").attr("href", function() {
		return correctEmailAddress($(this).attr("href"));
	});
	$(".emailaddress").each(function() {
		var emailAddress = correctEmailAddress($(this).html());
		$(this).html(emailAddress);
		if ($(this).is("a:not([href])")) {
			$(this).attr("href", "mailto:" + emailAddress);
		}
	});
	
	// Make external links open in a new tab.
	$("a.external").each(function() {
		$(this).not("[title]").attr("title", "This link opens in a new window");
		$(this).click(function() {window.open(this.href); return false;});
	});
	
	// Enable smooth scrolling.
	$("a[href^=#]").click(function (event) {
		event.preventDefault();
		var offset = $($(this).attr("href")).offset();
		if (offset == null) return;
		$("html, body").animate({scrollTop: offset.top}, "slow");
		window.location.hash = $(this).attr("href");
	});
	
	// Enable any accordion UI elements in the code.
	$(".ui-accordion").accordion({
		header: ".ui-accordion-header",
		active: false,
		collapsible: true,
		autoHeight: false,
		navigation: true
	});
	
	// Enable hidden detail sections.
	$(".hidden-details").hide();
	$(".hidden-details").append(" <a class=\"hidden-details-less\" href=\"#\">[hide]</a>");
	$(".hidden-details").prev().append(" <a class=\"hidden-details-more\" href=\"#\">[more]</a>");
	$(".hidden-details-more").click(function () {
		$(this).hide();
		$(this).parent().next().slideDown("slow");
		return false;
	});
	$(".hidden-details-less").click(function () {
		$(this).parent().add($(this).parent().nextUntil(":not(.hidden-details)")).slideUp("slow", function () {
			$(this).prev().children(".hidden-details-more").show();
		});
		return false;
	});
	
	// Display the latest blog posts.
	$(".caatblog-widget .rssFeed").rssfeed("http://blog.caat.org.uk/feed/", {
		limit: 3,
		header: false,
		titletag: "span",
		date: false,
		content: false,
		showerror: false
	});
	
	// Include Facebook SDK
	(function(d, s, id) {
		var js, fjs = d.getElementsByTagName(s)[0];
		if (d.getElementById(id)) return;
		js = d.createElement(s); js.id = id;
		js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1&appId=180838808679436";
		fjs.parentNode.insertBefore(js, fjs);
	}(document, 'script', 'facebook-jssdk'));
	
	// Google +1 render call
	window.___gcfg = {lang: 'en-GB'};
	(function() {
		var po = document.createElement('script'); po.type = 'text/javascript'; po.async = true;
		po.src = 'https://apis.google.com/js/plusone.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(po, s);
	})();
	
	// Twitter button
	!function(d,s,id) {
		var js, fjs = d.getElementsByTagName(s)[0];
		if (!d.getElementById(id)) {
			js = d.createElement(s);
			js.id = id;
			js.src = "//platform.twitter.com/widgets.js";
			fjs.parentNode.insertBefore(js, fjs);
		}
	}(document, "script", "twitter-wjs");
	
	// StumbleUpon
	(function() {
		var li = document.createElement('script'); li.type = 'text/javascript'; li.async = true;
		li.src = 'https://platform.stumbleupon.com/1/widgets.js';
		var s = document.getElementsByTagName('script')[0];
		s.parentNode.insertBefore(li, s);
	})();
	
	// Digg
	(function() {
		var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
		s.type = 'text/javascript';
		s.async = true;
		s.src = 'http://widgets.digg.com/buttons.js';
		s1.parentNode.insertBefore(s, s1);
	})();
});

