$(document).ready( 
	function()
	{
		$("#schedulesdiv").text( "The schedules will appear momentarily ..." );
		Ajax( "IMACSWeb", "schedules", { dbname: $("#dbname").val() }, 
			function( xml )
			{
				var	s = "",
					t = "";
			
				$(xml).find( "r" ).each( 
					function()
					{
						var r = $(this),
							y = parseInt( r.attr( "y" ) ),
							q = parseInt( r.attr( "q" ) ),
							name = r.attr( "name" );
							
						s += "<div id=\"" + y + "Q" + q + "tab\" class=\"tab\"><p>" + name + "</p></div>";
					
						if ( q < 5 )	
						{
							t += Quarter1234( r, y, q );
						}
						else	
						{
							t += Summer( r, y, q );
						}
					}
				);
			
				$("#schedulesdiv")
					.empty()
					.append( 
						"<div id=\"schedule\">" +
							"<div id=\"tabpane\">" +
								"<div class=\"tailtab\">" + 
								s +
								"</div>" +
								"<div id=\"openingdiv\">Click one of the above tabs to view scheduling information.</div>" +
								t +
							"</div>" +
						"</div>" );
						
				$(".detailslink").click( 
					function( evt )
					{
						$(this).attr( "id" ).match( /details(\d+)/ );
						Details( parseInt( RegExp.$1 ) );
						return false;						
					}
				);
				
				$("div.selector > ul > li,div.selector > ul > li > ul > li,div.selector > ul > li > ul > li > ul > li")
					.hover(
						function()
						{
							$(this).children( "p" ).css( "text-decoration", "underline" );
						},
						function()
						{
							$(this).children( "p" ).css( "text-decoration", "none" );
						} 
					);
				
				$("div.selector").each( // Simplify wording if only one top-level menu item
					function()
					{
						if ( $(this).find( '> ul > li' ).length == 1 )
						{
							$(this).siblings( 'p.coursesinstructions' ).text( 'To view class availability, click a class description.' );
							$(this).siblings( 'p.summerinstructions' ).text( 'To view class availability, click a week.' );
						}
						else
						{
							$(this).siblings( 'p.coursesinstructions' ).text( 'To view class availability, click a subject and then a class description.' );
							$(this).siblings( 'p.summerinstructions' ).text( 'To view class availability, click a location and then a week.' );
						}
					});

				$("div.tab").click(
					function()
					{
						$(this).attr( 'class', 'seltab' );
						$(this).siblings().attr( 'class', 'tab' );
						var selID = $(this).attr( 'id' );
						var divID = selID.substring( 0, selID.length - 3 ) + 'div';
						$( '#' + divID ).siblings().not( '.tailtab' ).addClass( 'hide' );
						$( '#' + divID ).removeClass( 'hide' );
								
						return false;
					});
					
				$("div.selector > ul > li,div.selector > ul > li > ul > li").click(
					function()
					{
						$(this).children( 'ul' ).toggle();
						return false;
					});
				
				$("div.selector > ul > li > ul > li > ul > li,div.summerselector > ul > li > ul > li").click(
					function()
					{
						$(this).children( "table" ).toggle();
						return false;
					});
			},
			function()
			{
				alert( "The schedules are not currently available, please try again later.\r\nWe apologize for the inconvenience." );
			} );
	}
);

function Details( classid )
{
	$("#schedule").css( "opacity", 0.2 );
	$(".classdetailsdiv").remove();
	
	Ajax( "IMACSWeb", "classdetails", { dbname: $("#dbname").val(), classid: classid }, 
			function( xml )
			{
				$("#schedulesdiv").css( "position", "relative" );
				$("#schedulesdiv").append( 
					"<div class=\"classdetailsdiv\">" +
					"<a class=\"closelink\" href=\"#\"><img title=\"Close\" class=\"closebutton\" src=\"/FileData/IMACSWeb/images/Closebutton.gif\" /></a>" +
					$(xml).find( "contents" ).text() +
					"</div>" );
					
				$("#schedulesdiv .closebutton").hover( 
					function()
					{	
						$(this).attr( "src", "/FileData/IMACSWeb/images/ClosebuttonHover.gif" );
						return false;
					},
					function()
					{	
						$(this).attr( "src", "/FileData/IMACSWeb/images/Closebutton.gif" );
						return false;
					}
				);
				
				$("#schedulesdiv .closelink").click(
					function()
					{
						$("#schedule").css( "opacity", 1 );
						$(".classdetailsdiv").remove();
						return false;
					}
				)
			}
	);
	
}

