var SubmitVideo = 
{
	init: function()
	{
		if (!$('show_index'))
		{
			return;
		}

		var iAutomaticShowId = parseInt($('show_index').getValue());

		if (iAutomaticShowId && iAutomaticShowId != 0)
		{
			$('aVideo[show_id]').selectedIndex = iAutomaticShowId;
			
			var iSelectedIndex = $('aVideo[show_id]').selectedIndex;
			GetCategories.exec($('aVideo[show_id]').options[iSelectedIndex].value); 
			SubmitVideo.deleteEpisodeOptions();
		}
		
		$('aVideo[show_id]').onchange = function() 
		{ 
			GetCategories.exec($F('aVideo[show_id]')); 
			SubmitVideo.deleteEpisodeOptions();
			
		};
		
		$('aVideo[category_id]').onchange = function() 
		{ 
			GetSubCategories.exec($F('aVideo[show_id]'), $F('aVideo[category_id]')); 
			SubmitVideo.deleteEpisodeOptions();
		};
		
//		$('video_type').onchange = function()
//		{
//			if (this.selectedIndex == 0)
//			{
//				$('video_data').update('<td><label for="aVideo[link]">Direct Link:</label></td><td><input style="width: 400px;" type="text" name="aVideo[link]" id="aVideo[link]" /></td>');
//			}
//			else
//			{
//				$('video_data').update('<td style="vertical-align: top;"><label for="aVideo[code]">Embed Code:</label></td><td><textarea rows="8" cols="50" name="aVideo[code]" id="aVideo[code]"></textarea></td>');
//			}
//		}
		
		$('form_submitted').onclick = function()
		{
			if (!SubmitVideoForm.validate())
			{
				return false;
			}

			var pParams = $H({
					show_id: $F('aVideo[show_id]'),
					category_id: $F('aVideo[category_id]'),
					sub_category_id: $F('aVideo[sub_category_id]'),
					video_type: $F('video_type'),
					part: $F('aVideo[part]')});

			if ($F('video_type') == 'code')
			{
				pParams.code = $F('aVideo[code]');
			}
			else
			{
				pParams.link = $F('aVideo[link]');
			}

			AjaxHelper.request('submit-video', pParams, function(pTransport) 
			{
				var pJson = pTransport.responseText.evalJSON(true);

				$('form_submitted').enable();

				if (pJson.status == false)
				{
					alert(pJson.result);
					return;
				}
				else
				{
					alert(pJson.result);
					$('aVideo[link]').clear();
					$('aVideo[link]').select();
					$('submit-video-container').hide();
				}
			});
			
			this.disable();
			
			return true;
		}
	},
	
	deleteEpisodeOptions: function()
	{
		var pEpisode = $('aVideo[sub_category_id]');
		for (var i = 1; i < pEpisode.options.length; i++)
		{
			pEpisode.remove(i);
			i--;
		}
	}
}

var SubmitVideoForm =
{
	validate: function()
	{
		if ($('aVideo[show_id]').selectedIndex == 0)
		{
			alert('You must select a show to be able to submit a video.');
			return false;
		}
		
		if ($('aVideo[category_id]').selectedIndex == 0)
		{
			alert('You must select a category to be able to submit a video.');
			return false;
		}	
		
		if ($('aVideo[sub_category_id]').selectedIndex == 0)
		{
			alert('You must select a sub-category to be able to submit a video.');
			return false;
		}		

		if ($F('aVideo[' + $F('video_type') + ']').length == 0)
		{
			alert('You must enter a video ' + $F('video_type'));
			return false;
		}
				
		if ($F('aVideo[show_id]').length == 0 || $F('aVideo[category_id]').length == 0 || $F('aVideo[sub_category_id]').length == 0)
		{
			alert('Naughty, naughty!');
			return false;
		}
		
		return true;
	}
}

GetCategories = 
{
	exec: function(iShowId)
	{
		var pParams = $H({show_id: iShowId});
		AjaxHelper.request('get-categories-as-options', pParams, GetCategories.success);
		pTable = $('aVideo[show_id]').up('table');
		 
		$('aVideo[category_id]').options[0].innerHTML = 'Loading (Please Wait)...';
		$('aVideo[category_id]').disable();
		
//		var pImage = LoadingImage.getImage('Loading categories...');
//		pImage.show();
//		pTable.parentNode.insertBefore(pImage, pTable);
	},
	
	success: function(pTransport)
	{
		$('aVideo[category_id]').enable();
		$('aVideo[category_id]').up().update(pTransport.responseText);
		$('aVideo[category_id]').onchange = function()
		{
			GetSubCategories.exec($F('aVideo[show_id]'), $F('aVideo[category_id]'));
			SubmitVideo.deleteEpisodeOptions();
		};
		
		LoadingImage.hide();
	}
}

GetSubCategories = 
{
	exec: function(iShowId, iSeason)
	{
		var pParams = $H({show_id: iShowId, category_id: iSeason});
		AjaxHelper.request('get-sub-categories-as-options', pParams, GetSubCategories.success);
		pTable = $('aVideo[show_id]').up('table');
		
		$('aVideo[sub_category_id]').options[0].innerHTML = 'Loading (Please Wait)...';
		$('aVideo[sub_category_id]').disable();
		
//		var pImage = LoadingImage.getImage('Loading episodes...');
//		pImage.show();
//		pTable.parentNode.insertBefore(pImage, pTable);
	},
	
	success: function(pTransport)
	{
		$('aVideo[sub_category_id]').enable();
		$('aVideo[sub_category_id]').up().update(pTransport.responseText);
		LoadingImage.hide();
	}
}

Event.observe(window, 'load', SubmitVideo.init);