jQuery.prototype.RGUSubjectClass = function()
{
	var classes = [
		"architecture", "art", "business", "computing", "engineering",
		"health", "information", "laboratory", "law", "socialwork"
		];

	for(var i = 0, c; c = classes[i]; i++)
		if(this.hasClass(c))
			return c;

	return false;
}

$(function()
{
	initFeatureAccordions(); // should remain first
	initTabGroups();
	initAccordions();
	initAutoSubmitForms();
	initSearchSubmitHover();
});

function initTabGroups()
{
	$("div.tab-group").each(function()
	{
		var $tabGroup = $(this);
		var $tabMenu = $("<ol></ol>").addClass("tabs");
		
		$("> div.section", this).each(function(i)
		{
			var $tab = $(this)
				.removeClass("section")
				.addClass("tab");
			
			var title = $("> .section-title:eq("+i+")", $tabGroup)
				.hide()
				.text();
			
			var $link = $("<a></a>")
				.append(title)
				.click(function()
				{
					$("> div.selected-tab", $tabGroup).removeClass("selected-tab");
					$tab.addClass("selected-tab");
					
					$("> li.selected", $tabMenu).removeClass("selected");
					$tabMenuItem.addClass("selected");
					
					$("div.section-footnote", $tabGroup).hide();
					$("+ div.section-footnote", $tab).show();
				});
				
			var $tabMenuItem = $("<li></li>").append($link);
			if(i == 0)
				$tabMenuItem.addClass("first");
			
			$tabMenu.append($tabMenuItem)
		});
		
		$(this)
			.addClass("js-tab-group")
			.prepend($tabMenu);
			
		$("a:first", $tabMenu).trigger("click");
	});
}

var accordionAnimationTime = 500;
function initAccordions()
{
	$("div.accordion div.section")
		.wrap("<div class=\"accordion-wrapper\"></div>");
		
	$("div.accordion div.accordion-wrapper").hide();
	
	$("div.accordion").each(function()
	{
		var $accordion = $(this);
		$accordion.addClass("js-accordion");
		
		$("> .section-title", this).each(function(i)
		{
			$(this).click(function()
			{	
				var collapseOnly = $(this).hasClass("selected-section-title");
				
				// Collapse the current selection
				var $selTitle = $("> .selected-section-title", $accordion);

				if(selSubject = $selTitle.RGUSubjectClass())
					$selTitle.removeClass(selSubject+"-selected");

				$selTitle.removeClass("selected-section-title");
				$("> div.selected-section", $accordion).slideUp(accordionAnimationTime);
				
				// If the current selection is clicked, return after collapsing
				if(collapseOnly)
					return;
				
				// Highlight the newly selected title
				$(this).addClass("selected-section-title");
				if(mySubject = $(this).RGUSubjectClass())
					$(this).addClass(mySubject+"-selected");
				
				// Expand the newly seelected section
				//   Height must be set explicity before we animate, otherwise the animation's target
				//   height will ignore any internal margins and paddings and the end will be jerky
				var $sectionToShow = $("> div.accordion-wrapper:eq("+i+")", $accordion);
				$sectionToShow
					.show()	// Show to get the current height
					.css("height", $sectionToShow.height())	// Explicitly set the height
					.hide()	// Hide again ready to animate
					.addClass("selected-section")
					.slideDown(accordionAnimationTime, function()
					{
						$(this).css("height", "auto");	// Remove explicit height when animation is complete
					});
			});
		});
		
		$("> .section-title", this).hover(
			function()
			{
				$(this).addClass("section-title-hover")

				var subjectClass = $(this).RGUSubjectClass();
				if(subjectClass)
					$(this).addClass(subjectClass+"-hover");
			},
			function()
			{
				$(this).removeClass("section-title-hover");

				var subjectClass = $(this).RGUSubjectClass();
				if(subjectClass)
					$(this).removeClass(subjectClass+"-hover");
			}
			);
		
		$("> .comment-title", this).hover(
			function() {$(this).addClass("comment-title-hover")},
			function() {$(this).removeClass("comment-title-hover")}
			);

		if(!$accordion.hasClass("all-collapsed"))
		{
			accordionAnimationTime = 0;
			$("> .section-title:first", $accordion).trigger("click");
			accordionAnimationTime = 500;
		}
	});
}

var featureAccordionAnimationTime = 250;
function initFeatureAccordions()
{
	$("div.feature-accordion div.section")
		.wrap("<div class=\"accordion-wrapper\"></div>");
		
	$("div.feature-accordion div.accordion-wrapper").hide();
	
	$("div.feature-accordion").each(function()
	{
		var $accordion = $(this);
		
		$accordion.addClass("feature-accordion-js");
		
		$("> .section-title", this).each(function(i)
		{
			$(this).click(function()
			{	
				if($(this).hasClass("selected-section-title"))
					return;
				
				// Collapse the current selection
				$("> .selected-section-title", $accordion).removeClass("selected-section-title");
				
				// Highlight the newly selected title
				$(this).addClass("selected-section-title");
				
				// Work out which section and feature will be shown
				var $sectionToHide = $("> div.selected-section", $accordion);
				var $featureToHide = $(".feature", $sectionToHide);
				var $sectionToShow = $("> div.accordion-wrapper:eq("+i+")", $accordion);
				var $featureToShow = $(".feature", $sectionToShow);
				
				// Hide the visible section and show the new section to get the height
				$sectionToShow
					.css("height", "auto")
					.css("visibility", "hidden")
					.show();
					
				$sectionToHide.hide();
				
				// Work out if we need to add anything to the height
				// The feature is absolutely positioned, so without this it could overflow the container
				var fHeight = $featureToShow.height();
				var aHeight = $accordion.height();
				var sHeight = $sectionToShow.get(0).offsetHeight + (fHeight > aHeight ? fHeight - aHeight : 0);
				
				// Height calculations are complete, reset the visibility to what it should be
				// This should all have happened too quickly for the user to notice
				$sectionToShow.css("visibility", "visible").hide();
				$sectionToHide.show();
				
				// Animate the hiding of the current section
				$sectionToHide
					.slideUp(featureAccordionAnimationTime)
					.removeClass("selected-section");
				
				// Update the z-index of the features so they are all at z=1 except the current feature at z=2
				$(".feature", $accordion).css("z-index", "1");
				$featureToHide.css("z-index", "2");
				
				// Move the newly selected feature to the top of the pile (z=3) and fade it in
				$featureToShow
					.hide()
					.css("z-index", "3")
					.fadeIn(featureAccordionAnimationTime);
					
				// Animate the newly selected section
				$sectionToShow
					.css("height", sHeight)	// Explicitly set the height
					.addClass("selected-section")
					.slideDown(featureAccordionAnimationTime);
			});
		});
		
		featureAccordionAnimationTime = 0;
		$("> .section-title:first", $accordion).trigger("click");
		featureAccordionAnimationTime = 250;
	});
}

function initAutoSubmitForms()
{
	$("form.autosubmit input[type=submit], form.autosubmit button[type=submit]").hide();
	$("form.autosubmit select").change(function()
	{
		if($(this).attr("value") == "")
			return;
			
		$(this).parents("form").trigger("submit");
	});
}

function initSearchSubmitHover()
{
	$("form#search button").hover(
		function() {$(this).addClass("hover")},
		function() {$(this).removeClass("hover")}
	);
}
