// ENF-Lightbox
// Created by Teddy Fishbein
// Copyright 2012

$(window).load(function () {
    $('a[rel="video"] img').each(function () {
        imgPos = $(this).position();
        imgLeft = imgPos.left + ($(this).width() / 2) - 25;
        imgTop = imgPos.top + ($(this).height() / 2) - 25;
        $('<img style="position:absolute;left:' + imgLeft + 'px;top:' + imgTop + 'px;cursor:pointer;width:49px;height:49px;background:transparent;border:0px;" src="/images/video-button.png"/>').appendTo($(this).parent());
    });
});

$(document).ready(function () {
    $('a[rel="video"]').click(function () {
        var url = $(this).attr('href');

        $('<div id="enf-lb-bg" style="position:absolute; z-index:100000; top:0; left:0; height:100%; width:100%; background:#53626f; display:none;"></div>').appendTo('body');
        $('<div id="enf-lb-fg" style="position:absolute; z-index:100001; background:white; width:600px; padding:30px; display:none; border-radius:10px; -moz-border-radius:10px; -webkit-border-radius:10px;text-align:right;"><div id="close-video" style="position:absolute;top:5px;right:15px;cursor:pointer;color:#0079C1;">Close</div></div>').appendTo('body');
        $('<iframe id="enf-lb-frame" height="338" width="600" style="border:0px;height:338px;width:600px;" src="' + url + '" frameborder="0" scrolling="no"></iframe>').appendTo('#enf-lb-fg');

        positionLB();

        $('#enf-lb-bg').fadeTo(500, .75);
        $('#enf-lb-fg').fadeTo(500, 1);

        $('#enf-lg-fg').load(url);

        $('#enf-lb-bg').click(function () {
            closeVideo();
        });

        $('#close-video').click(function () {
            closeVideo();
        });

        $(window).resize(positionLB);

        return false;
    });


    function positionLB() {
        var leftPos = ($(window).width() / 2) - ($('#enf-lb-fg').width() / 2)
        var topPos = ($(window).height() / 2) - ($('#enf-lb-fg').height() / 2) + $(window).scrollTop() - 100

        $('#enf-lb-fg').css('left', leftPos);
        $('#enf-lb-fg').css('top', topPos);
        $('#enf-lb-bg').width($(document).width());
        $('#enf-lb-bg').height($(document).height());
    }

    function closeVideo() {
        $('#enf-lb-bg').fadeOut(500, function () {
            $('#enf-lb-bg').remove();
        });
        $('#enf-lb-fg').fadeOut(500, function () {
            $('#enf-lb-fg').remove();
        });
    }
});
