$(function() {
	$('a[href^="http://www.youtube.com/watch?v="]').click( function( e ) {
		e.preventDefault();
		$('#movie-wrapper').remove();
		var url = this.href.replace( '/watch?v=', '/v/' ) + '&hl=en&fs=1&autoplay=1';

		// This was very elegant but IE choked on it.
		// After a few hours of debugging, now it is ugly but works in IE.

		var movieWrapper = $._div_( { 'id': 'movie-wrapper' } )
		.click( function( e ) {
			$(this).fadeOut( 'slow', function() { $(this).remove(); } );
		} );

		var movie = $._div_( { 'id': 'movie' } )
		.click( function( e ) {
			e.stopImmediatePropagation();
		} );

		movie.append( $._div_( { 'id': 'movie-placeholder' } ) );

		movieWrapper.append( movie );

		movieWrapper.append(
			$._a_( { 'href': '#' } )
			.click( function( e ) {
				e.preventDefault();
				$('#movie-wrapper').click();
			} ).text( 'Close' )
		);


		function embedTheVideo() {
			swfobject.embedSWF( url, 'movie-placeholder', '100%', '100%', '9', null, null, { 'quality': 'highest', 'wmode': 'window' }, {} );
		}

		movieWrapper.hide();
		$('#home_main').before( movieWrapper );
		movieWrapper.fadeIn('slow', embedTheVideo);
	});
});