/** Do all the following once the page is fully loaded */
$(document).ready(function() {
	/* As soon as possible, display: none; for all tabs, track the first. */
	var top_content = $('.content').css('display','none').first();
	/* Link all anchors with external relation to open in new window. */
	$('a[rel="external"]').attr('target', '_blank');
	$('a.resource_link').attr('target', '_blank');
	/* Add the usual behavior to clicking the school logo. */
	$('#side_nav_top').css('cursor','pointer')
		.click(function(event) {
			event.preventDefault();
			window.location.href = 'index.htm';
		});
	/* General CSS style changes for dynamic content switching (mouse in/out) */
	$('.side_nav_link').css('cursor','pointer')
		.hover(
			function() { $(this).addClass('on'); },
			function() { $(this).removeClass('on'); }
		);
	/* Dynamically replace <a href="file.pdf">Text</a> with <u id="file.pdf">Text</u>, but replicate link behavior */
	/* This is complicated, but makes it work regardless of having or not having either CSS or JS */
	/* Those who need accessible browsing should simply disable JS on this site, and everything will be fine */
	/* No CSS: little weird, but the underline at least looks like a link (function achieved through JS) */
	/* No JS: this script doesn't even run, there is a normal <a href="file.pdf">Text</a> on the page. */
	$('#side_nav_links .resource_link')
		.click(function(event) {
			/* Ignore the normal click event and find the inner span. */
			event.preventDefault();
			var target = $(event.target).is('span') ?
				event.target : $(event.target).children('span:first');
			/* Redirect the user to the former link's target. */
			window.location.href = $(target).attr('id');
		})
		/* All the anchors need to be switched with spans. */
		/* The anchor's href will be copied into the span's id. */
		.children('a').replaceWith(function() {
			return '<span id="' + $(this).attr('href') + '">' + $(this).html() + '</span>';
		});
	/* Activate behavior for all the side links, and hide all but first */
	/* Calculates the id of the element to hide and then the one to show */
	/* Checks that these are not the same element before doing fade switch */
	var top_link = $('#side_nav_links .content_link')
		.click(function(event) {
			event.preventDefault();
			if (!$(event.target).is('.current_slide')) {
				var old_id = '#' + $('#side_nav_links .current_slide').removeClass('current_slide').attr('id') + '_content';
				var new_id = '#' + $(event.target).addClass('current_slide').attr('id') + '_content';
				$(old_id).fadeOut('fast', function() { $(new_id).fadeIn('slow'); });
			}
		}).first();
	/* Experimental feature -- load proper slide based on hash */
	var target = window.location.hash.replace(/_slide$/,'');
	if ($(target).is('.resource_link')) { $(target).click(); }
	if ($(target).is('.content_link')) { top_link = $(target); }
	if ($(top_link).is('.content_link')) {
		top_content = '#' + $(top_link).addClass('current_slide').attr('id') + '_content';
	}
	$(top_content).fadeIn('slow');
});

/* Google Analytics code (adds itself as first script) */
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7939412-1']);
_gaq.push(['_trackPageview']);
(function() {
	var ga = document.createElement('script');
	ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0];
	s.parentNode.insertBefore(ga, s);
})();

/* Stupid idea (kept for posterity) */
// window.onload = function() { $('body').hide().fadeIn(); }


