$(document).ready(function () {
    $('a[name=modal]').click(function (e) {
        e.preventDefault();

        var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        $('#mask').css({ 'width': maskWidth, 'height': maskHeight });
        $('#mask').fadeIn(500);
        $('#mask').fadeTo("slow", 0.5);

        //Get the window height and width
        var winH = $(window).height();
        var winW = $(window).width();

        //Open Dialog from HREF
        var id = $(this).attr('href');
        var scrollTopo = $(window).scrollTop();
        $(id).css('top', parseInt(winH / 2 - $(id).height() / 2) + scrollTopo);
        $(id).css('left', winW / 2 - $(id).width() / 2);
        $(id).fadeIn(1000);
    });

    $(window).scroll(function () {
        if ($('#box').is(":visible")) {
            //Get the window height and width
            var winH = $(window).height();
            var winW = $(window).width();

            //Open Dialog from HREF
            var id = '#box';
            var scrollTopo = $(window).scrollTop();
            $(id).css('top', parseInt(winH / 2 - $(id).height() / 2) + scrollTopo);
            $(id).css('left', winW / 2 - $(id).width() / 2);
        }
    });

    $(window).scroll(function () {
        if ($('#boxSenha').is(":visible")) {
            //Get the window height and width
            var winH = $(window).height();
            var winW = $(window).width();

            //Open Dialog from HREF
            var id = '#box';
            var scrollTopo = $(window).scrollTop();
            $(id).css('top', parseInt(winH / 2 - $(id).height() / 2) + scrollTopo);
            $(id).css('left', winW / 2 - $(id).width() / 2);
        }
    });

    $("#fechar, #mask").live("click", function (event) {
        $('.window').fadeOut('slow', function () {
            $('.window').hide();
            $('#mask').hide();
        });

        $(".yplayer").each(function () {
            var playercopy = $(this).children().clone();
            $(this).children().remove();
            $(this).html(playercopy);
        });
    });

    $("#fecharSenha, #mask").live("click", function (event) {
        $('.windowSenha').fadeOut('slow', function () {
            $('.windowSenha').hide();
            $('#mask').hide();
        });
    });
});
