GetSeasonEpisodes = 
{
	init: function()
	{
		var aItems = $A(document.getElementsByClassName('season-link'));
		
		aItems.each(function(pItem)
		{ 
			pItem.setAttribute('href', 'javascript:void(0)');	
			pItem.onclick = function()
			{
				GetSeasonEpisodes.exec(this);
				return false;
			}
		});
	},
	
	exec: function(pItem)
	{
		var iCategoryId = pItem.getAttribute('rel');
		var pParams 	= $H({category_id: iCategoryId});
		var pTable 		= $('episodes');
		var pDefaultRow = $('clone_row');

		/* Delete every table row but the first. */
		$A(pTable.rows).each
		(
			function(pElement, iIndex)
			{
				if (iIndex <= 1) 
				{
					return;
				}
				
				$(pElement).remove();
			}
		);
		
		var pRow 	= pTable.insertRow(1);
		var pCell	= pRow.insertCell(0);
		
		$(pCell).setAttribute('colspan', 7);
		$(pCell).addClassName('nb');
		$(pCell).update(Semto_Wait.getAsHtml('<strong>One moment!</strong> Loading season...'));

		/* Set the <title> and <h1> tags to include the current season. */
		if (pItem.hasAttribute('title'))
		{
			var pH1		= $('content-block-container').down('h1');
			var szTitle = pH1.innerHTML;
			var szTitle = szTitle.replace(/Online/i, '');
			var szTitle = szTitle.replace(/Season \d+/i, '');
			var szTitle = szTitle.replace(/\s+$/, '');
			var szTitle = szTitle + ' ' + pItem.getAttribute('title') + ' Online';
			
			document.title 	= szTitle + ' - Vids.tv';
			pH1.update(szTitle);
		}
		
		AjaxHelper.request('get-sub-categories', pParams, function(pTransport)
		{
			var pData = pTransport.responseText.evalJSON(true);
			$(pRow).remove();
			
			pData.each
			(
				function (pElement)
				{
					var pClone = pDefaultRow.cloneNode(pDefaultRow);
					pTable.tBodies[0].appendChild(pClone);
					var pClone = $(pClone);

					pClone.removeClassName('hidden');
					pClone.down('td', 0).update(pElement.title);
					pClone.down('td', 1).update(pElement.season);
					pClone.down('td', 2).update(pElement.episode);
					pClone.down('td', 3).update(pElement.num_videos);
					pClone.down('td', 4).update(pElement.date_aired);
					pClone.down('td', 5).update(pElement.rating);
					
					if (pElement.num_videos == 0)
					{
						$(pClone).toggleClassName('noVideos');
					}
				}
			);
		});
	}
}

Event.observe(window, 'load', GetSeasonEpisodes.init);