var video_template = Handlebars.compile($('#video-template').html());

function hide(elem) {
    elem.fadeout(function() { elem.remove(); });
}

function build_overlay(video) {
    var url = 'http://www.youtube.com/embed/' + video + '?rel=0&amp;hd=1';
    var overlay = $(video_template({src: url}));
    overlay.click(function() {
        $(this).fadeOut().remove();
    });
    overlay.find('a').click(function() {
        hide(overlay);
        return false;
    });
    $('body').prepend(overlay);
}

$(function() {
    $('a.videolink').click(function(e) {
        var video = $(this).data('video');
        build_overlay(video);
        return false;
    });
});

$(document).keyup(function(e) {
    if (e.keyCode == 27) {
        hide($('.overlay'));
    }
});

