﻿$(document).ready(function(){
/*
 *	jQuery to show the course description when the course row is clicked
 *	this version uses a table to make it more accessible
 *	RJB 4/2/09
 */
 	var btnStatus = "show";
	// On load, javascript is enabled, so change css for the link on the course name
	$(".courses a").css({'text-decoration' : 'none'});
	
	// Add class 'link' to all the course link rows
	$('.courses tbody tr').addClass('link');
	
	// add rows to put course descriptions into
	$(".courses tbody tr").after('<tr class="courseDesc"><td colspan="3">&nbsp;</td></tr>');
	
	// hide all course description rows initially
	$("table.courses tr.courseDesc").hide();
	
	// load all the course description html into the rows
	var courseLink;								/* initialize link variable */

	$(window).bind("load", function() {			/* wait until the window has loaded before loading course descriptions */
		$("tr.link td a").each(function (i) {
			courseLink = $(this).attr('href');		/* get the value of the course link */
			courseLink += ' div.course-content';	/* add this limiter to load only the div portion of the course description page */
			$(this)
			.parent()		/* find td tag that contains this anchor link */
			.parent()		/* find tr tag that contains the td */
			.next()			/* find the next tag which is the tr that will contain the course description */
			.find('td')		/* find the td within that tr */
			.load(courseLink)		/* load the html from the linked page into that td */
			.end();			/* get 'this' back to the a tag so that the each function can handle the next one */
		});
	});

	// on hover highlight the row by adding the hover class css
	$('table.courses tr.link').hover(
	  function () {
	    $(this).addClass("hover");
	  },
	  function () {
	    $(this).removeClass("hover");
	  }
	);

	// on click of the course name row, show or hide individual course description
	$("table.courses tr.link").click(function () { 
		$(this)
			.children()			// get all the td's which are in this tr
			.toggleClass("descShowing")	/* hide the border so the borders define a single course */
			.end()				/* reset to the original 'this' to do the next part */
		.next()				/* get the next tr which contains the description */
			.animate({"height": "toggle", "opacity": "toggle"}, { duration: "normal" })
			;
		return false;
	});							/* end of show or hide individual course descriptions */

	// on click of the show/hide button, show or hide all course descriptions
	$("a.showHide").click(function () {			/* This function shows all course descriptions */
		if (btnStatus == "show") { 
			$("a.showHide").text("hide all");	/* change the link to say hide */
			$("table.courses tr.link")			/* get the table row with class of link which is an array of all of them */
			.each(function (i) {				/* loop through the array of rows and do the following steps */
				$(this)
				.children()						/* get all the td's which are in this tr */
				.addClass("descShowing")	/* hide the border so the borders define a single course */
				.end()							/* reset to the original 'this' to do the next part */
				.next()							/* get the next tr which contains the description */
				.animate({"height": "show", "opacity": "show"}, { duration: "normal" })
			});
			btnStatus = "hide";
		}
		else {
			$("a.showHide").text("show all");	/* change the link to say show */
			$("table.courses tr.link")			/* get the table rows with class of link which is an array of all of them */
			.each(function (i) {				/* loop through the array of rows and do the following steps */
				$(this)
				.children()						/* get all the td's which are in this tr */
				.removeClass("descShowing")	/* show the border so the borders define a single course */
				.end()							/* reset to the original 'this' to do the next part */
				.next()							/* get the next tr which contains the description */
				.animate({"height": "hide", "opacity": "hide"}, { duration: "normal" })
			});
			btnStatus = "show";
		} 	/* end of if statement */
		return false;
	});		/* end of show or hide all course descriptions */
}); /* end of document.ready function */
