function othersShow( type ) {
	$('#othervideostable').addClass( 'throbber' );

	$.ajax({
		type: 'GET',
		url: 'getvideotable.php',
		data: 'type=' + type,
		dataType: 'html',
		success: function( msg ) {
			$('#othervideostable').removeClass( 'throbber' );
			$('#othervideoslinks').hide();
			var table = $(msg).hide();
			table.appendTo( $('#othervideostable') ).slideDown();
			var closeLink = $('<a class="othervideoscloselink" href="#">Close</a>');
			$('#othervideostable').prepend( closeLink );

			$('#othervideostable .videothumb a').click( function() {
				var youtubeID = $(this).closest( '.videothumb' ).find('.youtubeid').val();
				var videoTitle = $(this).closest( '.videothumb' ).find('.videothumbdesc a').text();
				var videoHTML = othersVideoHTML( videoTitle, 500, 300, youtubeID );

				$('#content-wrap').hide();
				$.fancybox(
					videoHTML,
					{
					'autoDimensions'	: false,
					'width'         		: 520,
					'height'        		: 'auto',
					'transitionIn'		: 'none',
					'transitionOut'		: 'none',
					'onClosed' : function() {
							$('#content-wrap').show();
						}
					}
				);
				return false;
			});

			closeLink.click( function() {
				$('#othervideostable table').slideUp( 'normal', function() {
					$(this).remove();
					$('#othervideostable .othervideoscloselink').remove();
					$('#othervideoslinks').show();
				});
				return false;
			});
		},
		error: function( jqXHR, textStatus, errorThrown ) {
			$('#othervideostable').removeClass( 'throbber' );
		}
	});
}

function othersAddLinks() {
	var musicLink = $('<a href="#">See other popular videos (music)</a>').click( function() {
		othersShow( 'music' );
		return false;
	});
	var newsLink = $('<a href="#">See other popular videos (news and other)</a>').click( function() {
		othersShow( 'other' );
		return false;
	});

	$('#othervideoslinks').append( musicLink, '&nbsp;&nbsp;', newsLink );
}

function othersVideoHTML( title, w, h, code ) {
	return '<div id="fancybox-video"><object width="' + w + '" height="' + h + '"><param name="movie" value="http://www.youtube-nocookie.com/v/' + code + '?version=3&amp;hl=en_US&amp;rel=0"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube-nocookie.com/v/' + code + '?version=3&amp;hl=en_US&amp;rel=0" type="application/x-shockwave-flash" width="' + w + '" height="' + h + '" allowscriptaccess="always" allowfullscreen="true"></embed></object><div class="fancybox-video-desc">' + title + '</div></div>';
}

$(document).ready(function() {
	othersAddLinks();
});