function Quarter1234( r, y, q  )
{
	var t = "<div id=\"" + y + "Q" + q + "div\" class=\"hide\">" +
								"<p class=\"coursesinstructions\">To view class availability, click a subject and then a class description.</p>" +
								"<div class=\"selector q1234selector wide\">" +
								"<ul class=\"listlevel1\">"; 

	r.find( "cat" ).each( 
		function()
		{
			var cat = $(this);
			
			t += "<li class=\"level1\"><p>" + cat.attr( "name" ) + "</p>" +
					"<ul class=\"listlevel2\">";
			
			cat.find( "type" ).each( 
				function()
				{
					var type = $(this);
					
					t += "<li class=\"level2\"><p>" + type.attr( "name" ) + "</p>" +
							"<ul class=\"listlevel3\">";
					
					type.find( "loc" ).each( 
						function()
						{
							var loc = $(this),
								typename = loc.find( "c:first" ).attr( "t" );
							
							t += "<li><p>" + loc.attr( "name" ) + "</p>" +
									"<table border=0 cellspacing=0 cellpadding=0>" +
									"<tr>" +
									"<td colspan=\"3\" class=\"title\">" +
										loc.attr( "name" ) + " &mdash; " + typename +
									"</td>" +
									"</tr>";
							
							loc.find( "c" ).each(
								function()
								{
									var c = $(this);
									
									t += "<tr>" +
											"<td>" + c.attr( "f" ) + "</td>" +
											"<td>" + c.attr( "s" ).replace( /-/, " &ndash; " ) + "</td>" +
											"<td><a id=\"details" + c.attr( "i" ) + "\" class=\"detailslink\" href=\"#\">Details &raquo;</a></td>" +
											"</tr>";
								}
							);
							
							t += "</table></li>";
						}
					);
					
					t += "</ul></li>";
				}
			);
			
			t += "</ul></li>";
		}
	);
			
	t += "</ul></div></div>";
						
	return t;
}

function Summer( r, y, q )
{
	var t = "<div id=\"" + y + "Q" + q + "div\" class=\"hide\">" +
								"<p class=\"coursesinstructions\">To view class availability, click on a location and then on a week.</p>" +
								"<div class=\"selector summerselector wide\">" +
								"<ul>"; 

	r.find( "loc" ).each(
		function()
		{
			var loc = $(this);
			
			t += "<li><p>" + loc.attr( "name" ) + "</p>" +
					"<ul>";			
					
			loc.find( "session" ).each(
				function()
				{
					var session = $(this);
					
					t += "<li><p>Week beginning " + session.attr( "d" ) + ", " + session.attr( "w" ) + "</p>" +
							"<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">" +
							"<tr>" +
							"<td colspan=\"3\" class=\"title\">" +
								"Summer Classes in " + loc.attr( "name" ) + ", " +  session.attr( "d" ) + ", " + session.attr( "w" )
							"</td>" +
							"</tr>";
							
					session.find( "c" ).each(
						function()
						{
							var c = $(this),
								b = c.attr( "p" ),
								isfull = (b == "full"),
								availability = (b == "limited" ? 
													"<em>Limited</em>" : 
													(b == "full" ? 
														"<em>Full</em>" :
														"Available"));
									
									
									
							t += "<tr class=\"" + b + "\">" +
									"<td>" + c.attr( "n" ) + "</td>" +
									"<td>" + (c.attr( "y" ) + " " + c.attr( "s" )).replace( /-/, " &ndash; " ) + "</td>" +
									"<td>" + availability + "</td>" +									
									"</tr>";	
						}
					);
					
					t += "</table></li>";
				}
			);
					
			t += "</ul></li>";	
		}
	);
	
	t += "</ul></div></div>";
	
	return t;
}
