$(document).ready(function(e) {
    
    /*
     * Closes video when the backdrop is clicked.
     */
    $('#video-container, #close-box').live('click', function(e) {
        stopVideo();
    });
    
});

/*
 * Plays the specified video in a centered in a backdrop container.
 */
function playVideo(video_url, container_style, width, height) {
    
    if (container_style == 'opaque') {
        $('#container').append('<div id="video-container" style="display: none"><div id="video-wrapper"></div></div>');
    }
    else {
        $('#container').append('<div id="video-wrapper"></div>');
    }
    
    /* Insert video */
    var html = '<iframe id="video" src="' + video_url + '" width="' + width + '" height="' + height + '" frameborder="0"></iframe>';
    $('#video-wrapper').html(html);
    $('#video-wrapper').css('width', width);
    $('#video-wrapper').css('height', height);
    $('#video-wrapper').css('margin-left', '-' + width/2 + 'px');
    $('#video-wrapper').css('margin-top', '-' + height/2 + 'px');
    
    /* Close box */
    $('#video-wrapper').append('<div id="close-box"></div>');
    
    /* Show video with backdrop */
    if (container_style == 'opaque') {
        $('#video-container').removeClass();
        $('#video-container').addClass('opaque');
        $('#video-container').fadeIn('fast');
    };
    
}

function stopVideo() {
    
    /* Remove video */
	if ($('#video-container').length > 0) {
		$('#video-wrapper').html('');
	}
	else {
    	$('#video-wrapper').remove();
	}

    /* Hide video and backdrop */
    $('#video-container').fadeOut('fast');
    
}
