$(function() {
    $(".video").on('click', function () {
        var theModal = $(this).data("target"),
        videoSRC = $(this).attr("data-video"),
        title = $(this).attr("data-title"),
        main_author = $(this).attr("data-main_author"),
        videoSRCauto = videoSRC + "";
        $(theModal + ' source').attr('src', videoSRCauto);
        $(theModal + ' h4').html(title + '<br />' + $(this).prev().html());
        $(theModal + ' #main_author').attr('href', 'mailto:' + main_author);
        var videoInstance = $(theModal + ' video');
        videoInstance[0].load();
        videoInstance[0].play();
        $(theModal + ' button.close').click(function () {
            $(theModal + ' source').attr('src', videoSRC);
            videoInstance[0].pause();
        });
        $(theModal ).on("hidden.bs.modal", function () {
            $(theModal + ' source').attr('src', videoSRC);
            videoInstance[0].pause();
        });
    });
    
    $(document).ready(function(){
    function alignModal(){
        var modalDialog = $(this).find("#videoModal .modal-dialog");
        
        // Applying the top margin on modal to align it vertically center
        modalDialog.css("margin-top", Math.max(0, ($(window).height() - modalDialog.height()) / 2));
    }
    // Align modal when it is displayed
    $("#videoModal").on("shown.bs.modal", alignModal);
    
    // Align modal when user resize the window
    $(window).on("resize", function(){
        $("#videoModal:visible").each(alignModal);
    });   
});
    
});

